diff options
Diffstat (limited to 'src/io/DataWriter.java')
-rw-r--r-- | src/io/DataWriter.java | 57 |
1 files changed, 23 insertions, 34 deletions
diff --git a/src/io/DataWriter.java b/src/io/DataWriter.java index c26dbf3..42d69f8 100644 --- a/src/io/DataWriter.java +++ b/src/io/DataWriter.java @@ -39,28 +39,26 @@ public class DataWriter implements ResultListener { * the buffer of profile ids that already exist. */ private final Set<Long> m_profileIdSet; - + public final static String CFG_PROFILE_FILENAME = "profiles-filename"; - public final static String CFG_TWEETS_FILENAME = "tweets-filename"; + public final static String CFG_TWEETS_FILENAME = "tweets-filename"; /** * Opens a stream to every single file that data will be streamed to. + * * @param profilesName The file to write the profiles to. * @param tweetsName The file to write the tweets to. + * @throws java.io.IOException if the files cannot be read or written. */ - public DataWriter(final String profilesName, final String tweetsName) { - try { - m_profileIdSet = readIds(profilesName); - m_profileWriter = new FileWriter(profilesName, true); + public DataWriter(final String profilesName, final String tweetsName) + throws IOException { + m_profileIdSet = readIds(profilesName); + m_profileWriter = new FileWriter(profilesName, true); - m_tweetIdSet = readIds(tweetsName); - m_tweetWriter = new FileWriter(tweetsName, true); - } catch (IOException ex) { - // This should not happen. - throw new RuntimeException(ex.getMessage()); - } + m_tweetIdSet = readIds(tweetsName); + m_tweetWriter = new FileWriter(tweetsName, true); } - + public void close() { try { m_tweetWriter.close(); @@ -82,18 +80,15 @@ public class DataWriter implements ResultListener { /** * Read the current existing tweetName and profileName filenames and fill - * the existing id set, it will create the file when it doesn't exist. + * the existing id set. * - * @param filename The file to parse - * @return The set of ids + * @param filename The file to parse. + * @return The set of ids, may be empty if the fill does not exist. */ private Set readIds(String filename) throws IOException { + Set<Long> idSet = new HashSet<>(); try { Scanner reader = new Scanner(new File(filename)); - - // TODO: Read the file JSON objects and parse the ids. - Set idSet = new HashSet(); - // parse each line into a JSONObject, read the id and add it to // the set of ids. while (reader.hasNext()) { @@ -101,29 +96,23 @@ public class DataWriter implements ResultListener { long id = obj.getLong("id"); idSet.add(id); } - - return idSet; } catch (FileNotFoundException ex) { - // File does not exist, so create one. - File file = new File(filename); - // Return value should always be true. - file.createNewFile(); + // ignore, file will be created if necessary. } catch (JSONException ex) { - getLogger().log(Level.SEVERE, null, ex); + getLogger().log(Level.WARNING, filename + + ": File is only partially processed", ex); } - - // return the empty set. - return new HashSet(); + return idSet; } /** * Writes the JSONObject to a writer and update the idSet. - * + * * @param obj The object to write. * @param writer The writer object to append the object to. * @param idSet The id set to add the obj id to. */ - private void writeObject(JSONObject obj, FileWriter writer, Set idSet) { + private void writeObject(JSONObject obj, FileWriter writer, Set<Long> idSet) { try { long id = obj.getLong("id"); @@ -133,11 +122,11 @@ public class DataWriter implements ResultListener { writer.write(obj.toString() + "\n"); idSet.add(id); } catch (IOException ex) { - getLogger().log(Level.SEVERE, null, ex); + getLogger().log(Level.WARNING, "Cannot write to file", ex); } } } catch (JSONException ex) { - getLogger().log(Level.SEVERE, null, ex); + getLogger().log(Level.WARNING, "ID not found?!", ex); } } |