List of usage examples for java.io OutputStreamWriter OutputStreamWriter
public OutputStreamWriter(OutputStream out)
From source file:edu.cmu.lti.oaqa.knn4qa.apps.ExtractDataAndQueryAsSparseVectors.java
public static void main(String[] args) { String optKeys[] = { CommonParams.MAX_NUM_QUERY_PARAM, MAX_NUM_DATA_PARAM, CommonParams.MEMINDEX_PARAM, IN_QUERIES_PARAM, OUT_QUERIES_PARAM, OUT_DATA_PARAM, TEXT_FIELD_PARAM, TEST_QTY_PARAM, }; String optDescs[] = { CommonParams.MAX_NUM_QUERY_DESC, MAX_NUM_DATA_DESC, CommonParams.MEMINDEX_DESC, IN_QUERIES_DESC, OUT_QUERIES_DESC, OUT_DATA_DESC, TEXT_FIELD_DESC, TEST_QTY_DESC }; boolean hasArg[] = { true, true, true, true, true, true, true, true }; ParamHelper prmHlp = null;//w w w .j a v a 2 s . c o m try { prmHlp = new ParamHelper(args, optKeys, optDescs, hasArg); CommandLine cmd = prmHlp.getCommandLine(); Options opt = prmHlp.getOptions(); int maxNumQuery = Integer.MAX_VALUE; String tmpn = cmd.getOptionValue(CommonParams.MAX_NUM_QUERY_PARAM); if (tmpn != null) { try { maxNumQuery = Integer.parseInt(tmpn); } catch (NumberFormatException e) { UsageSpecify(CommonParams.MAX_NUM_QUERY_PARAM, opt); } } int maxNumData = Integer.MAX_VALUE; tmpn = cmd.getOptionValue(MAX_NUM_DATA_PARAM); if (tmpn != null) { try { maxNumData = Integer.parseInt(tmpn); } catch (NumberFormatException e) { UsageSpecify(MAX_NUM_DATA_PARAM, opt); } } String memIndexPref = cmd.getOptionValue(CommonParams.MEMINDEX_PARAM); if (null == memIndexPref) { UsageSpecify(CommonParams.MEMINDEX_PARAM, opt); } String textField = cmd.getOptionValue(TEXT_FIELD_PARAM); if (null == textField) { UsageSpecify(TEXT_FIELD_PARAM, opt); } textField = textField.toLowerCase(); int fieldId = -1; for (int i = 0; i < FeatureExtractor.mFieldNames.length; ++i) if (FeatureExtractor.mFieldNames[i].compareToIgnoreCase(textField) == 0) { fieldId = i; break; } if (-1 == fieldId) { Usage("Wrong field index, should be one of the following: " + String.join(",", FeatureExtractor.mFieldNames), opt); } InMemForwardIndex indx = new InMemForwardIndex( FeatureExtractor.indexFileName(memIndexPref, FeatureExtractor.mFieldNames[fieldId])); BM25SimilarityLucene bm25simil = new BM25SimilarityLucene(FeatureExtractor.BM25_K1, FeatureExtractor.BM25_B, indx); String inQueryFile = cmd.getOptionValue(IN_QUERIES_PARAM); String outQueryFile = cmd.getOptionValue(OUT_QUERIES_PARAM); if ((inQueryFile == null) != (outQueryFile == null)) { Usage("You should either specify both " + IN_QUERIES_PARAM + " and " + OUT_QUERIES_PARAM + " or none of them", opt); } String outDataFile = cmd.getOptionValue(OUT_DATA_PARAM); tmpn = cmd.getOptionValue(TEST_QTY_PARAM); int testQty = 0; if (tmpn != null) { try { testQty = Integer.parseInt(tmpn); } catch (NumberFormatException e) { UsageSpecify(TEST_QTY_PARAM, opt); } } ArrayList<DocEntry> testDocEntries = new ArrayList<DocEntry>(); ArrayList<DocEntry> testQueryEntries = new ArrayList<DocEntry>(); ArrayList<TrulySparseVector> testDocVectors = new ArrayList<TrulySparseVector>(); ArrayList<TrulySparseVector> testQueryVectors = new ArrayList<TrulySparseVector>(); if (outDataFile != null) { BufferedWriter out = new BufferedWriter( new OutputStreamWriter(CompressUtils.createOutputStream(outDataFile))); ArrayList<DocEntryExt> docEntries = indx.getDocEntries(); for (int id = 0; id < Math.min(maxNumData, docEntries.size()); ++id) { DocEntry e = docEntries.get(id).mDocEntry; TrulySparseVector v = bm25simil.getDocSparseVector(e, false); if (id < testQty) { testDocEntries.add(e); testDocVectors.add(v); } outputVector(out, v); } out.close(); } Splitter splitOnSpace = Splitter.on(' ').trimResults().omitEmptyStrings(); if (outQueryFile != null) { BufferedReader inpText = new BufferedReader( new InputStreamReader(CompressUtils.createInputStream(inQueryFile))); BufferedWriter out = new BufferedWriter( new OutputStreamWriter(CompressUtils.createOutputStream(outQueryFile))); String queryText = XmlHelper.readNextXMLIndexEntry(inpText); for (int queryQty = 0; queryText != null && queryQty < maxNumQuery; queryText = XmlHelper .readNextXMLIndexEntry(inpText), queryQty++) { Map<String, String> queryFields = null; // 1. Parse a query try { queryFields = XmlHelper.parseXMLIndexEntry(queryText); } catch (Exception e) { System.err.println("Parsing error, offending QUERY:\n" + queryText); throw new Exception("Parsing error."); } String fieldText = queryFields.get(FeatureExtractor.mFieldsSOLR[fieldId]); if (fieldText == null) { fieldText = ""; } ArrayList<String> tmpa = new ArrayList<String>(); for (String s : splitOnSpace.split(fieldText)) tmpa.add(s); DocEntry e = indx.createDocEntry(tmpa.toArray(new String[tmpa.size()])); TrulySparseVector v = bm25simil.getDocSparseVector(e, true); if (queryQty < testQty) { testQueryEntries.add(e); testQueryVectors.add(v); } outputVector(out, v); } out.close(); } int testedQty = 0, diffQty = 0; // Now let's do some testing for (int iq = 0; iq < testQueryEntries.size(); ++iq) { DocEntry queryEntry = testQueryEntries.get(iq); TrulySparseVector queryVector = testQueryVectors.get(iq); for (int id = 0; id < testDocEntries.size(); ++id) { DocEntry docEntry = testDocEntries.get(id); TrulySparseVector docVector = testDocVectors.get(id); float val1 = bm25simil.compute(queryEntry, docEntry); float val2 = TrulySparseVector.scalarProduct(queryVector, docVector); ++testedQty; if (Math.abs(val1 - val2) > 1e5) { System.err.println( String.format("Potential mismatch BM25=%f <-> scalar product=%f", val1, val2)); ++diffQty; } } } if (testedQty > 0) System.out.println(String.format("Tested %d Mismatched %d", testedQty, diffQty)); } catch (ParseException e) { Usage("Cannot parse arguments: " + e, prmHlp != null ? prmHlp.getOptions() : null); e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); System.err.println("Terminating due to an exception: " + e); System.exit(1); } }
From source file:edu.usc.qufd.Main.java
/** * The main method.//from w ww . jav a 2 s.c o m * * @param args the arguments * @throws IOException Signals that an I/O exception has occurred. */ public static void main(String[] args) throws IOException { if (parseInputs(args) == false) { System.exit(-1); //The input files do not exist } /* * Parsing inputs: fabric & qasm file */ PrintWriter outputFile; RandomAccessFile raf = null; String latencyPlaceHolder; if (RuntimeConfig.OUTPUT_TO_FILE) { latencyPlaceHolder = "Total Latency: " + Long.MAX_VALUE + " us" + System.lineSeparator(); raf = new RandomAccessFile(outputFileAddr, "rws"); //removing the old values in the file raf.setLength(0); //writing a place holder for the total latency raf.writeBytes(latencyPlaceHolder); raf.close(); outputFile = new PrintWriter(new BufferedWriter(new FileWriter(outputFileAddr, true)), true); } else { //writing to stdout outputFile = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)), true); } /* parsing the input*/ layout = LayoutParser.parse(pmdFileAddr); qasm = QASMParser.QASMParser(qasmFileAddr, layout); long totalLatency = qufd(outputFile); if (RuntimeConfig.OUTPUT_TO_FILE) { outputFile.close(); //Over writing the place holder with the actual latency String latencyActual = "Total Latency: " + totalLatency + " " + layout.getTimeUnit(); latencyActual = StringUtils.rightPad(latencyActual, latencyPlaceHolder.length() - System.lineSeparator().length()); raf = new RandomAccessFile(outputFileAddr, "rws"); //Writing to the top of a file raf.seek(0); //writing the actual total latency in the at the top of the output file raf.writeBytes(latencyActual + System.lineSeparator()); raf.close(); } else { outputFile.flush(); System.out.println("Total Latency: " + totalLatency + " " + layout.getTimeUnit()); } if (RuntimeConfig.VERBOSE) { System.out.println("Done."); } outputFile.close(); }
From source file:act.installer.pubchem.PubchemSynonymFinder.java
public static void main(String[] args) throws Exception { org.apache.commons.cli.Options opts = new org.apache.commons.cli.Options(); for (Option.Builder b : OPTION_BUILDERS) { opts.addOption(b.build());//w w w . jav a 2s .com } CommandLine cl = null; try { CommandLineParser parser = new DefaultParser(); cl = parser.parse(opts, args); } catch (ParseException e) { System.err.format("Argument parsing failed: %s\n", e.getMessage()); HELP_FORMATTER.printHelp(PubchemSynonymFinder.class.getCanonicalName(), HELP_MESSAGE, opts, null, true); System.exit(1); } if (cl.hasOption("help")) { HELP_FORMATTER.printHelp(PubchemSynonymFinder.class.getCanonicalName(), HELP_MESSAGE, opts, null, true); return; } File rocksDBFile = new File(cl.getOptionValue(OPTION_INDEX_PATH)); if (!rocksDBFile.isDirectory()) { System.err.format("Index directory does not exist or is not a directory at '%s'", rocksDBFile.getAbsolutePath()); HELP_FORMATTER.printHelp(PubchemSynonymFinder.class.getCanonicalName(), HELP_MESSAGE, opts, null, true); System.exit(1); } List<String> compoundIds = null; if (cl.hasOption(OPTION_PUBCHEM_COMPOUND_ID)) { compoundIds = Collections.singletonList(cl.getOptionValue(OPTION_PUBCHEM_COMPOUND_ID)); } else if (cl.hasOption(OPTION_IDS_FILE)) { File idsFile = new File(cl.getOptionValue(OPTION_IDS_FILE)); if (!idsFile.exists()) { System.err.format("Cannot find Pubchem CIDs file at %s", idsFile.getAbsolutePath()); HELP_FORMATTER.printHelp(PubchemSynonymFinder.class.getCanonicalName(), HELP_MESSAGE, opts, null, true); System.exit(1); } compoundIds = getCIDsFromFile(idsFile); if (compoundIds.size() == 0) { System.err.format("Found zero Pubchem CIDs to process in file at '%s', exiting", idsFile.getAbsolutePath()); HELP_FORMATTER.printHelp(PubchemSynonymFinder.class.getCanonicalName(), HELP_MESSAGE, opts, null, true); System.exit(1); } } else { System.err.format("Must specify one of '%s' or '%s'; index is too big to print all synonyms.", OPTION_PUBCHEM_COMPOUND_ID, OPTION_IDS_FILE); HELP_FORMATTER.printHelp(PubchemSynonymFinder.class.getCanonicalName(), HELP_MESSAGE, opts, null, true); System.exit(1); } // Run a quick check to warn users of malformed ids. compoundIds.forEach(x -> { if (!PC_CID_PATTERN.matcher(x).matches()) { // Use matches() for complete matching. LOGGER.warn("Specified compound id does not match expected format: %s", x); } }); LOGGER.info("Opening DB and searching for %d Pubchem CIDs", compoundIds.size()); Pair<RocksDB, Map<PubchemTTLMerger.COLUMN_FAMILIES, ColumnFamilyHandle>> dbAndHandles = null; Map<String, PubchemSynonyms> results = new LinkedHashMap<>(compoundIds.size()); try { dbAndHandles = PubchemTTLMerger.openExistingRocksDB(rocksDBFile); RocksDB db = dbAndHandles.getLeft(); ColumnFamilyHandle cidToSynonymsCfh = dbAndHandles.getRight() .get(PubchemTTLMerger.COLUMN_FAMILIES.CID_TO_SYNONYMS); for (String cid : compoundIds) { PubchemSynonyms synonyms = null; byte[] val = db.get(cidToSynonymsCfh, cid.getBytes(UTF8)); if (val != null) { ObjectInputStream oi = new ObjectInputStream(new ByteArrayInputStream(val)); // We're relying on our use of a one-value-type per index model here so we can skip the instanceof check. synonyms = (PubchemSynonyms) oi.readObject(); } else { LOGGER.warn("No synonyms available for compound id '%s'", cid); } results.put(cid, synonyms); } } finally { if (dbAndHandles != null) { dbAndHandles.getLeft().close(); } } try (OutputStream outputStream = cl.hasOption(OPTION_OUTPUT) ? new FileOutputStream(cl.getOptionValue(OPTION_OUTPUT)) : System.out) { OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValue(outputStream, results); new OutputStreamWriter(outputStream).append('\n'); } LOGGER.info("Done searching for Pubchem synonyms"); }
From source file:Main.java
public static void reboot() { try {/*from w w w . ja va 2 s . co m*/ Process su = Runtime.getRuntime().exec("su"); OutputStreamWriter out = new OutputStreamWriter(su.getOutputStream()); out.write("reboot"); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:Main.java
public static void wirteStringToStream(OutputStream out, String src) throws IOException { final OutputStreamWriter writer = new OutputStreamWriter(out); writer.write(src);// w w w .j a v a 2s . c o m writer.flush(); }
From source file:Main.java
public static void writeToFile(String path, String data) { try {//from w w w . j ava2 s . com OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(path)); outputStreamWriter.write(data); outputStreamWriter.close(); } catch (IOException e) { Log.e("Exception", "File write failed: " + e.toString()); } }
From source file:Main.java
public static Process execute(String command) throws Exception { final Process process = new ProcessBuilder("sh").redirectErrorStream(true).start(); BufferedWriter stdOutput = new BufferedWriter(new OutputStreamWriter(process.getOutputStream())); stdOutput.write(command + "; exit\n"); stdOutput.flush();//from w ww . j a v a2s .c o m stdOutput.close(); return process; }
From source file:Main.java
public static boolean isRooted() { Process p;/* w w w. j av a 2 s .com*/ try { p = new ProcessBuilder("su").start(); BufferedWriter stdin = new BufferedWriter(new OutputStreamWriter(p.getOutputStream())); BufferedReader stdout = new BufferedReader(new InputStreamReader(p.getInputStream())); stdin.write("whoami"); stdin.newLine(); stdin.write("exit"); stdin.newLine(); stdin.close(); try { p.waitFor(); if (!stdout.ready()) return false; String user = stdout.readLine(); //We only expect one line of output stdout.close(); if (user == "root") { return true; } else { return false; } } catch (InterruptedException e) { e.printStackTrace(); return false; } } catch (IOException e) { e.printStackTrace(); return false; } }
From source file:com.jgaap.backend.CLI.java
/** * Parses the arguments passed to jgaap from the command line. Will either * display the help or run jgaap on an experiment. * /* w w w . j a v a2 s. c o m*/ * @param args * command line arguments * @throws Exception */ public static void main(String[] args) throws Exception { CommandLineParser parser = new GnuParser(); CommandLine cmd = parser.parse(options, args); if (cmd.hasOption('h')) { String command = cmd.getOptionValue('h'); if (command == null) { HelpFormatter helpFormatter = new HelpFormatter(); helpFormatter.setLeftPadding(5); helpFormatter.setWidth(100); helpFormatter.printHelp( "jgaap -c [canon canon ...] -es [event] -ec [culler culler ...] -a [analysis] <-d [distance]> -l [file] <-s [file]>", "Welcome to JGAAP the Java Graphical Authorship Attribution Program.\nMore information can be found at http://jgaap.com", options, "Copyright 2013 Evaluating Variation in Language Lab, Duquesne University"); } else { List<Displayable> list = new ArrayList<Displayable>(); if (command.equalsIgnoreCase("c")) { list.addAll(Canonicizers.getCanonicizers()); } else if (command.equalsIgnoreCase("es")) { list.addAll(EventDrivers.getEventDrivers()); } else if (command.equalsIgnoreCase("ec")) { list.addAll(EventCullers.getEventCullers()); } else if (command.equalsIgnoreCase("a")) { list.addAll(AnalysisDrivers.getAnalysisDrivers()); } else if (command.equalsIgnoreCase("d")) { list.addAll(DistanceFunctions.getDistanceFunctions()); } else if (command.equalsIgnoreCase("lang")) { list.addAll(Languages.getLanguages()); } for (Displayable display : list) { if (display.showInGUI()) System.out.println(display.displayName() + " - " + display.tooltipText()); } if (list.isEmpty()) { System.out.println("Option " + command + " was not found."); System.out.println("Please use c, es, d, a, or lang"); } } } else if (cmd.hasOption('v')) { System.out.println("Java Graphical Authorship Attribution Program version 5.2.0"); } else if (cmd.hasOption("ee")) { String eeFile = cmd.getOptionValue("ee"); String lang = cmd.getOptionValue("lang"); ExperimentEngine.runExperiment(eeFile, lang); System.exit(0); } else { JGAAP.commandline = true; API api = API.getPrivateInstance(); String documentsFilePath = cmd.getOptionValue('l'); if (documentsFilePath == null) { throw new Exception("No Documents CSV specified"); } List<Document> documents; if (documentsFilePath.startsWith(JGAAPConstants.JGAAP_RESOURCE_PACKAGE)) { documents = Utils.getDocumentsFromCSV( CSVIO.readCSV(com.jgaap.JGAAP.class.getResourceAsStream(documentsFilePath))); } else { documents = Utils.getDocumentsFromCSV(CSVIO.readCSV(documentsFilePath)); } for (Document document : documents) { api.addDocument(document); } String language = cmd.getOptionValue("lang", "english"); api.setLanguage(language); String[] canonicizers = cmd.getOptionValues('c'); if (canonicizers != null) { for (String canonicizer : canonicizers) { api.addCanonicizer(canonicizer); } } String[] events = cmd.getOptionValues("es"); if (events == null) { throw new Exception("No EventDriver specified"); } for (String event : events) { api.addEventDriver(event); } String[] eventCullers = cmd.getOptionValues("ec"); if (eventCullers != null) { for (String eventCuller : eventCullers) { api.addEventCuller(eventCuller); } } String analysis = cmd.getOptionValue('a'); if (analysis == null) { throw new Exception("No AnalysisDriver specified"); } AnalysisDriver analysisDriver = api.addAnalysisDriver(analysis); String distanceFunction = cmd.getOptionValue('d'); if (distanceFunction != null) { api.addDistanceFunction(distanceFunction, analysisDriver); } api.execute(); List<Document> unknowns = api.getUnknownDocuments(); OutputStreamWriter outputStreamWriter; String saveFile = cmd.getOptionValue('s'); if (saveFile == null) { outputStreamWriter = new OutputStreamWriter(System.out); } else { outputStreamWriter = new OutputStreamWriter(new FileOutputStream(saveFile)); } Writer writer = new BufferedWriter(outputStreamWriter); for (Document unknown : unknowns) { writer.append(unknown.getFormattedResult(analysisDriver)); } writer.append('\n'); } }
From source file:Main.java
/** * Write the provided String, line by line, in the provided OutputStream. *///from ww w . ja va 2 s . co m private static void writeStream(String toWrite, OutputStream out) throws IOException { BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out)); BufferedReader reader = new BufferedReader(new StringReader(toWrite)); for (String line = reader.readLine(); line != null; line = reader.readLine()) { writer.write(line); } reader.close(); writer.close(); }