import java.util.regex.Matcher;
import java.util.regex.Pattern;
import twitter4j.Status;
import twitter4j.StatusDeletionNotice;
import twitter4j.StatusListener;
import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
public class StreamReader implements StatusListener{
/**
* @param args
*/
public static void main(String[] args) {
StreamReader streamReader = new StreamReader(User, Password);
streamReader.start();
}
/**
* ユーザ名
*/
String userName;
/**
* パスワード
*/
String password;
/**
* stream
*/
private TwitterStream streamTwitter;
public StreamReader(String userName, String password) {
super();
this.userName = userName;
this.password = password;
}
public void start() {
TwitterStreamFactory factory = new TwitterStreamFactory();
streamTwitter = factory.getInstance(userName, password);
streamTwitter.addListener(this);
streamTwitter.sample();
}
@Override
public void onStatus(Status status) {
if(isJapanese(status.getText())){
System.out.printf("%d\t%s\t%s\n", status.getId(), status.getUser().getScreenName(), status.getText());
//得られたStatusに対する処理をする
}
}
/**
* 日本語かどうかを返す
* @param text
* @return 日本語ならtrue
*/
boolean isJapanese(String text){
Matcher m = Pattern.compile("([\\p{InHiragana}\\p{InKatakana}])").matcher(text);
return m.find();
}
@Override
public void onDeletionNotice(StatusDeletionNotice arg0) {
}
@Override
public void onException(Exception arg0) {
}
@Override
public void onTrackLimitationNotice(int arg0) {
}
}
多分,これで動くはず.おまけで日本語だけを取得するようにしておいた.








