List of usage examples for java.io PrintStream close
public void close()
From source file:cc.twittertools.index.ExtractTweetidsFromIndex.java
@SuppressWarnings("static-access") public static void main(String[] args) throws Exception { Options options = new Options(); options.addOption(// ww w.j av a 2 s. c om OptionBuilder.withArgName("dir").hasArg().withDescription("index location").create(INDEX_OPTION)); CommandLine cmdline = null; CommandLineParser parser = new GnuParser(); try { cmdline = parser.parse(options, args); } catch (ParseException exp) { System.err.println("Error parsing command line: " + exp.getMessage()); System.exit(-1); } if (!cmdline.hasOption(INDEX_OPTION)) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(ExtractTweetidsFromIndex.class.getName(), options); System.exit(-1); } File indexLocation = new File(cmdline.getOptionValue(INDEX_OPTION)); if (!indexLocation.exists()) { System.err.println("Error: " + indexLocation + " does not exist!"); System.exit(-1); } IndexReader reader = DirectoryReader.open(FSDirectory.open(indexLocation)); PrintStream out = new PrintStream(System.out, true, "UTF-8"); for (int i = 0; i < reader.maxDoc(); i++) { Document doc = reader.document(i); out.println(doc.getField(StatusField.ID.name).stringValue() + "\t" + doc.getField(StatusField.SCREEN_NAME.name).stringValue()); } out.close(); reader.close(); }
From source file:at.tuwien.ifs.somtoolbox.apps.helper.VectorFileLabelLister.java
public static void main(String[] args) throws IOException { // register and parse all options JSAPResult config = OptionFactory.parseResults(args, OPTIONS); String inputVectorFileName = AbstractOptionFactory.getFilePath(config, "input"); String outputFileName = AbstractOptionFactory.getFilePath(config, "output"); PrintStream out; if (StringUtils.isBlank(outputFileName)) { out = System.out;/* ww w. ja va2s . co m*/ } else { out = new PrintStream(outputFileName); } InputData data = InputDataFactory.open(inputVectorFileName); String[] labels = data.getLabels(); Arrays.sort(labels); for (String s : labels) { out.println(s); } out.close(); }
From source file:AnotherBeerServer.java
public static void main(String args[]) throws Exception { ServerSocket ssock = new ServerSocket(1234); System.out.println("Listening"); Socket sock = ssock.accept(); ssock.close(); // no more connects PrintStream ps = new PrintStream(sock.getOutputStream()); // ask for count ps.print("count? "); BufferedReader input = new BufferedReader(new InputStreamReader(sock.getInputStream())); // read and parse it String line = input.readLine(); ps.println(""); int count = Integer.parseInt(line); for (int i = count; i >= 0; i--) { ps.println(i + " Java Source and Support."); }/*from www .j a va 2 s . com*/ ps.close(); sock.close(); }
From source file:cc.twittertools.util.ExtractSubcollection.java
@SuppressWarnings("static-access") public static void main(String[] args) throws Exception { Options options = new Options(); options.addOption(OptionBuilder.withArgName("dir").hasArg().withDescription("source collection directory") .create(COLLECTION_OPTION)); options.addOption(//from w w w . j a va 2 s .c om OptionBuilder.withArgName("file").hasArg().withDescription("list of tweetids").create(ID_OPTION)); CommandLine cmdline = null; CommandLineParser parser = new GnuParser(); try { cmdline = parser.parse(options, args); } catch (ParseException exp) { System.err.println("Error parsing command line: " + exp.getMessage()); System.exit(-1); } if (!cmdline.hasOption(COLLECTION_OPTION) || !cmdline.hasOption(ID_OPTION)) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(ExtractSubcollection.class.getName(), options); System.exit(-1); } String collectionPath = cmdline.getOptionValue(COLLECTION_OPTION); LongOpenHashSet tweetids = new LongOpenHashSet(); File tweetidsFile = new File(cmdline.getOptionValue(ID_OPTION)); if (!tweetidsFile.exists()) { System.err.println("Error: " + tweetidsFile + " does not exist!"); System.exit(-1); } LOG.info("Reading tweetids from " + tweetidsFile); FileInputStream fin = new FileInputStream(tweetidsFile); BufferedReader br = new BufferedReader(new InputStreamReader(fin)); String s; while ((s = br.readLine()) != null) { tweetids.add(Long.parseLong(s)); } br.close(); fin.close(); LOG.info("Read " + tweetids.size() + " tweetids."); File file = new File(collectionPath); if (!file.exists()) { System.err.println("Error: " + file + " does not exist!"); System.exit(-1); } PrintStream out = new PrintStream(System.out, true, "UTF-8"); StatusStream stream = new JsonStatusCorpusReader(file); Status status; while ((status = stream.next()) != null) { if (tweetids.contains(status.getId())) { out.println(status.getJsonObject().toString()); } } stream.close(); out.close(); }
From source file:edu.msu.cme.rdp.taxatree.TreeBuilder.java
public static void main(String[] args) throws IOException { if (args.length != 3) { System.err.println("USAGE: TreeBuilder <idmapping> <merges.bin> <newick_out>"); return;//from ww w . j a v a2s. c o m } IdMapping<Integer> idMapping = IdMapping.fromFile(new File(args[0])); DataInputStream mergeStream = new DataInputStream(new BufferedInputStream(new FileInputStream(args[1]))); TaxonHolder lastMerged = null; int taxid = 0; final Map<Integer, Double> distMap = new HashMap(); Map<Integer, TaxonHolder> taxonMap = new HashMap(); try { while (true) { if (mergeStream.readBoolean()) { // Singleton int cid = mergeStream.readInt(); int intId = mergeStream.readInt(); TaxonHolder<Taxon> holder; List<String> seqids = idMapping.getIds(intId); if (seqids.size() == 1) { holder = new TaxonHolder(new Taxon(taxid++, seqids.get(0), "")); } else { holder = new TaxonHolder(new Taxon(taxid++, "", "")); for (String seqid : seqids) { int id = taxid++; distMap.put(id, 0.0); TaxonHolder th = new TaxonHolder(new Taxon(id, seqid, "")); th.setParent(holder); holder.addChild(th); } } lastMerged = holder; taxonMap.put(cid, holder); } else { int ci = mergeStream.readInt(); int cj = mergeStream.readInt(); int ck = mergeStream.readInt(); double dist = (double) mergeStream.readInt() / DistanceCalculator.MULTIPLIER; TaxonHolder holder = new TaxonHolder(new Taxon(taxid++, "", "")); taxonMap.put(ck, holder); holder.addChild(taxonMap.get(ci)); taxonMap.get(ci).setParent(holder); distMap.put(ci, dist); holder.addChild(taxonMap.get(cj)); taxonMap.get(cj).setParent(holder); distMap.put(cj, dist); lastMerged = holder; } } } catch (EOFException e) { } if (lastMerged == null) { throw new IOException("No merges in file"); } PrintStream newickTreeOut = new PrintStream(new File(args[2])); NewickPrintVisitor visitor = new NewickPrintVisitor(newickTreeOut, false, new NewickDistanceFactory() { public float getDistance(int i) { return distMap.get(i).floatValue(); } }); lastMerged.biDirectionDepthFirst(visitor); newickTreeOut.close(); }
From source file:cc.wikitools.lucene.ScoreWikipediaArticle.java
@SuppressWarnings("static-access") public static void main(String[] args) throws Exception { Options options = new Options(); options.addOption(/*from ww w .java2s . c om*/ OptionBuilder.withArgName("path").hasArg().withDescription("index location").create(INDEX_OPTION)); options.addOption( OptionBuilder.withArgName("num").hasArg().withDescription("article id").create(ID_OPTION)); options.addOption( OptionBuilder.withArgName("string").hasArg().withDescription("article title").create(TITLE_OPTION)); options.addOption( OptionBuilder.withArgName("string").hasArg().withDescription("query text").create(QUERY_OPTION)); CommandLine cmdline = null; CommandLineParser parser = new GnuParser(); try { cmdline = parser.parse(options, args); } catch (ParseException exp) { System.err.println("Error parsing command line: " + exp.getMessage()); System.exit(-1); } if (!(cmdline.hasOption(ID_OPTION) || cmdline.hasOption(TITLE_OPTION)) || !cmdline.hasOption(INDEX_OPTION) || !cmdline.hasOption(QUERY_OPTION)) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(ScoreWikipediaArticle.class.getName(), options); System.exit(-1); } File indexLocation = new File(cmdline.getOptionValue(INDEX_OPTION)); if (!indexLocation.exists()) { System.err.println("Error: " + indexLocation + " does not exist!"); System.exit(-1); } String queryText = cmdline.getOptionValue(QUERY_OPTION); WikipediaSearcher searcher = new WikipediaSearcher(indexLocation); PrintStream out = new PrintStream(System.out, true, "UTF-8"); if (cmdline.hasOption(ID_OPTION)) { out.println("score: " + searcher.scoreArticle(queryText, Integer.parseInt(cmdline.getOptionValue(ID_OPTION)))); } else { out.println("score: " + searcher.scoreArticle(queryText, cmdline.getOptionValue(TITLE_OPTION))); } searcher.close(); out.close(); }
From source file:cc.twittertools.search.api.SearchStatusesThrift.java
@SuppressWarnings("static-access") public static void main(String[] args) throws Exception { Options options = new Options(); options.addOption(new Option(HELP_OPTION, "show help")); options.addOption(OptionBuilder.withArgName("string").hasArg().withDescription("host").create(HOST_OPTION)); options.addOption(OptionBuilder.withArgName("port").hasArg().withDescription("port").create(PORT_OPTION)); options.addOption(// w w w. java 2s .c o m OptionBuilder.withArgName("string").hasArg().withDescription("query id").create(QID_OPTION)); options.addOption( OptionBuilder.withArgName("string").hasArg().withDescription("query text").create(QUERY_OPTION)); options.addOption( OptionBuilder.withArgName("string").hasArg().withDescription("runtag").create(RUNTAG_OPTION)); options.addOption(OptionBuilder.withArgName("num").hasArg().withDescription("maxid").create(MAX_ID_OPTION)); options.addOption(OptionBuilder.withArgName("num").hasArg().withDescription("number of results to return") .create(NUM_RESULTS_OPTION)); options.addOption( OptionBuilder.withArgName("string").hasArg().withDescription("group id").create(GROUP_OPTION)); options.addOption( OptionBuilder.withArgName("string").hasArg().withDescription("access token").create(TOKEN_OPTION)); options.addOption(new Option(VERBOSE_OPTION, "print out complete document")); CommandLine cmdline = null; CommandLineParser parser = new GnuParser(); try { cmdline = parser.parse(options, args); } catch (ParseException exp) { System.err.println("Error parsing command line: " + exp.getMessage()); System.exit(-1); } if (cmdline.hasOption(HELP_OPTION) || !cmdline.hasOption(HOST_OPTION) || !cmdline.hasOption(PORT_OPTION)) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(SearchStatusesThrift.class.getName(), options); System.exit(-1); } String qid = cmdline.hasOption(QID_OPTION) ? cmdline.getOptionValue(QID_OPTION) : DEFAULT_QID; String query = cmdline.hasOption(QUERY_OPTION) ? cmdline.getOptionValue(QUERY_OPTION) : DEFAULT_Q; String runtag = cmdline.hasOption(RUNTAG_OPTION) ? cmdline.getOptionValue(RUNTAG_OPTION) : DEFAULT_RUNTAG; long maxId = cmdline.hasOption(MAX_ID_OPTION) ? Long.parseLong(cmdline.getOptionValue(MAX_ID_OPTION)) : DEFAULT_MAX_ID; int numResults = cmdline.hasOption(NUM_RESULTS_OPTION) ? Integer.parseInt(cmdline.getOptionValue(NUM_RESULTS_OPTION)) : DEFAULT_NUM_RESULTS; boolean verbose = cmdline.hasOption(VERBOSE_OPTION); String group = cmdline.hasOption(GROUP_OPTION) ? cmdline.getOptionValue(GROUP_OPTION) : null; String token = cmdline.hasOption(TOKEN_OPTION) ? cmdline.getOptionValue(TOKEN_OPTION) : null; TrecSearchThriftClient client = new TrecSearchThriftClient(cmdline.getOptionValue(HOST_OPTION), Integer.parseInt(cmdline.getOptionValue(PORT_OPTION)), group, token); System.err.println("qid: " + qid); System.err.println("q: " + query); System.err.println("max_id: " + maxId); System.err.println("num_results: " + numResults); PrintStream out = new PrintStream(System.out, true, "UTF-8"); List<TResult> results = client.search(query, maxId, numResults); int i = 1; for (TResult result : results) { out.println(String.format("%s Q0 %d %d %f %s", qid, result.id, i, result.rsv, runtag)); if (verbose) { System.out.println("# " + result.toString().replaceAll("[\\n\\r]+", " ")); } i++; } out.close(); }
From source file:com.act.lcms.ExtractFromNetCDFAroundMass.java
public static void main(String[] args) throws Exception { if (args.length != 4 || !args[0].endsWith(".nc")) { throw new RuntimeException( "Needs (1) NetCDF .nc file, " + "(2) mass value, e.g., 132.0772 for debugging, " + "(3) how many timepoints to process (-1 for all), " + "(4) prefix for .data and rendered .pdf, '-' for stdout"); }/*from www . ja v a2 s .c om*/ String netCDF = args[0]; Double mz = Double.parseDouble(args[1]); Integer numSpectraToProcess = Integer.parseInt(args[2]); String outPrefix = args[3]; String outPDF = outPrefix.equals("-") ? null : outPrefix + ".pdf"; String outDATA = outPrefix.equals("-") ? null : outPrefix + ".data"; ExtractFromNetCDFAroundMass e = new ExtractFromNetCDFAroundMass(); List<Triple<Double, Double, Double>> window = e.get2DWindow(netCDF, mz, numSpectraToProcess); // Write data output to outfile PrintStream whereTo = outDATA == null ? System.out : new PrintStream(new FileOutputStream(outDATA)); for (Triple<Double, Double, Double> xyz : window) { whereTo.format("%.4f\t%.4f\t%.4f\n", xyz.getLeft(), xyz.getMiddle(), xyz.getRight()); whereTo.flush(); } if (outDATA != null) { // if outDATA is != null, then we have written to .data file // now render the .data to the corresponding .pdf file // first close the .data whereTo.close(); // render outDATA to outPDF using gnuplo Gnuplotter plotter = new Gnuplotter(); plotter.plot3D(outDATA, outPDF, netCDF, mz); } }
From source file:MainClass.java
public static void main(String args[]) throws Exception { SSLServerSocketFactory ssf = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault(); SSLServerSocket ss = (SSLServerSocket) ssf.createServerSocket(443); ss.setNeedClientAuth(true);// ww w .j a va2 s . com while (true) { Socket s = ss.accept(); SSLSession session = ((SSLSocket) s).getSession(); Certificate[] cchain = session.getPeerCertificates(); for (int j = 0; j < cchain.length; j++) { System.out.println(((X509Certificate) cchain[j]).getSubjectDN()); } PrintStream out = new PrintStream(s.getOutputStream()); BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); String info = null; while ((info = in.readLine()) != null) { System.out.println("now got " + info); if (info.equals("")) break; } out.println("HTTP/1.0 200 OK\nMIME_version:1.0"); out.println("Content_Type:text/html"); String c = "<html> <head></head><body> <h1> Hi,</h1></Body></html>"; out.println("Content_Length:" + c.length()); out.println(""); out.println(c); out.close(); s.close(); in.close(); } }
From source file:MainClass.java
public static void main(String args[]) throws Exception { ServerSocket ss = new ServerSocket(80); while (true) { Socket s = ss.accept();//from w w w . j a v a2 s.c om PrintStream out = new PrintStream(s.getOutputStream()); BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); String info = null; while ((info = in.readLine()) != null) { System.out.println("now got " + info); if (info.equals("")) break; } out.println("HTTP/1.0 200 OK"); out.println("MIME_version:1.0"); out.println("Content_Type:text/html"); String c = "<html> <head></head><body> <h1> hi</h1></Body></html>"; out.println("Content_Length:" + c.length()); out.println(""); out.println(c); out.close(); s.close(); in.close(); } }