Example usage for java.io PrintStream close

List of usage examples for java.io PrintStream close

Introduction

In this page you can find the example usage for java.io PrintStream close.

Prototype

public void close() 

Source Link

Document

Closes the stream.

Usage

From source file:com.act.lcms.AnimateNetCDFAroundMass.java

public static void main(String[] args) throws Exception {
    if (args.length < 7 || !areNCFiles(Arrays.copyOfRange(args, 5, args.length))) {
        throw new RuntimeException(
                "Needs: \n" + "(1) mass value, e.g., 132.0772 \n" + "(2) time value, e.g., 39.2, (seconds), \n"
                        + "(3) minimum Mz Precision, 0.04 \n" + "(4) max z axis, e.g., 20000 \n"
                        + "(5) prefix for .data and rendered .pdf \n" + "(6..) 2 or more NetCDF .nc files");
    }/*  w  w  w  . ja v  a  2s.  c  om*/

    Double mz = Double.parseDouble(args[0]);
    Double time = Double.parseDouble(args[1]);
    Double minMzPrecision = Double.parseDouble(args[2]);
    Double maxZAxis = Double.parseDouble(args[3]);
    String outPrefix = args[4];

    // the mz values go from 50-950, we start with a big window and exponentially narrow down
    double mzWin = 100;
    // time values go from 0-450, we start with a big window and exponentially narrow down
    double timeWin = 50;

    // the factor by which to zoom in every step (has to be >1, a value of 2 is good)
    double factor = 1.2;

    // the animation frame count
    int frame = 1;

    AnimateNetCDFAroundMass c = new AnimateNetCDFAroundMass();
    String[] netCDFFnames = Arrays.copyOfRange(args, 5, args.length);
    List<List<XYZ>> spectra = c.getSpectra(netCDFFnames, time, timeWin, mz, mzWin);

    for (List<XYZ> s : spectra) {
        System.out.format("%d xyz datapoints in (initial narrowed) spectra\n", s.size());
    }

    String[] labels = new String[netCDFFnames.length];
    for (int i = 0; i < labels.length; i++)
        labels[i] = "Dataset: " + i;
    // you could set labels to netCDFFnames to get precise labels on the graphs

    Gnuplotter plotter = new Gnuplotter();
    String fmt = "png";

    List<String> outImgFiles = new ArrayList<>(), outDataFiles = new ArrayList<>();
    while (mzWin > minMzPrecision) {

        // exponentially narrow windows down
        mzWin /= factor;
        timeWin /= factor;

        List<List<XYZ>> windowedSpectra = c.getSpectraInWindowAll(spectra, time, timeWin, mz, mzWin);

        String frameid = String.format("%03d", frame);
        String outPDF = outPrefix + frameid + "." + fmt;
        String outDATA = outPrefix + frameid + ".data";
        outImgFiles.add(outPDF);
        outDataFiles.add(outDATA);
        frame++;

        // Write data output to outfile
        PrintStream out = new PrintStream(new FileOutputStream(outDATA));

        // print out the spectra to outDATA
        for (List<XYZ> windowOfSpectra : windowedSpectra) {
            for (XYZ xyz : windowOfSpectra) {
                out.format("%.4f\t%.4f\t%.4f\n", xyz.time, xyz.mz, xyz.intensity);
                out.flush();
            }
            // delimit this dataset from the rest
            out.print("\n\n");
        }

        // close the .data
        out.close();

        // render outDATA to outPDF using gnuplot
        plotter.plotMulti3D(outDATA, outPDF, fmt, labels, maxZAxis);
    }

    String outImgs = outPrefix + "*." + fmt;
    plotter.makeAnimatedGIF(outImgs, outPrefix + ".gif");
    // all the frames are now in the animated gif, remove the intermediate files
    for (String f : outDataFiles)
        new File(f).delete();
    for (String f : outImgFiles)
        new File(f).delete();
}

From source file:org.apache.jackrabbit.oak.scalability.ScalabilityRunner.java

