File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Display a file in Notepad Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Display a file in Notepad" Watch "Display a file in Notepad" New topic
Author

Display a file in Notepad

Pomchi Bedi
Ranch Hand

Joined: Apr 03, 2001
Posts: 55
Hi,
Can I open/display a txt file in Notepad or any other editor by clicking on the open button in JFileChooser's openDialog.
Thanks
Pomchi
Ashwin Desai
Ranch Hand

Joined: Jul 17, 2000
Posts: 124
Yes. You can create a new process when the user presses the Open button.
for e.g.

Hope this helps.
Ashwin.
Pomchi Bedi
Ranch Hand

Joined: Apr 03, 2001
Posts: 55
Hi Ashwin,
Thanks for the reply.
The command:
Process p = Runtime.getRuntime().exec("notepad");
is working fine.It opens the notepad,but when I pass the filename/path argument and press the open button then it says ArrayIndexOutOfBoundsException is caught.
I have never used Process or Runtime class ever so I am not able to understand what is going wrong.

Thanks again
Pomchi

[This message has been edited by Pomchi Bedi (edited May 03, 2001).]
Ashwin Desai
Ranch Hand

Joined: Jul 17, 2000
Posts: 124
Hi,
This is how I had used in my programs and it executes properly !
Process p = Runtime.getRuntime().exec("notepad.exe Test.java");
I could think of only one thing. Check the fileName that is returned by the FileChooser. On Windows it will return a filename like "c:\MyDir\Test.java". If that is the case, replace all the "\" by "\\" and see what happens.
Or catch the exception and print the stacktrace using e.printStackTrace() to further debug the problem.
Ashwin.

Pomchi Bedi
Ranch Hand

Joined: Apr 03, 2001
Posts: 55
Hi Ashwin,
Thanks buddy!
It is working fine now.Actually I was doing a very stupid mistake. I had the file name and path stored in a variable and I wanted to pass the variable.
I was using the following command for that.
String s="new.txt"
Process p = Runtime.getRuntime().exec("notepad s");
Ideally I should have done:
Process p = Runtime.getRuntime().exec("notepad " + s);
But sometimes these small things don't come into your mind.
Thanks again
-Pomchi
 
jQuery in Action, 2nd edition
 
subject: Display a file in Notepad
 
Similar Threads
how to write comma in CSV file
opening src.jar ... Need Help!
Can I persist state of an object that runs exe file like notepad
a small GUI question
open text file