如何打开文件

import java.io.File;
import java.awt.Desktop;

public class OpenFileExample {

public static void main(String[] args) {
String filePath = "C:\\Users\\User\\Documents\\example.txt";

File file = new File(filePath);

if (!file.exists() || !file.canRead()) {
System.out.println("无法读取该文件");
return;
}

try {
Desktop desktop = Desktop.getDesktop();
desktop.open(file);
} catch (Exception e) {
e.printStackTrace();
}
}
}

posted @ 2023-05-12 22:11  子过杨梅  阅读(6)  评论(0)    收藏  举报