public static void main(String[] args) throws Exception {
    OptionParser parser = new OptionParser();
    OptionSpec<File> base = parser.accepts("base", "Base directory").withRequiredArg().ofType(File.class)
            .defaultsTo(new File("target"));
    OptionSpec<String> host = parser.accepts("host", "MongoDB host").withRequiredArg().defaultsTo("localhost");
    OptionSpec<Integer> port = parser.accepts("port", "MongoDB port").withRequiredArg().ofType(Integer.class)
            .defaultsTo(27017);//from   w  ww  .  j  a va2 s  .  com
    OptionSpec<String> dbName = parser.accepts("db", "MongoDB database").withRequiredArg();
    OptionSpec<Boolean> dropDBAfterTest = parser
            .accepts("dropDBAfterTest", "Whether to drop the MongoDB database after the test").withOptionalArg()
            .ofType(Boolean.class).defaultsTo(true);
    OptionSpec<String> rdbjdbcuri = parser.accepts("rdbjdbcuri", "RDB JDBC URI").withOptionalArg()
            .defaultsTo("jdbc:h2:./target/benchmark");
    OptionSpec<String> rdbjdbcuser = parser.accepts("rdbjdbcuser", "RDB JDBC user").withOptionalArg()
            .defaultsTo("");
    OptionSpec<String> rdbjdbcpasswd = parser.accepts("rdbjdbcpasswd", "RDB JDBC password").withOptionalArg()
            .defaultsTo("");
    OptionSpec<String> rdbjdbctableprefix = parser.accepts("rdbjdbctableprefix", "RDB JDBC table prefix")
            .withOptionalArg().defaultsTo("");
    OptionSpec<Boolean> mmap = parser.accepts("mmap", "TarMK memory mapping").withOptionalArg()
            .ofType(Boolean.class).defaultsTo("64".equals(System.getProperty("sun.arch.data.model")));
    OptionSpec<Integer> cache = parser.accepts("cache", "cache size (MB)").withRequiredArg()
            .ofType(Integer.class).defaultsTo(100);
    OptionSpec<Integer> fdsCache = parser.accepts("blobCache", "cache size (MB)").withRequiredArg()
            .ofType(Integer.class).defaultsTo(32);
    OptionSpec<Boolean> withStorage = parser.accepts("storage", "Index storage enabled").withOptionalArg()
            .ofType(Boolean.class);
    OptionSpec<File> csvFile = parser.accepts("csvFile", "File to write a CSV version of the benchmark data.")
            .withOptionalArg().ofType(File.class);
    OptionSpec help = parser.acceptsAll(asList("h", "?", "help"), "show help").forHelp();
    OptionSpec<String> nonOption = parser.nonOptions();

    OptionSet options = parser.parse(args);

    if (options.has(help)) {
        parser.printHelpOn(System.out);
        System.exit(0);
    }

    int cacheSize = cache.value(options);
    RepositoryFixture[] allFixtures = new RepositoryFixture[] {
            new JackrabbitRepositoryFixture(base.value(options), cacheSize),
            OakRepositoryFixture.getMemoryNS(cacheSize * MB),
            OakRepositoryFixture.getMongo(host.value(options), port.value(options), dbName.value(options),
                    dropDBAfterTest.value(options), cacheSize * MB),
            OakRepositoryFixture.getMongoWithFDS(host.value(options), port.value(options),
                    dbName.value(options), dropDBAfterTest.value(options), cacheSize * MB, base.value(options),
                    fdsCache.value(options)),
            OakRepositoryFixture.getMongoNS(host.value(options), port.value(options), dbName.value(options),
                    dropDBAfterTest.value(options), cacheSize * MB),
            OakRepositoryFixture.getTar(base.value(options), 256, cacheSize, mmap.value(options)),
            OakRepositoryFixture.getTarWithBlobStore(base.value(options), 256, cacheSize, mmap.value(options)),
            OakRepositoryFixture.getSegmentTar(base.value(options), 256, cacheSize, mmap.value(options)),
            OakRepositoryFixture.getSegmentTarWithBlobStore(base.value(options), 256, cacheSize,
                    mmap.value(options)),
            OakRepositoryFixture.getRDB(rdbjdbcuri.value(options), rdbjdbcuser.value(options),
                    rdbjdbcpasswd.value(options), rdbjdbctableprefix.value(options),
                    dropDBAfterTest.value(options), cacheSize * MB),
            OakRepositoryFixture.getRDBWithFDS(rdbjdbcuri.value(options), rdbjdbcuser.value(options),
                    rdbjdbcpasswd.value(options), rdbjdbctableprefix.value(options),
                    dropDBAfterTest.value(options), cacheSize * MB, base.value(options),
                    fdsCache.value(options)) };
    ScalabilitySuite[] allSuites = new ScalabilitySuite[] {
            new ScalabilityBlobSearchSuite(withStorage.value(options)).addBenchmarks(new FullTextSearcher(),
                    new NodeTypeSearcher(), new FormatSearcher(), new FacetSearcher(),
                    new LastModifiedSearcher(Date.LAST_2_HRS), new LastModifiedSearcher(Date.LAST_24_HRS),
                    new LastModifiedSearcher(Date.LAST_7_DAYS), new LastModifiedSearcher(Date.LAST_MONTH),
                    new LastModifiedSearcher(Date.LAST_YEAR), new OrderByDate()),
            new ScalabilityNodeSuite(withStorage.value(options)).addBenchmarks(new OrderBySearcher(),
                    new SplitOrderBySearcher(), new OrderByOffsetPageSearcher(),
                    new SplitOrderByOffsetPageSearcher(), new OrderByKeysetPageSearcher(),
                    new SplitOrderByKeysetPageSearcher(), new MultiFilterOrderBySearcher(),
                    new MultiFilterSplitOrderBySearcher(), new MultiFilterOrderByOffsetPageSearcher(),
                    new MultiFilterSplitOrderByOffsetPageSearcher(), new MultiFilterOrderByKeysetPageSearcher(),
                    new MultiFilterSplitOrderByKeysetPageSearcher(), new ConcurrentReader(),
                    new ConcurrentWriter()),
            new ScalabilityNodeRelationshipSuite(withStorage.value(options))
                    .addBenchmarks(new AggregateNodeSearcher()) };

    Set<String> argset = Sets.newHashSet(nonOption.values(options));
    List<RepositoryFixture> fixtures = Lists.newArrayList();
    for (RepositoryFixture fixture : allFixtures) {
        if (argset.remove(fixture.toString())) {
            fixtures.add(fixture);
        }
    }

    Map<String, List<String>> argmap = Maps.newHashMap();
    // Split the args to get suites and benchmarks (i.e. suite:benchmark1,benchmark2)
    for (String arg : argset) {
        List<String> tokens = Splitter.on(":").limit(2).splitToList(arg);
        if (tokens.size() > 1) {
            argmap.put(tokens.get(0), Splitter.on(",").trimResults().splitToList(tokens.get(1)));
        } else {
            argmap.put(tokens.get(0), null);
        }
        argset.remove(arg);
    }

    if (argmap.isEmpty()) {
        System.err.println(
                "Warning: no scalability suites specified, " + "supported  are: " + Arrays.asList(allSuites));
    }

    List<ScalabilitySuite> suites = Lists.newArrayList();
    for (ScalabilitySuite suite : allSuites) {
        if (argmap.containsKey(suite.toString())) {
            List<String> benchmarks = argmap.get(suite.toString());
            // Only keep requested benchmarks
            if (benchmarks != null) {
                Iterator<String> iter = suite.getBenchmarks().keySet().iterator();
                for (; iter.hasNext();) {
                    String availBenchmark = iter.next();
                    if (!benchmarks.contains(availBenchmark)) {
                        iter.remove();
                    }
                }
            }
            suites.add(suite);
            argmap.remove(suite.toString());
        }
    }

    if (argmap.isEmpty()) {
        PrintStream out = null;
        if (options.has(csvFile)) {
            out = new PrintStream(FileUtils.openOutputStream(csvFile.value(options), true), false,
                    Charsets.UTF_8.name());
        }
        for (ScalabilitySuite suite : suites) {
            if (suite instanceof CSVResultGenerator) {
                ((CSVResultGenerator) suite).setPrintStream(out);
            }
            suite.run(fixtures);
        }
        if (out != null) {
            out.close();
        }
    } else {
        System.err.println("Unknown arguments: " + argset);
    }
}

