How can I free Notepad with Java code

I want to open Notepad in MS Windows using Java code to open a text file.

Help me do this.

+1
java
source share
4 answers

You can use java.awt.Desktop if you use Java 1.6, .txt registered in notepad and the desktop is supported:

  if (!Desktop.isDesktopSupported()) { System.err.println("Desktop not supported"); // use alternative (Runtime.exec) return; } Desktop desktop = Desktop.getDesktop(); if (!desktop.isSupported(Desktop.Action.EDIT)) { System.err.println("EDIT not supported"); // use alternative (Runtime.exec) return; } try { desktop.edit(new File("test.txt")); } catch (IOException ex) { ex.printStackTrace(); } 

this way you can open / edit files in a more OS-independent way.

+10
source share
 Runtime.getRuntime().exec("notepad c:/asd.txt"); 

where c:/asd.txt is the full path to your text file. If / does not work for you, use \\ .

+3
source share

use class ProcessBuilder

  Process p = new ProcessBuilder("notepad", "file.txt").start(); 
+3
source share

If you registered the .txt extension in your OS and your text file already exists, you can even

 Runtime.getRuntime().exec(new String[]{"cmd.exe","/c","text.txt"}); 

The advantage is that the program will be associated with .txt, which may differ from notepad.exe.

+3
source share

All Articles