List of usage examples for java.io FileReader FileReader
public FileReader(FileDescriptor fd)
From source file:Phnbk.java
public static void main(String[] args) throws IOException, Exception { String source = System.getProperty("user.home") + "/input.csv"; Phnbk pb = new Phnbk(); CSVParser parser = new CSVParser(new FileReader(source), CSVFormat.EXCEL.withHeader()); pb.directory(parser);//w w w.j av a2s . co m parser.close(); }
From source file:uk.co.moonsit.rmi.GraphClient.java
@SuppressWarnings("SleepWhileInLoop") public static void main(String args[]) { Properties p = new Properties(); try {/*from w w w .ja v a 2 s. co m*/ p.load(new FileReader("graph.properties")); } catch (IOException ex) { Logger.getLogger(GraphClient.class.getName()).log(Level.SEVERE, null, ex); } //String[] labels = p.getProperty("labels").split(","); GraphClient demo = null; int type = 0; boolean log = false; if (args[0].equals("-l")) { log = true; } switch (type) { case 0: try { System.setProperty("java.security.policy", "file:./client.policy"); if (System.getSecurityManager() == null) { System.setSecurityManager(new RMISecurityManager()); } String name = "GraphServer"; String host = p.getProperty("host"); System.out.println(host); Registry registry = LocateRegistry.getRegistry(host); String[] list = registry.list(); for (String s : list) { System.out.println(s); } GraphDataInterface gs = null; boolean bound = false; while (!bound) { try { gs = (GraphDataInterface) registry.lookup(name); bound = true; } catch (NotBoundException e) { System.err.println("GraphServer exception:" + e.toString()); Thread.sleep(500); } } @SuppressWarnings("null") String config = gs.getConfig(); System.out.println(config); /*float[] fs = gs.getValues(); for (float f : fs) { System.out.println(f); }*/ demo = new GraphClient(gs, 1000, 700, Float.parseFloat(p.getProperty("frequency")), log); demo.init(config); demo.run(); } catch (RemoteException e) { System.err.println("GraphClient exception:" + e.toString()); } catch (FileNotFoundException ex) { Logger.getLogger(GraphClient.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(GraphClient.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { Logger.getLogger(GraphClient.class.getName()).log(Level.SEVERE, null, ex); } finally { if (demo != null) { demo.close(); } } break; case 1: try { demo = new GraphClient(1000, 700, Float.parseFloat(p.getProperty("frequency"))); demo.init("A^B|Time|Error|-360|360"); demo.addData(0, 1, 100); demo.addData(0, 2, 200); demo.addData(1, 1, 50); demo.addData(1, 2, 450); } catch (FileNotFoundException ex) { Logger.getLogger(GraphClient.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(GraphClient.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { Logger.getLogger(GraphClient.class.getName()).log(Level.SEVERE, null, ex); } break; } }
From source file:com.diversityarrays.kdxplore.trials.TrialSelectionDialog.java
static public void main(String[] args) { Transformer<BackgroundRunner, TrialSearchOptionsPanel> factory = new Transformer<BackgroundRunner, TrialSearchOptionsPanel>() { @Override//w w w . j av a2 s. c o m public TrialSearchOptionsPanel transform(BackgroundRunner backgroundRunner) { return TrialSelectionSearchOptionsPanel.create(backgroundRunner); } }; boolean test = false; if (test) { @SuppressWarnings("unchecked") TrialSelectionDialog tsd = new TrialSelectionDialog(null, "Test Tree", null, Collections.EMPTY_LIST, //$NON-NLS-1$ factory); // tsd.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); tsd.setVisible(true); } else { File propertiesFile = new File(System.getProperty("user.home"), //$NON-NLS-1$ "LoginDialog.properties"); //$NON-NLS-1$ //.getBundle("LoginDialog"); //, Locale.getDefault()); ResourceBundle bundle = null; if (propertiesFile.exists()) { try { bundle = new PropertyResourceBundle(new FileReader(propertiesFile)); } catch (FileNotFoundException e) { e.printStackTrace(); System.exit(1); } catch (IOException e) { e.printStackTrace(); System.exit(1); } } Preferences loginPreferences = KdxplorePreferences.getInstance().getPreferences(); // Preferences loginPreferences = Preferences.userNodeForPackage(KdxConstants.class); LoginDialog ld = new LoginDialog(null, "Login Please", loginPreferences, bundle); //$NON-NLS-1$ ld.setVisible(true); DALClient client = ld.getDALClient(); if (client != null) { @SuppressWarnings("unchecked") TrialSelectionDialog tsd = new TrialSelectionDialog(null, "Test Tree", client, //$NON-NLS-1$ Collections.EMPTY_LIST, factory); // tsd.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); tsd.setVisible(true); if (tsd.trialRecords == null) { System.out.println("Cancelled !"); //$NON-NLS-1$ } else { System.out.println(tsd.trialRecords.length + " chosen: "); //$NON-NLS-1$ for (TrialPlus tr : tsd.trialRecords) { System.out.println("\t" + tr.getTrialId() + ": " + tr.getTrialName()); //$NON-NLS-1$ //$NON-NLS-2$ } System.out.println("- - -"); //$NON-NLS-1$ } } } System.exit(0); }
From source file:se.vgregion.portal.cs.util.CryptoUtilImpl.java
/** * Main method used for creating initial key file. * //from w w w . j a v a 2 s .c o m * @param args * - not used */ public static void main(String[] args) { final String keyFile = "./howto.key"; final String pwdFile = "./howto.properties"; CryptoUtilImpl cryptoUtils = new CryptoUtilImpl(); cryptoUtils.setKeyFile(new File(keyFile)); String clearPwd = "my_cleartext_pwd"; Properties p1 = new Properties(); Writer w = null; try { p1.put("user", "liferay"); String encryptedPwd = cryptoUtils.encrypt(clearPwd); p1.put("pwd", encryptedPwd); w = new FileWriter(pwdFile); p1.store(w, ""); } catch (GeneralSecurityException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (w != null) { try { w.close(); } catch (IOException e) { e.printStackTrace(); } } } // ================== Properties p2 = new Properties(); Reader r = null; try { r = new FileReader(pwdFile); p2.load(r); String encryptedPwd = p2.getProperty("pwd"); System.out.println(encryptedPwd); System.out.println(cryptoUtils.decrypt(encryptedPwd)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (GeneralSecurityException e) { e.printStackTrace(); } finally { if (r != null) { try { r.close(); } catch (IOException e) { e.printStackTrace(); } } } }
From source file:edu.illinois.cs.cogcomp.datalessclassification.ta.ESADatalessAnnotator.java
/** * @param args config: config file path testFile: Test File *///from w w w . j a va 2 s .com public static void main(String[] args) { CommandLine cmd = getCMDOpts(args); ResourceManager rm; try { String configFile = cmd.getOptionValue("config", "config/project.properties"); ResourceManager nonDefaultRm = new ResourceManager(configFile); rm = new ESADatalessConfigurator().getConfig(nonDefaultRm); } catch (IOException e) { rm = new ESADatalessConfigurator().getDefaultConfig(); } String testFile = cmd.getOptionValue("testFile", "data/graphicsTestDocument.txt"); StringBuilder sb = new StringBuilder(); String line; try (BufferedReader br = new BufferedReader(new FileReader(new File(testFile)))) { while ((line = br.readLine()) != null) { sb.append(line); sb.append(" "); } String text = sb.toString().trim(); TokenizerTextAnnotationBuilder taBuilder = new TokenizerTextAnnotationBuilder(new StatefulTokenizer()); TextAnnotation ta = taBuilder.createTextAnnotation(text); ESADatalessAnnotator datalessAnnotator = new ESADatalessAnnotator(rm); datalessAnnotator.addView(ta); List<Constituent> annots = ta.getView(ViewNames.DATALESS_ESA).getConstituents(); System.out.println("Predicted LabelIDs:"); for (Constituent annot : annots) { System.out.println(annot.getLabel()); } Map<String, String> labelNameMap = DatalessAnnotatorUtils .getLabelNameMap(rm.getString(DatalessConfigurator.LabelName_Path.key)); System.out.println("Predicted Labels:"); for (Constituent annot : annots) { System.out.println(labelNameMap.get(annot.getLabel())); } } catch (FileNotFoundException e) { e.printStackTrace(); logger.error("Test File not found at " + testFile + " ... exiting"); System.exit(-1); } catch (IOException e) { e.printStackTrace(); logger.error("IO Error while reading the test file ... exiting"); System.exit(-1); } catch (AnnotatorException e) { e.printStackTrace(); logger.error("Error Annotating the Test Document with the Dataless View ... exiting"); System.exit(-1); } }
From source file:gobblin.test.TestWorker.java
@SuppressWarnings("all") public static void main(String[] args) throws Exception { // Build command-line options Option configOption = OptionBuilder.withArgName("framework config file") .withDescription("Configuration properties file for the framework").hasArgs().withLongOpt("config") .create('c'); Option jobConfigsOption = OptionBuilder.withArgName("job config files") .withDescription("Comma-separated list of job configuration files").hasArgs() .withLongOpt("jobconfigs").create('j'); Option modeOption = OptionBuilder.withArgName("run mode") .withDescription("Test mode (schedule|run); 'schedule' means scheduling the jobs, " + "whereas 'run' means running the jobs immediately") .hasArg().withLongOpt("mode").create('m'); Option helpOption = OptionBuilder.withArgName("help").withDescription("Display usage information") .withLongOpt("help").create('h'); Options options = new Options(); options.addOption(configOption);// ww w. j a v a 2 s . c o m options.addOption(jobConfigsOption); options.addOption(modeOption); options.addOption(helpOption); // Parse command-line options CommandLineParser parser = new BasicParser(); CommandLine cmd = parser.parse(options, args); if (cmd.hasOption('h')) { printUsage(options); System.exit(0); } // Start the test worker with the given configuration properties Configuration config = new PropertiesConfiguration(cmd.getOptionValue('c')); Properties properties = ConfigurationConverter.getProperties(config); TestWorker testWorker = new TestWorker(properties); testWorker.start(); // Job running mode Mode mode = Mode.valueOf(cmd.getOptionValue('m').toUpperCase()); // Get the list of job configuration files List<String> jobConfigFiles = Lists .newArrayList(Splitter.on(',').omitEmptyStrings().trimResults().split(cmd.getOptionValue('j'))); CountDownLatch latch = new CountDownLatch(jobConfigFiles.size()); for (String jobConfigFile : jobConfigFiles) { // For each job, load the job configuration, then run or schedule the job. Properties jobProps = new Properties(); jobProps.load(new FileReader(jobConfigFile)); jobProps.putAll(properties); testWorker.runJob(jobProps, mode, new TestJobListener(latch)); } // Wait for all jobs to finish latch.await(); testWorker.stop(); }
From source file:com.flaptor.hounder.indexer.RmiIndexerStub.java
public static void main(String[] args) { // create the parser CommandLineParser parser = new PosixParser(); CommandLine line = null;//ww w. j a v a 2 s . c o m Options options = getOptions(); try { // parse the command line arguments line = parser.parse(options, args); } catch (ParseException exp) { // oops, something went wrong HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("RmiIndexerStub -h <hostName> -p <basePort> [options] ", options); System.exit(1); } boolean doOptimize = line.hasOption("optimize"); boolean doCheckpoint = line.hasOption("checkpoint"); boolean doStop = line.hasOption("stop"); Integer port = ((Long) line.getOptionObject("port")).intValue(); String host = line.getOptionValue("host"); try { RmiIndexerStub stub = new RmiIndexerStub(port, host); if (line.hasOption("deleteUrl")) { String url = line.getOptionValue("deleteUrl"); Document dom = generateDeleteDocument(url); indexOrFail(stub, dom, "Could not delete " + url); System.out.println("delete " + url + " command accepted by indexer"); } if (line.hasOption("deleteFile")) { BufferedReader reader = new BufferedReader(new FileReader(line.getOptionValue("deleteFile"))); while (reader.ready()) { String url = reader.readLine(); if (url.length() > 0 && url.charAt(0) != '#') { // ignore empty lines and comments Document dom = generateDeleteDocument(url); indexOrFail(stub, dom, "Could not delete " + url); System.out.println("delete " + url + " command accepted by indexer"); } } reader.close(); } if (doOptimize) { Document dom = generateCommandDocument("optimize"); indexOrFail(stub, dom, "Could not send optimize command."); System.out.println("optimize command accepted by indexer"); } if (doCheckpoint) { Document dom = generateCommandDocument("checkpoint"); indexOrFail(stub, dom, "Could not send checkpoint command."); System.out.println("checkpoint command accepted by indexer"); } if (doStop) { Document dom = generateCommandDocument("close"); indexOrFail(stub, dom, "Could not send stop command."); System.out.println("stop command accepted by indexer"); } } catch (Exception e) { System.err.println("An error occurred: " + e.getMessage()); e.printStackTrace(); } }
From source file:es.eucm.ead.exporter.ExporterMain.java
@SuppressWarnings("all") public static void main(String args[]) { Options options = new Options(); Option help = new Option("h", "help", false, "print this message"); Option quiet = new Option("q", "quiet", false, "be extra quiet"); Option verbose = new Option("v", "verbose", false, "be extra verbose"); Option legacy = OptionBuilder.withArgName("s> <t").hasArgs(3) .withDescription(//from w ww . j a v a 2s.c o m "source is a version 1.x game; must specify\n" + "<simplify> if 'true', simplifies result\n" + "<translate> if 'true', enables translation") .withLongOpt("import").create("i"); Option war = OptionBuilder.withArgName("web-base").hasArg() .withDescription("WAR packaging (web app); " + "must specify\n<web-base> the base WAR directory") .withLongOpt("war").create("w"); Option jar = OptionBuilder.withDescription("JAR packaging (desktop)").withLongOpt("jar").create("j"); Option apk = OptionBuilder.withArgName("props> <adk> <d").hasArgs(3) .withDescription("APK packaging (android); must specify \n" + "<props> (a properties file) \n" + "<adk> (location of the ADK to use) \n" + "<deploy> ('true' to install & deploy)") .withLongOpt("apk").create("a"); // EAD option Option ead = OptionBuilder.withDescription("EAD packaging (eAdventure)").withLongOpt("ead").create("e"); options.addOption(legacy); options.addOption(help); options.addOption(quiet); options.addOption(verbose); options.addOption(jar); options.addOption(war); options.addOption(apk); options.addOption(ead); CommandLineParser parser = new PosixParser(); CommandLine cmd; try { cmd = parser.parse(options, args); } catch (ParseException pe) { System.err.println("Error parsing command-line: " + pe.getMessage()); showHelp(options); return; } // general options String[] extras = cmd.getArgs(); if (cmd.hasOption(help.getOpt()) || extras.length < 2) { showHelp(options); return; } if (cmd.hasOption(verbose.getOpt())) { verbosity = Verbose; } if (cmd.hasOption(quiet.getOpt())) { verbosity = Quiet; } // import String source = extras[0]; // optional import step if (cmd.hasOption(legacy.getOpt())) { String[] values = cmd.getOptionValues(legacy.getOpt()); AdventureConverter converter = new AdventureConverter(); if (values.length > 0 && values[0].equalsIgnoreCase("true")) { converter.setEnableSimplifications(true); } if (values.length > 1 && values[1].equalsIgnoreCase("true")) { converter.setEnableTranslations(true); } // set source for next steps to import-target source = converter.convert(source, null); } if (cmd.hasOption(jar.getOpt())) { if (checkFilesExist(cmd, options, source)) { JarExporter e = new JarExporter(); e.export(source, extras[1], verbosity.equals(Quiet) ? new QuietStream() : System.err); } } else if (cmd.hasOption(apk.getOpt())) { String[] values = cmd.getOptionValues(apk.getOpt()); if (checkFilesExist(cmd, options, values[0], values[1], source)) { AndroidExporter e = new AndroidExporter(); Properties props = new Properties(); File propsFile = new File(values[0]); try { props.load(new FileReader(propsFile)); props.setProperty(AndroidExporter.SDK_HOME, props.getProperty(AndroidExporter.SDK_HOME, values[1])); } catch (IOException ioe) { System.err.println("Could not load properties from " + propsFile.getAbsolutePath()); return; } e.export(source, extras[1], props, values.length > 2 && values[2].equalsIgnoreCase("true")); } } else if (cmd.hasOption(war.getOpt())) { if (checkFilesExist(cmd, options, extras[0])) { WarExporter e = new WarExporter(); e.setWarPath(cmd.getOptionValue(war.getOpt())); e.export(source, extras[1]); } } else if (cmd.hasOption(ead.getOpt())) { String destiny = extras[1]; if (!destiny.endsWith(".ead")) { destiny += ".ead"; } FileUtils.zip(new File(destiny), new File(source)); } else { showHelp(options); } }
From source file:friendsandfollowers.FilesThreaderFriendsIDsParser.java
public static void main(String[] args) throws ClassNotFoundException, SQLException, JSONException, FileNotFoundException, UnsupportedEncodingException { // Check how many arguments were passed in if ((args == null) || (args.length < 5)) { System.err.println("5 Parameters are required plus one optional " + "parameter to launch a Job."); System.err.println("First: String 'INPUT: DB or /input/path/'"); System.err.println("Second: String 'OUTPUT: /output/path/'"); System.err.println("Third: (int) Total Number Of Jobs"); System.err.println("Fourth: (int) This Job Number"); System.err.println("Fifth: (int) Number of seconds to pause"); System.err.println("Sixth: (int) Number of ids to fetch" + "Provide number which increment by 5000 " + "(5000, 10000, 15000 etc) " + "or -1 to fetch all ids."); System.err.println("Example: fileToRun /input/path/ " + "/output/path/ 10 1 3 75000"); System.exit(-1);/*w w w. j av a 2s.co m*/ } // TODO documentation for command line AppOAuth AppOAuths = new AppOAuth(); Misc helpers = new Misc(); String endpoint = "/friends/ids"; String inputPath = null; try { inputPath = StringEscapeUtils.escapeJava(args[0]); } catch (Exception e) { System.err.println("Argument " + args[0] + " must be an String."); System.exit(-1); } String outputPath = null; try { outputPath = StringEscapeUtils.escapeJava(args[1]); } catch (Exception e) { System.err.println("Argument " + args[1] + " must be an String."); System.exit(-1); } int TOTAL_JOBS = 0; try { TOTAL_JOBS = Integer.parseInt(args[2]); } catch (NumberFormatException e) { System.err.println("Argument " + args[2] + " must be an integer."); System.exit(1); } int JOB_NO = 0; try { JOB_NO = Integer.parseInt(args[3]); } catch (NumberFormatException e) { System.err.println("Argument " + args[3] + " must be an integer."); System.exit(1); } int secondsToPause = 0; try { secondsToPause = Integer.parseInt(args[4]); } catch (NumberFormatException e) { System.err.println("Argument" + args[4] + " must be an integer."); System.exit(-1); } int IDS_TO_FETCH_INT = -1; if (args.length == 6) { try { IDS_TO_FETCH_INT = Integer.parseInt(args[5]); } catch (NumberFormatException e) { System.err.println("Argument" + args[5] + " must be an integer."); System.exit(-1); } } int IDS_TO_FETCH = 0; if (IDS_TO_FETCH_INT > 5000) { float IDS_TO_FETCH_F = (float) IDS_TO_FETCH_INT / 5000; IDS_TO_FETCH = (int) Math.ceil(IDS_TO_FETCH_F); } else if ((IDS_TO_FETCH_INT <= 5000) && (IDS_TO_FETCH_INT > 0)) { IDS_TO_FETCH = 1; } secondsToPause = (TOTAL_JOBS * secondsToPause) - (JOB_NO * secondsToPause); System.out.println("secondsToPause: " + secondsToPause); helpers.pause(secondsToPause); try { int TotalWorkLoad = 0; ArrayList<String> allFiles = null; try { final File folder = new File(inputPath); allFiles = helpers.listFilesForSingleFolder(folder); TotalWorkLoad = allFiles.size(); } catch (Exception e) { System.err.println("Input folder is not exists: " + e.getMessage()); System.exit(-1); } System.out.println("Total Workload is: " + TotalWorkLoad); if (TotalWorkLoad < 1) { System.err.println("No screen names file exists in: " + inputPath); System.exit(-1); } if (TOTAL_JOBS > TotalWorkLoad) { System.err.println("Number of jobs are more than total work" + " load. Please reduce 'Number of jobs' to launch."); System.exit(-1); } float TotalWorkLoadf = TotalWorkLoad; float TOTAL_JOBSf = TOTAL_JOBS; float res = (TotalWorkLoadf / TOTAL_JOBSf); int chunkSize = (int) Math.ceil(res); int offSet = JOB_NO * chunkSize; int chunkSizeToGet = (JOB_NO + 1) * chunkSize; System.out.println("My Share is " + chunkSize); System.out.println(); // Load OAuh User TwitterFactory tf = AppOAuths.loadOAuthUser(endpoint, TOTAL_JOBS, JOB_NO); Twitter twitter = tf.getInstance(); int RemainingCalls = AppOAuths.RemainingCalls; int RemainingCallsCounter = 0; System.out.println("First Time OAuth Remianing Calls: " + RemainingCalls); String Screen_name = AppOAuths.screen_name; System.out.println("First Time Loaded OAuth Screen_name: " + Screen_name); System.out.println(); IDs ids; System.out.println("Going to get friends ids."); // to write output in a file System.out.flush(); if (JOB_NO + 1 == TOTAL_JOBS) { chunkSizeToGet = TotalWorkLoad; } List<String> myFilesShare = allFiles.subList(offSet, chunkSizeToGet); for (String myFile : myFilesShare) { System.out.println("Going to parse file: " + myFile); try (BufferedReader br = new BufferedReader(new FileReader(inputPath + "/" + myFile))) { String line; OUTERMOST: while ((line = br.readLine()) != null) { // process the line. System.out.println("Going to get friends ids of Screen-name / user_id: " + line); System.out.println(); String targetedUser = line.trim(); // tmp long cursor = -1; int idsLoopCounter = 0; int totalIDs = 0; PrintWriter writer = new PrintWriter(outputPath + "/" + targetedUser, "UTF-8"); // call different functions for screen_name and id_str Boolean chckedNumaric = helpers.isNumeric(targetedUser); do { ids = null; try { if (chckedNumaric) { long LongValueTargetedUser = Long.valueOf(targetedUser).longValue(); ids = twitter.getFriendsIDs(LongValueTargetedUser, cursor); } else { ids = twitter.getFriendsIDs(targetedUser, cursor); } } catch (TwitterException te) { // do not throw if user has protected tweets, or // if they deleted their account if (te.getStatusCode() == HttpResponseCode.UNAUTHORIZED || te.getStatusCode() == HttpResponseCode.NOT_FOUND) { System.out.println(targetedUser + " is protected or account is deleted"); } else { System.out.println("Friends Get Exception: " + te.getMessage()); } // If rate limit reached then switch Auth user RemainingCallsCounter++; if (RemainingCallsCounter >= RemainingCalls) { // load auth user tf = AppOAuths.loadOAuthUser(endpoint, TOTAL_JOBS, JOB_NO); twitter = tf.getInstance(); System.out.println( "New Loaded OAuth User " + " Screen_name: " + AppOAuths.screen_name); RemainingCalls = AppOAuths.RemainingCalls; RemainingCallsCounter = 0; System.out.println("New OAuth Remianing Calls: " + RemainingCalls); } // Remove file if ids not found if (totalIDs == 0) { System.out.println("No ids fetched so removing " + "file " + targetedUser); File fileToDelete = new File(outputPath + "/" + targetedUser); fileToDelete.delete(); } System.out.println(); // If error then switch to next user continue OUTERMOST; } if (ids.getIDs().length > 0) { idsLoopCounter++; totalIDs += ids.getIDs().length; System.out.println(idsLoopCounter + ": IDS length: " + ids.getIDs().length); JSONObject responseDetailsJson = new JSONObject(); JSONArray jsonArray = new JSONArray(); for (long id : ids.getIDs()) { jsonArray.put(id); } Object idsJSON = responseDetailsJson.put("ids", jsonArray); writer.println(idsJSON); } // If rate limit reached then switch Auth user RemainingCallsCounter++; if (RemainingCallsCounter >= RemainingCalls) { // load auth user tf = AppOAuths.loadOAuthUser(endpoint, TOTAL_JOBS, JOB_NO); twitter = tf.getInstance(); System.out.println("New Loaded OAuth User Screen_name: " + AppOAuths.screen_name); RemainingCalls = AppOAuths.RemainingCalls; RemainingCallsCounter = 0; System.out.println("New OAuth Remianing Calls: " + RemainingCalls); } if (IDS_TO_FETCH_INT != -1) { if (idsLoopCounter == IDS_TO_FETCH) { break; } } } while ((cursor = ids.getNextCursor()) != 0); writer.close(); System.out.println("Total ids dumped of " + targetedUser + " are: " + totalIDs); // Remove file if ids not found if (totalIDs == 0) { System.out.println("No ids fetched so removing " + "file " + targetedUser); File fileToDelete = new File(outputPath + "/" + targetedUser); fileToDelete.delete(); } System.out.println(); } // while get records from single file } // read my single file catch (IOException e) { System.err.println("Failed to read lines from " + myFile); } // to write output in a file System.out.flush(); } // all my files share } catch (TwitterException te) { // te.printStackTrace(); System.err.println("Failed to get friends' ids: " + te.getMessage()); System.exit(-1); } System.out.println("!!!! DONE !!!!"); // Close System.out for this thread which will // flush and close this thread. System.out.close(); }
From source file:friendsandfollowers.FilesThreaderFollowersIDsParser.java
public static void main(String[] args) throws ClassNotFoundException, SQLException, JSONException, FileNotFoundException, UnsupportedEncodingException { // Check how many arguments were passed in if ((args == null) || (args.length < 5)) { System.err.println("5 Parameters are required plus one optional " + "parameter to launch a Job."); System.err.println("First: String 'INPUT: DB or /input/path/'"); System.err.println("Second: String 'OUTPUT: /output/path/'"); System.err.println("Third: (int) Total Number Of Jobs"); System.err.println("Fourth: (int) This Job Number"); System.err.println("Fifth: (int) Number of seconds to pause"); System.err.println("Sixth: (int) Number of ids to fetch" + "Provide number which increment by 5000 " + "(5000, 10000, 15000 etc) " + "or -1 to fetch all ids."); System.err.println("Example: fileToRun /input/path/ " + "/output/path/ 10 1 3 75000"); System.exit(-1);//from w w w .j a v a2s . c om } // TODO documentation for command line AppOAuth AppOAuths = new AppOAuth(); Misc helpers = new Misc(); String endpoint = "/followers/ids"; String inputPath = null; try { inputPath = StringEscapeUtils.escapeJava(args[0]); } catch (Exception e) { System.err.println("Argument " + args[0] + " must be an String."); System.exit(-1); } String outputPath = null; try { outputPath = StringEscapeUtils.escapeJava(args[1]); } catch (Exception e) { System.err.println("Argument " + args[1] + " must be an String."); System.exit(-1); } int TOTAL_JOBS = 0; try { TOTAL_JOBS = Integer.parseInt(args[2]); } catch (NumberFormatException e) { System.err.println("Argument " + args[2] + " must be an integer."); System.exit(1); } int JOB_NO = 0; try { JOB_NO = Integer.parseInt(args[3]); } catch (NumberFormatException e) { System.err.println("Argument " + args[3] + " must be an integer."); System.exit(1); } int secondsToPause = 0; try { secondsToPause = Integer.parseInt(args[4]); } catch (NumberFormatException e) { System.err.println("Argument" + args[4] + " must be an integer."); System.exit(-1); } int IDS_TO_FETCH_INT = -1; if (args.length == 6) { try { IDS_TO_FETCH_INT = Integer.parseInt(args[5]); } catch (NumberFormatException e) { System.err.println("Argument" + args[5] + " must be an integer."); System.exit(-1); } } int IDS_TO_FETCH = 0; if (IDS_TO_FETCH_INT > 5000) { float IDS_TO_FETCH_F = (float) IDS_TO_FETCH_INT / 5000; IDS_TO_FETCH = (int) Math.ceil(IDS_TO_FETCH_F); } else if ((IDS_TO_FETCH_INT <= 5000) && (IDS_TO_FETCH_INT > 0)) { IDS_TO_FETCH = 1; } secondsToPause = (TOTAL_JOBS * secondsToPause) - (JOB_NO * secondsToPause); System.out.println("secondsToPause: " + secondsToPause); helpers.pause(secondsToPause); try { int TotalWorkLoad = 0; ArrayList<String> allFiles = null; try { final File folder = new File(inputPath); allFiles = helpers.listFilesForSingleFolder(folder); TotalWorkLoad = allFiles.size(); } catch (Exception e) { System.err.println("Input folder is not exists: " + e.getMessage()); System.exit(-1); } System.out.println("Total Workload is: " + TotalWorkLoad); if (TotalWorkLoad < 1) { System.err.println("No screen names file exists in: " + inputPath); System.exit(-1); } if (TOTAL_JOBS > TotalWorkLoad) { System.err.println("Number of jobs are more than total work" + " load. Please reduce 'Number of jobs' to launch."); System.exit(-1); } float TotalWorkLoadf = TotalWorkLoad; float TOTAL_JOBSf = TOTAL_JOBS; float res = (TotalWorkLoadf / TOTAL_JOBSf); int chunkSize = (int) Math.ceil(res); int offSet = JOB_NO * chunkSize; int chunkSizeToGet = (JOB_NO + 1) * chunkSize; System.out.println("My Share is " + chunkSize); System.out.println(); // Load OAuh User TwitterFactory tf = AppOAuths.loadOAuthUser(endpoint, TOTAL_JOBS, JOB_NO); Twitter twitter = tf.getInstance(); int RemainingCalls = AppOAuths.RemainingCalls; int RemainingCallsCounter = 0; System.out.println("First Time OAuth Remianing Calls: " + RemainingCalls); String Screen_name = AppOAuths.screen_name; System.out.println("First Time Loaded OAuth Screen_name: " + Screen_name); System.out.println(); IDs ids; System.out.println("Going to get followers ids."); // to write output in a file System.out.flush(); if (JOB_NO + 1 == TOTAL_JOBS) { chunkSizeToGet = TotalWorkLoad; } List<String> myFilesShare = allFiles.subList(offSet, chunkSizeToGet); for (String myFile : myFilesShare) { System.out.println("Going to parse file: " + myFile); try (BufferedReader br = new BufferedReader(new FileReader(inputPath + "/" + myFile))) { String line; OUTERMOST: while ((line = br.readLine()) != null) { // process the line. System.out.println("Going to get followers ids of Screen-name / user_id: " + line); System.out.println(); String targetedUser = line.trim(); // tmp long cursor = -1; int idsLoopCounter = 0; int totalIDs = 0; PrintWriter writer = new PrintWriter(outputPath + "/" + targetedUser, "UTF-8"); // call different functions for screen_name and id_str Boolean chckedNumaric = helpers.isNumeric(targetedUser); do { ids = null; try { if (chckedNumaric) { long LongValueTargetedUser = Long.valueOf(targetedUser).longValue(); ids = twitter.getFollowersIDs(LongValueTargetedUser, cursor); } else { ids = twitter.getFollowersIDs(targetedUser, cursor); } } catch (TwitterException te) { // do not throw if user has protected tweets, or // if they deleted their account if (te.getStatusCode() == HttpResponseCode.UNAUTHORIZED || te.getStatusCode() == HttpResponseCode.NOT_FOUND) { System.out.println(targetedUser + " is protected or account is deleted"); } else { System.out.println("Followers Get Exception: " + te.getMessage()); } // If rate limit reached then switch Auth user RemainingCallsCounter++; if (RemainingCallsCounter >= RemainingCalls) { // load auth user tf = AppOAuths.loadOAuthUser(endpoint, TOTAL_JOBS, JOB_NO); twitter = tf.getInstance(); System.out.println( "New Loaded OAuth User " + " Screen_name: " + AppOAuths.screen_name); RemainingCalls = AppOAuths.RemainingCalls; RemainingCallsCounter = 0; System.out.println("New OAuth Remianing Calls: " + RemainingCalls); } // Remove file if ids not found if (totalIDs == 0) { System.out.println("No ids fetched so removing " + "file " + targetedUser); File fileToDelete = new File(outputPath + "/" + targetedUser); fileToDelete.delete(); } System.out.println(); // If error then switch to next user continue OUTERMOST; } if (ids.getIDs().length > 0) { idsLoopCounter++; totalIDs += ids.getIDs().length; System.out.println(idsLoopCounter + ": IDS length: " + ids.getIDs().length); JSONObject responseDetailsJson = new JSONObject(); JSONArray jsonArray = new JSONArray(); for (long id : ids.getIDs()) { jsonArray.put(id); } Object idsJSON = responseDetailsJson.put("ids", jsonArray); writer.println(idsJSON); } // If rate limit reached then switch Auth user RemainingCallsCounter++; if (RemainingCallsCounter >= RemainingCalls) { // load auth user tf = AppOAuths.loadOAuthUser(endpoint, TOTAL_JOBS, JOB_NO); twitter = tf.getInstance(); System.out.println("New Loaded OAuth User Screen_name: " + AppOAuths.screen_name); RemainingCalls = AppOAuths.RemainingCalls; RemainingCallsCounter = 0; System.out.println("New OAuth Remianing Calls: " + RemainingCalls); } if (IDS_TO_FETCH_INT != -1) { if (idsLoopCounter == IDS_TO_FETCH) { break; } } } while ((cursor = ids.getNextCursor()) != 0); writer.close(); System.out.println("Total ids dumped of " + targetedUser + " are: " + totalIDs); // Remove file if ids not found if (totalIDs == 0) { System.out.println("No ids fetched so removing " + "file " + targetedUser); File fileToDelete = new File(outputPath + "/" + targetedUser); fileToDelete.delete(); } System.out.println(); } // while get records from single file } // read my single file catch (IOException e) { System.err.println("Failed to read lines from " + myFile); } // to write output in a file System.out.flush(); } // all my files share } catch (TwitterException te) { // te.printStackTrace(); System.err.println("Failed to get followers' ids: " + te.getMessage()); System.exit(-1); } System.out.println("!!!! DONE !!!!"); // Close System.out for this thread which will // flush and close this thread. System.out.close(); }