From source file:com.linkedin.mlease.regression.liblinearfunc.LibLinear.java

/**
 * Command-line tool/*from w  w  w.ja v  a 2s.com*/
 * 
 * <pre>
 * java -cp target/regression-0.1-uber.jar com.linkedin.lab.regression.LibLinear
 * </pre>
 * 
 * @param args
 * @throws Exception
 */
public static void main(String[] args) throws Exception {

    String cmd = "Input parameters (separated by space): \n"
            + "   run:<command>      (required) train or predict\n"
            + "   ftype:<file_type>  (required) libsvm or json\n"
            + "   data:<file_name>   (required) Input data file of the specified type\n"
            + "   out:<file_name>    (required) Output file\n"
            + "   bias:<bias>        (optional) Set to 0 if you do not want to add an\n"
            + "                                 bias/intercept term\n"
            + "                                 Set to 1 if you want to add a feature with\n"
            + "                                 value 1 to every instance\n"
            + "                                 Default: 0\n"
            + "   param:<file_name>  (optional) for run:train, it specifies the prior mean\n"
            + "                      (required) for run:predict, it specifies the model\n"
            + "                                 File format: <featureName>=<value> per line\n"
            + "   priorVar:<var>     (required) for run:train, <var> is the a number\n"
            + "                      (not used) for run:predict\n"
            + "   init:<file_name>   (optional) for run:train, it specifies the initial value\n"
            + "                                 File format: <featureName>=<value> per line\n"
            + "   posteriorVar:1/0   (optional) Whether to compute posterior variances\n"
            + "                                 Default: 1\n"
            + "   posteriorCov:1/0   (optional) Whether to compute posterior covariances\n"
            + "                                 Default: 0\n"
            + "   binaryFeature:1/0  (optional) Whether all of the input features are binary\n"
            + "   useShort:1/0       (optional) Whether to use short to store feature indices\n"
            + "   option:<options>   (optional) Comma-separated list of options\n"
            + "                                 No space is allowed in <options>\n"
            + "                                 Eg: max_iter=5,epsilon=0.01,positive_weight=2\n"
            + "                      (not used) for run:predict\n";

    if (args.length < 3) {
        System.out.println("\n" + cmd);
        System.exit(0);
    }

    // Read the input parameters
    String run = null;
    String ftype = null;
    File dataFile = null;
    File outFile = null;
    double bias = 0;
    File paramFile = null;
    File initFile = null;
    double priorVar = Double.NaN;
    String option = null;
    boolean binaryFeature = false;
    boolean useShort = false;
    boolean computePostVar = true;
    boolean computePostCov = false;

    for (int i = 0; i < args.length; i++) {
        if (args[i] == null)
            continue;
        String[] token = args[i].split(":");
        if (token.length < 2)
            cmd_line_error("'" + args[i] + "' is not a valid input parameter string!", cmd);
        for (int k = 2; k < token.length; k++)
            token[1] += ":" + token[k];
        if (token[0].equals("run")) {
            run = token[1];
        } else if (token[0].equals("ftype")) {
            ftype = token[1];
        } else if (token[0].equals("data")) {
            dataFile = new File(token[1]);
        } else if (token[0].equals("out")) {
            outFile = new File(token[1]);
        } else if (token[0].equals("bias")) {
            bias = Double.parseDouble(token[1]);
        } else if (token[0].equals("param")) {
            paramFile = new File(token[1]);
        } else if (token[0].equals("init")) {
            initFile = new File(token[1]);
        } else if (token[0].equals("priorVar")) {
            priorVar = Double.parseDouble(token[1]);
        } else if (token[0].equals("option")) {
            option = token[1];
        } else if (token[0].equals("binaryFeature")) {
            binaryFeature = Util.atob(token[1]);
        } else if (token[0].equals("useShort")) {
            useShort = Util.atob(token[1]);
        } else if (token[0].equals("posteriorVar")) {
            computePostVar = Util.atob(token[1]);
        } else if (token[0].equals("posteriorCov")) {
            computePostCov = Util.atob(token[1]);
        } else
            cmd_line_error("'" + args[i] + "' is not a valid input parameter string!", cmd);
    }

    if (run == null)
        cmd_line_error("Please specify run:<command>", cmd);
    if (ftype == null)
        cmd_line_error("Please specify ftype:<file_type>", cmd);
    if (dataFile == null)
        cmd_line_error("Please specify data:<file_name>", cmd);
    if (outFile == null)
        cmd_line_error("Please specify out:<file_name>", cmd);

    if (run.equals(RUN_TRAIN)) {

        Map<String, Double> priorMean = null;
        Map<String, Double> initParam = null;
        if (paramFile != null) {
            if (!paramFile.exists())
                cmd_line_error("Param File '" + paramFile.getPath() + "' does not exist", cmd);
            priorMean = Util.readStringDoubleMap(paramFile, "=");
        }
        if (initFile != null) {
            if (!initFile.exists())
                cmd_line_error("Init File '" + initFile.getPath() + "' does not exist", cmd);
            initParam = Util.readStringDoubleMap(initFile, "=");
        }

        if (priorVar == Double.NaN)
            cmd_line_error("Please specify priorVar:<var>", cmd);

        if (!dataFile.exists())
            cmd_line_error("Data File '" + dataFile.getPath() + "' does not exist", cmd);

        LibLinearDataset dataset;
        if (binaryFeature) {
            dataset = new LibLinearBinaryDataset(bias, useShort);
        } else {
            dataset = new LibLinearDataset(bias);
        }

        if ("libsvm".equals(ftype)) {
            dataset.readFromLibSVM(dataFile);
        }
        //else if ("json".equals(ftype))
        //{
        //  dataset.readFromJSON(dataFile);
        //}
        else
            cmd_line_error("Unknown file type 'ftype:" + ftype + "'", cmd);

        if (computePostCov == true && computePostVar == false)
            cmd_line_error("Cannot compute posterior covariances with posteriorVar:0", cmd);

        LibLinear liblinear = new LibLinear();
        liblinear.setComputeFullPostVar(computePostCov);

        liblinear.train(dataset, initParam, priorMean, null, 0.0, priorVar, option, computePostVar);

        PrintStream out = new PrintStream(outFile);
        Util.printStringDoubleMap(out, liblinear.getParamMap(), "=", true);
        out.close();

        if (computePostVar) {
            out = new PrintStream(outFile + ".var");
            Util.printStringDoubleMap(out, liblinear.getPostVarMap(), "=", true);
            out.close();
            if (computePostCov) {
                out = new PrintStream(outFile + ".cov");
                Util.printStringListDoubleMap(out, liblinear.getPostVarMatrixMap(), "=");
                out.close();
            }
        }
    } else if (run.equals(RUN_PREDICT)) {

        throw new Exception("run:predict is not supported yet :(");

    } else
        cmd_line_error("Unknown run:" + run, cmd);
}

