我想知道如何通过java打开文件.
我可以像这样打开Office
try {
Runtime runTime = Runtime.getRuntime();
Process process = runTime.exec("C:\\Program Files\\Microsoft Office\\Office15\\EXCEL.EXE");
} catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
但我想直接从java打开文件.
sik*_*der 10
试试这个,
try{
if ((new File("c:\\your_file.pdf")).exists()) {
Process p = Runtime
.getRuntime()
.exec("rundll32 url.dll,FileProtocolHandler c:\\your_file.pdf");
p.waitFor();
} else {
System.out.println("File does not exist");
}
} catch (Exception ex) {
ex.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
或者你可以这样做Desktop.open(File),
if (Desktop.isDesktopSupported()) {
try {
File myFile = new File("/path/to/file.pdf");
Desktop.getDesktop().open(myFile);
} catch (IOException ex) {
// no application registered for PDFs
}
}
Run Code Online (Sandbox Code Playgroud)
您也可以使用此方法打开pptx(和更多)文件.
| 归档时间: |
|
| 查看次数: |
17305 次 |
| 最近记录: |