summaryrefslogtreecommitdiff
path: root/src/Chapter4/util/TweetFileProcessor.java
diff options
context:
space:
mode:
authorPeter Wu <peter@lekensteyn.nl>2014-04-23 12:22:20 +0200
committerPeter Wu <peter@lekensteyn.nl>2014-04-23 12:22:20 +0200
commit14d7547cd31c5be878e377a4a5370f604c8d59d4 (patch)
tree003840f1a21d39b07d45cd3112c38b6eed40e3ab /src/Chapter4/util/TweetFileProcessor.java
downloadTwitterDataAnalytics-14d7547cd31c5be878e377a4a5370f604c8d59d4.tar.gz
Initial commit
build.xml, etc. are modified a bit after opening in Netbeans 7.4.
Diffstat (limited to 'src/Chapter4/util/TweetFileProcessor.java')
-rw-r--r--src/Chapter4/util/TweetFileProcessor.java76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/Chapter4/util/TweetFileProcessor.java b/src/Chapter4/util/TweetFileProcessor.java
new file mode 100644
index 0000000..9b6b99c
--- /dev/null
+++ b/src/Chapter4/util/TweetFileProcessor.java
@@ -0,0 +1,76 @@
+package Chapter4.util;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.UnsupportedEncodingException;
+import java.util.Iterator;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+public class TweetFileProcessor implements Iterator<JSONObject>{
+
+ protected BufferedReader fileBuffer;
+ protected boolean endOfFile;
+ protected String nextLine;
+
+ public TweetFileProcessor(File f){
+
+ endOfFile = false;
+
+ InputStreamReader isr;
+ BufferedReader br = null;
+ try {
+ isr = new InputStreamReader(new FileInputStream(f), "UTF-8");
+ br = new BufferedReader(isr);
+ nextLine = br.readLine();
+ } catch (UnsupportedEncodingException e) {
+ e.printStackTrace();
+ endOfFile = true;
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ endOfFile = true;
+ } catch (IOException e) {
+ e.printStackTrace();
+ endOfFile = true;
+ }
+ finally{
+ fileBuffer = br;
+ }
+ }
+
+ @Override
+ public boolean hasNext() {
+ return !endOfFile;
+ }
+
+ @Override
+ public JSONObject next() {
+ JSONObject obj = null;
+ try {
+ obj = new JSONObject(nextLine);
+ } catch (JSONException ex) {
+ Logger.getLogger(TweetFileProcessor.class.getName()).log(Level.SEVERE, null, ex);
+ }
+ try {
+ nextLine = fileBuffer.readLine();
+ if(nextLine == null){
+ endOfFile = true;
+ }
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ return obj;
+ }
+
+ @Override
+ public void remove() throws UnsupportedOperationException{
+ throw new UnsupportedOperationException();
+ }
+}