From source file:org.apache.mahout.knn.tools.TrainNewsGroupsKMeansLogisticRegression.java

public static void main(String[] args) throws IOException, ParseException {
    Options options = new Options();
    options.addOption("i", "input", true,
            "Path to the input folder containing the training set's" + " sequence files.");
    options.addOption("o", "output", true, "Base path to the output file. The name will be "
            + "appended with a suffix for each type of training.");
    options.addOption("a", "actual", false, "If set, runs the training with the actual cluster "
            + "assignments and outputs the model to the output path with a -actual suffix.");
    options.addOption("b", "ballkmeans", false, "If set, runs the training with the ball k-means "
            + "cluster assignments and outputs the model to the output path with a -ballkmeans suffix.");
    options.addOption("s", "streamingkmeans", false,
            "If set, runs the training with the "
                    + "streaming k-means cluster assignments and outputs the model to the output path with a "
                    + "-streamingkmeans suffix.");
    options.addOption("c", "centroids", true, "Path to the centroids seqfile");

    CommandLine cmd = (new PosixParser()).parse(options, args);

    String inputPath = cmd.getOptionValue("input");
    Preconditions.checkNotNull(inputPath);

    String outputBase = cmd.getOptionValue("output");
    Preconditions.checkNotNull(outputBase);

    String centroidsPath = cmd.getOptionValue("centroids");
    Preconditions.checkNotNull(centroidsPath);

    Configuration conf = new Configuration();
    SequenceFileDirIterable<Text, VectorWritable> inputIterable = new SequenceFileDirIterable<Text, VectorWritable>(
            new Path(inputPath), PathType.LIST, conf);

    PrintStream clusterIdOut = new PrintStream(new FileOutputStream("cluster-ids.csv"));
    clusterIdOut.printf("clusterName, clusterId\n");
    int clusterId = 0;
    Map<String, Integer> clusterNamesToIds = Maps.newHashMapWithExpectedSize(NUM_CLASSES);
    for (Pair<Text, VectorWritable> pair : inputIterable) {
        String clusterName = pair.getFirst().toString();
        if (!clusterNamesToIds.containsKey(clusterName)) {
            clusterIdOut.printf("%s, %d\n", clusterName, clusterId);
            clusterNamesToIds.put(clusterName, clusterId++);
        }//from ww  w . j  av  a  2  s . co m
    }
    clusterIdOut.close();

    if (cmd.hasOption("actual")) {
        System.out.printf("\nActual clusters models\n");
        System.out.printf("----------------------\n");
        long start = System.currentTimeMillis();
        trainActual(inputIterable, outputBase, clusterNamesToIds);
        long end = System.currentTimeMillis();
        System.out.printf("Trained models for actual clusters. Took %d ms\n", end - start);
    }

    if (cmd.hasOption("ballkmeans") || cmd.hasOption("streamingkmeans")) {
        SequenceFileValueIterable<CentroidWritable> centroidIterable = new SequenceFileValueIterable<CentroidWritable>(
                new Path(centroidsPath), conf);
        List<Centroid> centroids = Lists
                .newArrayList(CreateCentroids.getCentroidsFromCentroidWritableIterable(centroidIterable));

        if (cmd.hasOption("ballkmeans")) {
            System.out.printf("\nBall k-means clusters models\n");
            System.out.printf("----------------------------\n");
            long start = System.currentTimeMillis();
            trainComputed(inputIterable, outputBase, "ballkmeans", clusterNamesToIds,
                    new Pair<Integer, Iterable<Centroid>>(NUM_FEATURES_BKM, centroids));
            long end = System.currentTimeMillis();
            System.out.printf("Trained models for ballkmeans clusters. Took %d ms\n", end - start);
        }

        if (cmd.hasOption("streamingkmeans")) {
            System.out.printf("\nStreaming k-means clusters models\n");
            System.out.printf("---------------------------------\n");
            long start = System.currentTimeMillis();
            trainComputed(inputIterable, outputBase, "streamingkmeans", clusterNamesToIds,
                    new Pair<Integer, Iterable<Centroid>>(centroids.size(), centroids));
            long end = System.currentTimeMillis();
            System.out.printf("Trained models for streamingkmeans clusters. Took %d ms\n", end - start);
        }
    }
}

