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();
}
}
} }