private void checkTimeout() { Date now = new Date(); if (now.after(nextTimeout)) { // prevent: double tick, but no stream -> 2 files.. while (nextTimeout.before(now)) { nextTimeout = new Date((nextTimeout.getTime() + interval)); } // timeout -> change file file = new File( path + "/" + TimeStamp.createLong(new Date(nextTimeout.getTime() - interval)) + "_" + this.hashtag + ".json"); try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } }
/** * Constructs a Stream Writer that writes a json file to disk * * @param hashtag {@link String} for filename * @param interval in ms - afer this time a new file with timestamp will be created */ public HashtagStreamWriter(String hashtag) { this.interval = Config.crawler_newFileInterval; // save date of first call and increment it later (after timeout) nextTimeout = new Date(new Date().getTime() + interval); // normalize hashtag this.hashtag = hashtag.replaceAll("#", ""); // set file location this.path = Config.get_location_tweets() + this.hashtag; // make folder for hashtag (if not already existing) try { File dir = new File(path); dir.mkdirs(); } catch (Exception e) { System.out.println(e.getMessage()); } // create File (hashtag_timestamp.json) file = new File(path + "/" + TimeStamp.getLong() + "_" + this.hashtag + ".json"); try { file.createNewFile(); System.out.println("new Stream to File" + file.getAbsolutePath()); } catch (IOException e) { System.out.println(e.getMessage()); } }