From source file:com.cyberway.issue.util.SurtPrefixSet.java

/**
 * Allow class to be used as a command-line tool for converting 
 * URL lists (or naked host or host/path fragments implied
 * to be HTTP URLs) to implied SURT prefix form. 
 * // www .jav a  2  s.c o  m
 * Read from stdin or first file argument. Writes to stdout. 
 *
 * @param args cmd-line arguments: may include input file
 * @throws IOException
 */
public static void main(String[] args) throws IOException {
    InputStream in = args.length > 0 ? new BufferedInputStream(new FileInputStream(args[0])) : System.in;
    PrintStream out = args.length > 1 ? new PrintStream(new BufferedOutputStream(new FileOutputStream(args[1])))
            : System.out;
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String line;
    while ((line = br.readLine()) != null) {
        if (line.indexOf("#") > 0)
            line = line.substring(0, line.indexOf("#"));
        line = line.trim();
        if (line.length() == 0)
            continue;
        out.println(prefixFromPlain(line));
    }
    br.close();
    out.close();
}

From source file:cc.wikitools.lucene.FetchWikipediaArticle.java

@SuppressWarnings("static-access")
public static void main(String[] args) throws Exception {
    Options options = new Options();
    options.addOption(// w ww.  ja  v a2  s  .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));

    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)) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(FetchWikipediaArticle.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);
    }

    WikipediaSearcher searcher = new WikipediaSearcher(indexLocation);
    PrintStream out = new PrintStream(System.out, true, "UTF-8");

    if (cmdline.hasOption(ID_OPTION)) {
        int id = Integer.parseInt(cmdline.getOptionValue(ID_OPTION));
        Document doc = searcher.getArticle(id);

        if (doc == null) {
            System.err.print("id " + id + " doesn't exist!\n");
        } else {
            out.println(doc.getField(IndexField.TEXT.name).stringValue());
        }
    } else {
        String title = cmdline.getOptionValue(TITLE_OPTION);
        Document doc = searcher.getArticle(title);

        if (doc == null) {
            System.err.print("article \"" + title + "\" doesn't exist!\n");
        } else {
            out.println(doc.getField(IndexField.TEXT.name).stringValue());
        }
    }

    searcher.close();
    out.close();
}

