-2

All I was able to find while searching through previously asked questions was how to open/write/read from a text file.

I was wondering how I would go about physically opening a text file (as if I opened it myself). Is there some command for this, and if so, can it be applied to other programs? ex: paint, calculator, microsoft word, ect...

Thanks!

Lain
  • 2,166
  • 4
  • 23
  • 47

4 Answers4

2

The answer depends...

Do you want to open a file in a platform independent manner so it opens the default editor associated to the file as specified by the OS or not...

If you do, then you should take a look at Desktop

For example...

File file = ...;
Desktop.getDesktop().open(file);

or

Desktop.getDesktop().edit(file);

Now, remember, this will open the associated program for the give action and is specific to the user's context, that is, I have NotePad++ setup to edit *.txt files, so on my system using this method will open NotePad++, on other systems it may open NotePad or what ever the user has configured for that file

Take a look at How to Integrate with the Desktop Class for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

Try this:

Process p = Runtime.getRuntime().exec("notepad.exe");

For more details check:

http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html
http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
1

You can get the program you want to execute as a file Object, e.g:

File f = new File("C:\\test\\test.exe");

Then execute it using

Desktop.getDesktop().open(f);

Torhan Bartel
  • 550
  • 6
  • 33
0

Have you tried

Process process = Runtime.getRuntime().exec("notepad.exe");
process.waitFor();
Ove Sundberg
  • 333
  • 3
  • 10