I'm writing a program that takes a number of tweets from a user timeline using twitter4j and writes them to a CSV file. At the moment my code returns all the tweets to console but only writes the last value to my .CSV file. I know there's an issue somewhere as in the console it displays "done" after each tweet.
Twitter twitter1 = TwitterFactory.getSingleton();
List<Status> statuses = twitter1.getUserTimeline();
System.out.println("Showing home timeline.");
for (Status status : statuses) {
System.out.println(status.getUser().getScreenName() + ":" +
status.getText());
PrintWriter pw = new PrintWriter(new File("/Desktop/cwk/4104/tweets.csv"));
StringBuilder sb = new StringBuilder ();
sb.append("ID");
sb.append(",");
sb.append("UserId");
sb.append(",");
sb.append("Username");
sb.append(",");
sb.append("Name");
sb.append(",");
sb.append("Location");
sb.append(",");
sb.append("Tweet");
sb.append("\n");
for( int i = 0; i < 20; i++ ) {
sb.append(1+i);
sb.append(",");
sb.append(status.getUser().getId());
sb.append(",");
sb.append(status.getUser().getScreenName());
sb.append(",");
sb.append(status.getUser().getName());
sb.append(",");
sb.append(status.getUser().getLocation());
sb.append(",");
sb.append(status.getText());
sb.append("\n");
}
pw.write(sb.toString());
pw.close();
System.out.println("done");
}