Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.mycompany.mech2015task; import java.io.*; import java.nio.channels.FileChannel; import twitter4j.StallWarning; import twitter4j.Status; import twitter4j.StatusDeletionNotice; import twitter4j.StatusListener; import twitter4j.*; import twitter4j.TwitterStream; import twitter4j.TwitterStreamFactory; import twitter4j.conf.Configuration; import twitter4j.conf.ConfigurationBuilder; public class App { private static final String CONSUMER_KEY = ""; private static final String CONSUMER_SECRET = ""; private static final String ACCESS_TOKEN = ""; private static final String ACCESS_TOKEN_SECRET = ""; public static void copyFile(File from, File to) throws IOException { if (!to.exists()) { to.createNewFile(); } try (FileChannel in = new FileInputStream(from).getChannel(); FileChannel out = new FileOutputStream(to).getChannel()) { out.transferFrom(in, 0, in.size()); } } static class MyStatusListener implements StatusListener { public void onStatus(Status status) { Twitter twitter = new TwitterFactory().getInstance(); System.out.println("Status: " + status.getText()); String[] parts = status.getText().split(" "); if (status.getText().startsWith("mech2015task add")) { try { Status st = twitter.updateStatus("@" + status.getUser().getScreenName() + " " + status.getText().substring(17, status.getText().length()) + "?????"); } catch (TwitterException e) { } try { File file = new File("task.txt"); copyFile(file, new File("task_old.txt")); if (parts.length > 2) { BufferedWriter bw = new BufferedWriter(new FileWriter(file, true)); bw.write(status.getText().substring(17, status.getText().length())); bw.newLine(); bw.close(); } } catch (IOException e) { } } else if (status.getText().equals("mech2015task list")) { // try{ // Status st = twitter.updateStatus("@" + status.getUser().getScreenName() + " " + status.getText().substring(17, status.getText().length())); // } // catch(TwitterException e){} try { // if (parts.length > 2) { File file = new File("task.txt"); BufferedReader br = new BufferedReader(new FileReader(file)); String str = br.readLine(); String text = ""; while (str != null) { System.out.println(str); text += " " + str + "\n"; System.out.println(text); str = br.readLine(); } br.close(); Status st = twitter.updateStatus("@" + status.getUser().getScreenName() + " ????\n" + text + "??"); /* bw.write(status.getText().substring(17, status.getText().length())); bw.newLine(); bw.close();*/ // } } catch (IOException e) { } catch (TwitterException e) { } } else if (status.getText().startsWith("mech2015task delete")) { String delete_str = status.getText().substring(20, status.getText().length()); String text = ""; System.out.println(delete_str); // try{ // Status st = twitter.updateStatus("@" + status.getUser().getScreenName() + " " + status.getText().substring(17, status.getText().length())); // } // catch(TwitterException e){} try { // if (parts.length > 2) { File file = new File("task_old.txt"); copyFile(new File("task.txt"), file); File file_out = new File("task.txt"); BufferedWriter bw = new BufferedWriter(new FileWriter(file_out)); BufferedReader br = new BufferedReader(new FileReader(file)); String str = br.readLine(); while (str != null) { System.out.println(str); if (str.contains(delete_str)) { text += " " + str + "\n"; } else { bw.write(str); bw.newLine(); } //System.out.println(text); str = br.readLine(); } br.close(); bw.close(); if (!text.equals("")) { Status st = twitter.updateStatus("@" + status.getUser().getScreenName() + " " + text + "?????"); } else { Status st = twitter.updateStatus("@" + status.getUser().getScreenName() + " delete failed"); } /* bw.write(status.getText().substring(17, status.getText().length())); bw.newLine(); bw.close();*/ // } } catch (IOException e) { } catch (TwitterException e) { } } if (status.getText().equals("??")) { try { Status st = twitter .updateStatus("@" + status.getUser().getScreenName() + " ??????"); } catch (TwitterException e) { } } // System.out.println("@" + status.getUser().getScreenName() + " | " + status.getText() + " ? https://twitter.com/" + status.getUser().getScreenName() + "/status/" + status.getId() + " "); // ????status??????????????????????????? } public void onDeletionNotice(StatusDeletionNotice sdn) { System.out.println("onDeletionNotice."); } public void onTrackLimitationNotice(int i) { System.out.println("onTrackLimitationNotice.(" + i + ")"); } public void onScrubGeo(long lat, long lng) { System.out.println("onScrubGeo.(" + lat + ", " + lng + ")"); } public void onException(Exception excptn) { System.out.println("onException."); } @Override public void onStallWarning(StallWarning arg0) { // TODO Auto-generated method stub } } public static void main(String[] args) throws Exception { ConfigurationBuilder builder = new ConfigurationBuilder(); builder.setOAuthConsumerKey(CONSUMER_KEY); builder.setOAuthConsumerSecret(CONSUMER_SECRET); builder.setOAuthAccessToken(ACCESS_TOKEN); builder.setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET); // ??Twitter4J??API???betastream.twitter.com??????? builder.setUserStreamBaseURL("https://userstream.twitter.com/2/"); // Configuration? Configuration configuration = builder.build(); /* Configuration configuration = new ConfigurationBuilder().setOAuthConsumerKey(CONSUMER_KEY) .setOAuthConsumerSecret(CONSUMER_SECRET) .setOAuthAccessToken(ACCESS_TOKEN) .setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET) .build(); */ TwitterStream twStream = new TwitterStreamFactory(configuration).getInstance(); //TwitterStream twStream = new TwitterStreamFactory().getInstance(); twStream.addListener(new MyStatusListener()); twStream.user(); //twStream.sample(); } }