From source file:com.xylocore.copybook.runtime.internal.AbstractCopybookGenerator.java

public static void main(String[] args) {
    try {/*from   w ww  .  j av a  2  s . c om*/
        PrintStream myOutputStream = System.out;
        myOutputStream = new PrintStream(new FileOutputStream(
                "src/main/java/com/xylocore/commons/data/copybook/runtime/AbstractCopybook.java.new"));

        AbstractCopybookGenerator myGenerator = new AbstractCopybookGenerator();
        myGenerator.generate(myOutputStream);

        myOutputStream.close();
    } catch (Exception myException) {
        myException.printStackTrace();
    }
}

From source file:cc.wikitools.lucene.SearchWikipedia.java

@SuppressWarnings("static-access")
public static void main(String[] args) throws Exception {
    Options options = new Options();
    options.addOption(//from  www. j  a v  a  2s  . c o  m
            OptionBuilder.withArgName("path").hasArg().withDescription("index location").create(INDEX_OPTION));
    options.addOption(
            OptionBuilder.withArgName("string").hasArg().withDescription("query text").create(QUERY_OPTION));
    options.addOption(OptionBuilder.withArgName("num").hasArg().withDescription("number of results to return")
            .create(NUM_RESULTS_OPTION));

    options.addOption(new Option(VERBOSE_OPTION, "print out complete document"));
    options.addOption(new Option(TITLE_OPTION, "search title"));
    options.addOption(new Option(ARTICLE_OPTION, "search article"));

    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(QUERY_OPTION) || !cmdline.hasOption(INDEX_OPTION)
            || !cmdline.hasOption(QUERY_OPTION)) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(SearchWikipedia.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);
    int numResults = cmdline.hasOption(NUM_RESULTS_OPTION)
            ? Integer.parseInt(cmdline.getOptionValue(NUM_RESULTS_OPTION))
            : DEFAULT_NUM_RESULTS;
    boolean verbose = cmdline.hasOption(VERBOSE_OPTION);
    boolean searchArticle = !cmdline.hasOption(TITLE_OPTION);

    PrintStream out = new PrintStream(System.out, true, "UTF-8");

    WikipediaSearcher searcher = new WikipediaSearcher(indexLocation);
    TopDocs rs = null;
    if (searchArticle) {
        rs = searcher.searchArticle(queryText, numResults);
    } else {
        rs = searcher.searchTitle(queryText, numResults);
    }

    int i = 1;
    for (ScoreDoc scoreDoc : rs.scoreDocs) {
        Document hit = searcher.doc(scoreDoc.doc);

        out.println(String.format("%d. %s (wiki id = %s, lucene id = %d) %f", i,
                hit.getField(IndexField.TITLE.name).stringValue(),
                hit.getField(IndexField.ID.name).stringValue(), scoreDoc.doc, scoreDoc.score));
        if (verbose) {
            out.println("# " + hit.toString().replaceAll("[\\n\\r]+", " "));
        }
        i++;
    }

    searcher.close();
    out.close();
}

