SpilledcupofJava
SpilledcupofJava

Reputation: 33

How can I save java all lines from java console output to a text file and properly append it?

I am new to Java and have been trying to string some open source code together to search tweets and finally was able to succeed. Then I wanted to save the output to a text file. I searched and reviewed console out methods, filewriter, printwriter and I found one on here that works but it's saving only one tweet and overwrites the previous one it saves. How can I properly append the existing text file without overwriting a previous save and make sure it saves all the tweets from the console screen? Example code below:

    JSONObject js = new JSONObject(buff.toString());  
    JSONArray tweets = js.getJSONArray("results");  
    JSONObject tweet;  
    for(int i=0;i<tweets.length();i++) {  
        tweet = tweets.getJSONObject(i); 
        PrintWriter out;
         try {
        out = new PrintWriter(new FileWriter("C:\\tweet\\outputfile.txt"));
        System.out.println((i+1)+")http://twitter.com/"+tweet.getString("from_user")+" at "+tweet.getString("created_at"));  
        System.out.println(tweets.getJSONObject(i).getString("text")+"\n");
        out.println((i+1)+")http://twitter.com/"+tweet.getString("from_user")+" at "+tweet.getString("created_at"));
        out.println(tweets.getJSONObject(i).getString("text")+"\n");

         out.close();
    } catch (IOException e) {
        e.printStackTrace();
    } 


}

} }

Upvotes: 3

Views: 7835

Answers (4)

public final String path ="your path"

        PrintWriter pw = new PrintWriter(new FileWriter(path),true);/*automatically append the line you want to save and if false it will overwrite the data */

pw.write("what ever you want")

        pw.close();

Upvotes: -1

Alpesh Gediya
Alpesh Gediya

Reputation: 3794

Instead of creating object of PrintWriter and FileWriter object during each iteration of your for loop you should initialize PrintWriter outside of you for loop(its will improve performance)finally to release resource.

PrintWriter  out = null ;
try
{
  // putting
  out = new PrintWriter(new FileWriter("C:\\tweet\\outputfile.txt"),true); // appending file if exists.
  // JSON Parsing instructions
  for(int i=0;i<tweets.length();i++) 
  { 
    // processing logic and write operation to file
  }
}
catch(CustomExceptions e) {//All other exception handling}
catch(Exception e){//Generic exception handling }
finally{
          if(out != null
          {
              out.close();
          }
       }

Upvotes: 1

Jhon
Jhon

Reputation: 582

Change this line (Because it will erase your previously written data):

out = new PrintWriter(new FileWriter("C:\\tweet\\outputfile.txt"));

For (Here you are setting Append mode to true)

out = new PrintWriter(new FileWriter("C:\\tweet\\outputfile.txt"), true);

Default mode is set to false so it will overwrite your file.

Upvotes: 1

greedybuddha
greedybuddha

Reputation: 7507

You are so tantalizingly close. You just need to open the FileWriter with append set to true. Append will make it add to the end of the file, as opposed to overwriting it every time.

out = new PrintWriter(new FileWriter("C:\\tweet\\outputfile.txt", true));

Upvotes: 5

Related Questions