BufferedWriter doesn't work
in
Contributed Library Questions
•
1 month ago
Dear all interested reader,
I'm trying to make an example I found on this forum work:
The function that doesn't work in Processing 2.0 is the BufferedWriter. How can I update my code so that it works?
I allready updated this line myself:
// Date ddate = new Date();
java.util.Date ddate = new java.util.Date();
Date ddate = new Date(); wasn't working. I guess I need to do something simular with the Writer, but I cant find out how.
Hope that someone can help me with this.
Thanks in advance.
Joshua
I'm trying to make an example I found on this forum work:
- String ConsumerKey = "4oucztsQs3oMPPOD39Sg";
- String ConsumerSecret = "I9USGJugckIZ4y1hFlq5lSJl6scsgbQgXiJEPM7v7zA";
- String oauth_token = "79202117-nWKjs9tJyMVV4EgV4rYjd7ZPysEjP3bX5Cp8BvilE";
- String oauth_token_secret = "jDzfeldXJwWNOR3XAcyssKgqYEM5hWHgbEdSMuZfs";
- TwitterFactory tf;
- String geoURL;
- double lat;
- double lon;
- double res;
- String resUnit;
- import java.util.List;
- void setup() { println("starting...");
- println();
- ConfigurationBuilder cb = new ConfigurationBuilder(); //ConfigurationBuilder declared cb is a new instance of this
- cb.setDebugEnabled(true)
- .setOAuthConsumerKey(ConsumerKey) //sets the Consumer Key String
- .setOAuthConsumerSecret(ConsumerSecret) //sets the Consumer Secret String .setOAuthAccessToken(oauth_token)
- .setOAuthAccessTokenSecret(oauth_token_secret);
- tf = new TwitterFactory(cb.build());
- }
- void draw() {
- println("entering void draw...");
- // Date ddate = new Date();
- java.util.Date ddate = new java.util.Date();
- System.out.println(ddate + "\n"+ "\n");
- // These values are from Milan and probably you'llhave to change them according to your position
- lat = 45.49985;
- lon = 9.191093;
- res = 25;
- resUnit="km";
- try {
- //FILE SAVE DATA (change the path from below)
- String filename = "/twitterLog.csv";
- boolean newFile=false;
- File f = new File(filename);
- if(!f.exists()){
- println("creating file...");
- f.createNewFile();
- newFile=true;
- println("new file "+ filename +" has been created " + newFile);
- }else{
- println("opening the existing file..." + filename);
- }
- BufferedWriter writer = new BufferedWriter(new FileWriter(f, true));
- //
- if (newFile == true){
- writer.write("day,month,hour,min,sec,user,longitude,latitude,\n");
- writer.flush();
- }
- int count = 0;
- try {
- // this value below is about 3 hours of running time
- while (count!=1000) {
- Twitter twitter = tf.getInstance();
- try {
- //TWITTER DATA
- Query query = new Query().geoCode(new GeoLocation(lat,lon), res, resUnit);
- query.rpp(100);
- QueryResult result = twitter.search(query);
- List <Tweet> tweets = result.getTweets();
- for (int i=0; i < tweets.size(); i++) {
- Tweet tweet = (Tweet)tweets.get(i);
- if (tweet.getGeoLocation()!= null){
- println("Found!");
- GeoLocation loc=tweet.getGeoLocation();
- //Transorming variables
- String myLon = Double.toString(loc.getLongitude());
- String myLat = Double.toString(loc.getLatitude());
- String user = tweet.getFromUser();
- Date date = tweet.getCreatedAt();
- // Splitting the date in day-month-hour-min-sec
- DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
- DateFormat ObjMonth = new SimpleDateFormat("MM");
- String MyMonth = ObjMonth.format(date);
- DateFormat ObjDay = new SimpleDateFormat("dd");
- String MyDay = ObjDay.format(date);
- DateFormat ObjHour = new SimpleDateFormat("kk");
- String MyHour = ObjHour.format(date);
- DateFormat ObjMin = new SimpleDateFormat("mm");
- String MyMin = ObjMin.format(date);
- DateFormat ObjSec = new SimpleDateFormat("ss");
- String MySec = ObjSec.format(date);
- String[] data = new String[8];
- data[0] = MyDay; data[1] = MyMonth; data[2] = MyHour; data[3] = MyMin; data[4] = MySec; data[5] =
- user; data[6] = myLon; data[7] = myLat;
- //this if case will fill the array just if the search is null
- if (data[0] ==null){
- data[0] = "24"; data[1] = "05"; data[2] = "17"; data[3] = "19"; data[4] = "55"; data[5] = "ikkio"; data[6] =
- "9.2033"; data[7] = "45.4853";
- }
- for (int k=0; k < data.length; k++) {
- writer.write(data[k] + ",");
- }
- writer.write("\n");
- writer.write("\n");
- writer.flush();
- //println("Created at: " + MyDay +"-"+ MyMonth +" at: "+ MyHour +"-"+ MyMin +"-"+ MySec + " @" + user + ": longitude: "+ myLon + " latitude: "+ myLat );
- }
- }
- println("--");
- println("sleeping...");
- println("--");
- println("Count: "+ count);
- println();
- println();
- count +=1;
- Thread.sleep(10400);
- } catch (TwitterException te) {
- te.printStackTrace();
- println("TWITTER ERROR, Failed to search tweets: " + te.getMessage());
- exit();
- }
- }
- //exit WHILE (COUNT o TRUE)
- writer.close();
- exit();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- } catch (IOException ioe) {
- println("IO/ERROR: " + ioe);
- }
- }
The function that doesn't work in Processing 2.0 is the BufferedWriter. How can I update my code so that it works?
I allready updated this line myself:
Date ddate = new Date(); wasn't working. I guess I need to do something simular with the Writer, but I cant find out how.
Hope that someone can help me with this.
Thanks in advance.
Joshua
1