Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Display a file in Notepad

 
Pomchi Bedi
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic