hello guys, i need a lil guidance here
i am trying to save the output from my "SThreads" results to a txt file without deleting the
previously stored data, i jst want to keep adding to the file..any suggestions that will help are appreciated..

heres the code i have done so far,

import java.io.*;


public class SThread extends Thread {
private int countDown = 5;
private int threadNumber;
private static int threadCount = 0;
public SThread() {
threadNumber = ++threadCount;
System.out.println("Making " + threadNumber);


}
public void run() {
while(true) {
System.out.println("Thread " +
threadNumber + "(" + countDown + ")");
if(--countDown == 0) return;


}
}
public static void main(String[] args) {
for(int i = 0; i < 5; i++)
new SThread().start();
System.out.println("All Threads Started");
try
{


BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String lineFromInput = in.readLine();
PrintWriter out = new PrintWriter(new FileWriter("d:output.txt"));\\ file to output or save to
out.println(lineFromInput);
out.close();


}
catch(IOException e)
{
System.out.println("Error during reading/writing");
}
}
}

thnx for your time..

tc

Recommended Answers

All 6 Replies

Use this constructor for your FileWriter to append to the file instead of overwriting it: FileWriter.FileWriter(java.lang.String,boolean)

You will also need to change \\ file to output or save to to // file to output or save to if you want that to compile - wrong slashes for an inline comment.

srry wrong slashes, i realised after writing the post, i see what your saying but do i have to eliminate anything from my coding, because when i run it, it doenst save anything to the specified file..and where would that line come into my coding..

Well, as you have it written it would just save the single line read from the console to that file. You are not reading anything from your threads and currently those threads don't have any output anyway, so there seems little to save at this point.

thanks dude,, for the advice,

i found another way to save to the txt file, from the command line using the follwing command:

Java nameofcode>filename.txt

tc..now.

Well yes, if all you want to do is pipe your program output stream to a file that is fine. If you need to have any greater control over what gets written to the file you'll need to use the FileWriter.

cool.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.