How to open the notepad file in java?

放肆的年华 提交于 2019-11-27 01:25:33

问题


I want to open Notepad in my Java program. Suppose that I have one button if I click this button the notepad will appear. I already have a file name and a directory.

How can I implement this case?


回答1:


Try

if (Desktop.isDesktopSupported()) {
    Desktop.getDesktop().edit(file);
} else {
    // dunno, up to you to handle this
}

Make sure the file exists. Thanks to Andreas_D who pointed this out.




回答2:


(assuming you want notepad to open "myfile.txt" :)

ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "myfile.txt");
pb.start();



回答3:


Assuming you wish to launch the windows program notepad.exe, you are looking for the exec function. You probably want to call something like:

Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("C:\\path\\to\\notepad.exe C:\\path\\to\\file.txt");

For example, on my machine notepad is located at C:\Windows\notepad.exe:

Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("C:\\Windows\\notepad.exe C:\\test.txt");

This will open notepad with the file test.txt open for editing.

Note you can also specify a third parameter to exec which is the working directory to execute from - therefore, you could launch a text file that is stored relative to the working directory of your program.




回答4:


Using SWT, you can launch any If you want to emulate double-clicking on a text in windows, it's not possible only with a plain JRE. You can use a native library like SWT and use the following code to open a file:

    org.eclipse.swt.program.Program.launch("c:\path\to\file.txt")

If you don't want to use a third-party lib, you should know and you know where notepad.exe is (or it's visible in PATH):

    runtime.exec("notepad.exe c:\path\to\file.txt");

Apache common-exec is a good library for handling external process execution.

UPDATE: A more complete answer to your question can be found here




回答5:


In IDE (Eclipse) it compains about "C:\path\to\notepad.exe C:\path\to\file.txt" . So i have used the following which works for me keeping me and my IDE happy :o) Hopefully this will help others out there.

String fpath;
fPath =System.getProperty("java.io.tmpdir")+"filename1" +getDateTime()+".txt";
//SA - Below launches the generated file, via explorer then delete the file "fPath"
       try { 
        Runtime runtime = Runtime.getRuntime();         
        Process process = runtime.exec("explorer " + fPath);

Thread.sleep(500); //lets give the OS some time to open the file before deleting

    boolean success = (new File(fPath)).delete();
    if (!success) {
        System.out.println("failed to delete file :"+fPath);
        // Deletion failed
    }

} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace(); 
}



回答6:


String fileName = "C:\\Users\\Riyasam\\Documents\\NetBeansProjects\\Student Project\\src\\studentproject\\resources\\RealWorld.chm";
        String[] commands = {"cmd", "/c", fileName};
        try {
            Runtime.getRuntime().exec(commands);
//Runtime.getRuntime().exec("C:\\Users\\Riyasam\\Documents\\NetBeansProjects\\SwingTest\\src\\Test\\RealWorld.chm");
        } catch (Exception ex) {
            ex.printStackTrace();
        }



回答7:


You could do this the best if you start notepad in command line with command: start notepad

String[] startNotePadWithoutAdminPermissions = new String[] {"CMD.EXE", "/C", "start" "notepad" };

Save array of string commands and give it like parametr in exec

Process runtimeProcess = Runtime.getRuntime().exec(startNotepadAdmin2);
runtimeProcess.waitFor();


来源:https://stackoverflow.com/questions/3487149/how-to-open-the-notepad-file-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!