Opening a .txt file from an application

I have an application that extends JFrame. There is a JButton that I want to use to open a file called

"Instructions.txt."

and the path to the file is

"E:\CM0112\Code\Instructions.txt".

I want to open this file using TextPad, I have tried the code

Runtime.getRuntime.().exec()

but I keep getting compile errors. Any hints would be greatly appreciated.

[395 byte] By [Satchya] at [2007-11-26 18:37:48]
# 1
What compile errors?
DavidKNa at 2007-7-9 6:11:52 > top of Java-index,Java Essentials,New To Java...
# 2
Runtime.getRuntime.().exec()^?
hunter9000a at 2007-7-9 6:11:52 > top of Java-index,Java Essentials,New To Java...
# 3
Post. Code.
DrLaszloJamfa at 2007-7-9 6:11:52 > top of Java-index,Java Essentials,New To Java...
# 4
if (ev.getSource() == howToPlay){//Open Instructions.txtRuntime.getRuntime.().exec("Instructions.txt");}
Satchya at 2007-7-9 6:11:52 > top of Java-index,Java Essentials,New To Java...
# 5
That's not a compile error. It's the same line of code with a little hat sign pointing to it.However, the obvious problem is that you have a . there.
DavidKNa at 2007-7-9 6:11:52 > top of Java-index,Java Essentials,New To Java...
# 6
I removed the . and have got an IO Exception compile error
Satchya at 2007-7-9 6:11:52 > top of Java-index,Java Essentials,New To Java...
# 7
Are you catching that exception? Anyway, here is an article on Runtime.exec: http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=1
DrLaszloJamfa at 2007-7-9 6:11:52 > top of Java-index,Java Essentials,New To Java...
# 8
I have caught the exception and the code compiles, however clicking the button does nothing. I have got an action listener on the button so my RunTime code is at fault.
Satchya at 2007-7-9 6:11:52 > top of Java-index,Java Essentials,New To Java...
# 9

import java.io.*;

public class Example {

public static void main(String[] args) throws IOException {

String[] v = {"cmd.exe", "/c", "start", "TextPad.exe", "E:\\CM0112\\Code\\Instructions.txt"};

Runtime.getRuntime().exec(v);

}

}

DrLaszloJamfa at 2007-7-9 6:11:52 > top of Java-index,Java Essentials,New To Java...
# 10

try this code

public class Open

{

public static void main(String[] args)

{

String line;

EasyReader inFile = new EasyReader("instructions.txt")

if(inFile.bad())

{

System.out.println(" *** Can't Open instructions.txt *** ");

System.exit(1);

}

while((line = inFile.readLine()) != null)

{

//implement your own code, each time loop executes

//line is the next line in the file until it is null.

....

}

}

}

Go to www.rfrank.net/cs-labs/1400-tasmania/EasyReader.java to find EasyReader.java

shotgun929a at 2007-7-9 6:11:52 > top of Java-index,Java Essentials,New To Java...
# 11
Er, Shotgun, did you miss the part (Runtime and all that) where the original poster was trying to launch TextPad.exe and open the file in that application?
DrLaszloJamfa at 2007-7-9 6:11:52 > top of Java-index,Java Essentials,New To Java...