Search code examples
javatwittertwitter4j

Not able to Save Tweets during Twitter Streaming


I'm trying to stream tweets and save them from Twitter API using Twitter4j.

public class TwitterStreaming{

private static int count;
private static PrintWriter writer;  
private static PrintWriter writer2;

In Main Function:

TwitterStreaming.writer  = new PrintWriter("FF8Json.txt", "UTF-8");  
    TwitterStreaming.writer2  =new PrintWriter("FF8.txt", "UTF-8");

   TwitterStreaming.count=0;

    //writer.println("hello :)");
    //writer2.println("Check 123");

    StatusListener listener;
    listener = new StatusListener() {
        @Override
        public void onStatus(Status status) {

            TwitterStreaming.writer.println(DataObjectFactory.getRawJSON(status));
            TwitterStreaming.writer2.println(status.getLang()+" - "+status.getText());
            TwitterStreaming.count++;
            System.out.println("Count - "+TwitterStreaming.count);

            //To change the body of generated methods, choose Tools | Templates.
        } 
//Other Override functions are removed over here

        };

FF8Json.txt and FF8.txt both will be empty. I have tried creating a function and sending the data to it and trying to write the data to the file through that function also. But it doesn't work. How can I Solve this? Is there any other way to write the data into a file ?

But when I use System.out.println instead of TwitterStreaming.writer It works.

Whole Code over HERE


Solution

  • Is going to be hard to find something in those files if you dont close the printwriter object..

     writer.close();
    

    and please don't make everything static in the TwitterStreaming class