Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open the notepad file in java?

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?

like image 914
guilgamos Avatar asked Aug 15 '10 11:08

guilgamos


People also ask

How do I open notepad in Java?

Step 1: Open the notepad by pressing the Windows Key + R, type notepad and press enter key, or click on the Ok button. It opens the notepad. Step 2: Write a Java program that you want to compile and run. We have written the following code in the notepad.

How do I open a notepad file in eclipse?

Right click on file, Open with -> Other then select external programs and then choose eclipse .

How do I open a TXT file in notepad?

In Windows, you can open a TXT file with Microsoft Notepad or Microsoft WordPad, both of which come included with Windows. To open a TXT file with Notepad, select File → Open.... In macOS, you can open a TXT file with Apple TextEdit, which comes included with the operating system.


2 Answers

Try

if (Desktop.isDesktopSupported()) {
    Desktop.getDesktop().edit(file);
} else {
    // I don't know, up to you to handle this
}

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

like image 80
whiskeysierra Avatar answered Oct 09 '22 15:10

whiskeysierra


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

ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "myfile.txt");
pb.start();
like image 34
Hal Avatar answered Oct 09 '22 16:10

Hal