From source file:cc.twittertools.index.ExtractTermStatisticsFromIndex.java

@SuppressWarnings("static-access")
public static void main(String[] args) throws Exception {
    Options options = new Options();

    options.addOption(OptionBuilder.withArgName("dir").hasArg().withDescription("index").create(INDEX_OPTION));
    options.addOption(OptionBuilder.withArgName("num").hasArg().withDescription("min").create(MIN_OPTION));

    CommandLine cmdline = null;/*from   ww w.  j a  va 2  s  . c o  m*/
    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(ExtractTermStatisticsFromIndex.class.getName(), options);
        System.exit(-1);
    }

    String indexLocation = cmdline.getOptionValue(INDEX_OPTION);
    int min = cmdline.hasOption(MIN_OPTION) ? Integer.parseInt(cmdline.getOptionValue(MIN_OPTION)) : 1;

    PrintStream out = new PrintStream(System.out, true, "UTF-8");

    IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(indexLocation)));
    Terms terms = SlowCompositeReaderWrapper.wrap(reader).terms(StatusField.TEXT.name);
    TermsEnum termsEnum = terms.iterator(TermsEnum.EMPTY);

    long missingCnt = 0;
    int skippedTerms = 0;
    BytesRef bytes = new BytesRef();
    while ((bytes = termsEnum.next()) != null) {
        byte[] buf = new byte[bytes.length];
        System.arraycopy(bytes.bytes, 0, buf, 0, bytes.length);
        String term = new String(buf, "UTF-8");
        int df = termsEnum.docFreq();
        long cf = termsEnum.totalTermFreq();

        if (df < min) {
            skippedTerms++;
            missingCnt += cf;
            continue;
        }

        out.println(term + "\t" + df + "\t" + cf);
    }

    reader.close();
    out.close();
    System.err.println("skipped terms: " + skippedTerms + ", cnt: " + missingCnt);
}

From source file:Redirect.java

public static void main(String args[]) throws Exception {
    PrintStream origOut = System.out;
    PrintStream origErr = System.err;

    InputStream stdin = null;/*from  www. ja va 2s  .  c om*/
    stdin = new FileInputStream("Redirect.in");
    PrintStream stdout = null;
    stdout = new PrintStream(new FileOutputStream("Redirect.out"));
    PrintStream stderr = null;
    stderr = new PrintStream(new FileOutputStream("Redirect.err"));
    origOut.println("1");
    System.out.println("2");
    origOut.println("3");
    System.err.println("4");
    origErr.println("5");

    System.setIn(stdin);
    System.setOut(stdout);
    System.setErr(stderr);

    origOut.println("\nR");
    System.out.println("T");
    origOut.println("Tq");
    System.err.println("Tqw");
    origErr.println("Test");

    origOut.println("\nRedirect:  Round #3");
    int inChar = 0;
    while (-1 != inChar) {
        try {
            inChar = System.in.read();
        } catch (Exception e) {
            // Clean up the output and bail.
            origOut.print("\n");
            break;
        }
        origOut.write(inChar);
    }

    stdin.close();
    stdout.close();
    stderr.close();

    System.exit(0);
}