You can use BufferedWriter to write to a file:
FileWriter fWriter = null;
BufferedWriter writer = null;
try {
fWriter = new FileWriter("fileName");
writer = new BufferedWriter(fWriter);
//WRITES 1 LINE TO FILE AND CHANGES LINE
writer.write("This line is written");
writer.newLine();
writer.close();
} catch (Exception e) {
}
Also check these APIs BufferedWriter
The BufferedWriter's super class: Writer
FileWriter
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
Take a look at the Java I/O package. I am sure it's a monster containing hundreads of classes but it present as many ways as there can be to write to a file in Java.
OR
You can use a PrintWriter over an OutputStream like the FileOutputStream . The FileOutputStream can be opened on a File object, or a string that specifies the filepath.
OR
For a quick example check here
Together almost the same post
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
If you want to use that code, I'd suggest you simply copy and paste it into another file, and edit it so that it sends its output to a text file rather than prints it on the screen. You can use PrintWriter to do this.
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
I haven't written this kind of java code before but I am familiar with the redirect command: '>'
Maybe this is your problem:
Process p = Runtime.getRuntime().exec("java Add2number>C:\\Program Files\\Java\\jdk1.6.0_03\\bin \\out.txt");
Have you noticed a space between\\bin and \\out
...\\bin \\out
Can you try it with this:
Process p = Runtime.getRuntime().exec("java Add2number>C:\\Program Files\\Java\\jdk1.6.0_03\\bin\\out.txt");
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
Good call.
I don't know if this solves the problem. We will needthijo to reply with an update of his problem
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
I see that you marked the thread solved.
What was the problem?
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
hey guys...i have one question.i have file Sample.data, in that file i write "Hello world". how to write code to separate word between "Hello" and "world" and print out it as output seperately.
Start a new thread.
Or better search this forum. Your question has been answered a million times
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448