如何通过java打开.docx,.txt,.pptx等现有文件?

Kas*_*inn 8 java

我想知道如何通过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(和更多)文件.