Example usage for java.io PrintWriter println

List of usage examples for java.io PrintWriter println

Introduction

In this page you can find the example usage for java.io PrintWriter println.

Prototype

public void println() 

Source Link

Document

Terminates the current line by writing the line separator string.

Usage

From source file:edu.cornell.med.icb.goby.reads.ColorSpaceConverter.java

public static void main(final String[] args) throws JSAPException, IOException {
    final JSAP jsap = new JSAP();

    final FlaggedOption sequenceOption = new FlaggedOption("input");
    sequenceOption.setRequired(true);//from www .  ja  va 2  s.co m
    sequenceOption.setLongFlag("input");
    sequenceOption.setShortFlag('i');
    sequenceOption.setStringParser(FileStringParser.getParser().setMustBeFile(true).setMustExist(true));
    sequenceOption.setHelp("The input file (in Fasta format) to convert");
    jsap.registerParameter(sequenceOption);

    final FlaggedOption outputOption = new FlaggedOption("output");
    outputOption.setRequired(false);
    outputOption.setLongFlag("output");
    outputOption.setShortFlag('o');
    outputOption.setStringParser(FileStringParser.getParser().setMustBeFile(true));
    outputOption.setHelp("The output file to write to (default = stdout)");
    jsap.registerParameter(outputOption);

    final FlaggedOption titleOption = new FlaggedOption("title");
    titleOption.setRequired(false);
    titleOption.setLongFlag("title");
    titleOption.setShortFlag('t');
    titleOption.setHelp("Title for this conversion");
    jsap.registerParameter(titleOption);

    final Switch verboseOption = new Switch("verbose");
    verboseOption.setLongFlag("verbose");
    verboseOption.setShortFlag('v');
    verboseOption.setHelp("Verbose output");
    jsap.registerParameter(verboseOption);

    final Switch helpOption = new Switch("help");
    helpOption.setLongFlag("help");
    helpOption.setShortFlag('h');
    helpOption.setHelp("Print this message");
    jsap.registerParameter(helpOption);

    jsap.setUsage("Usage: " + ColorSpaceConverter.class.getName() + " " + jsap.getUsage());

    final JSAPResult result = jsap.parse(args);

    if (result.getBoolean("help")) {
        System.out.println(jsap.getHelp());
        System.exit(0);
    }

    if (!result.success()) {
        final Iterator<String> errors = result.getErrorMessageIterator();
        while (errors.hasNext()) {
            System.err.println(errors.next());
        }
        System.err.println(jsap.getUsage());
        System.exit(1);
    }

    final boolean verbose = result.getBoolean("verbose");

    final File sequenceFile = result.getFile("input");
    if (verbose) {
        System.out.println("Reading sequence from: " + sequenceFile);
    }

    // extract the title to use for the output header
    final String title;
    if (result.contains("title")) {
        title = result.getString("title");
    } else {
        title = sequenceFile.getName();
    }

    Reader inputReader = null;
    PrintWriter outputWriter = null;

    try {
        if ("gz".equals(FilenameUtils.getExtension(sequenceFile.getName()))) {
            inputReader = new InputStreamReader(new GZIPInputStream(FileUtils.openInputStream(sequenceFile)));
        } else {
            inputReader = new FileReader(sequenceFile);
        }
        final FastaParser fastaParser = new FastaParser(inputReader);

        final File outputFile = result.getFile("output");
        final OutputStream outputStream;
        if (outputFile != null) {
            outputStream = FileUtils.openOutputStream(outputFile);
            if (verbose) {
                System.out.println("Writing sequence : " + outputFile);
            }
        } else {
            outputStream = System.out;
        }
        outputWriter = new PrintWriter(outputStream);

        // write the header portion of the output
        outputWriter.print("# ");
        outputWriter.print(new Date());
        outputWriter.print(' ');
        outputWriter.print(ColorSpaceConverter.class.getName());
        for (final String arg : args) {
            outputWriter.print(' ');
            outputWriter.print(arg);
        }
        outputWriter.println();
        outputWriter.print("# Cwd: ");
        outputWriter.println(new File(".").getCanonicalPath());
        outputWriter.print("# Title: ");
        outputWriter.println(title);

        // now parse the input sequence
        long sequenceCount = 0;
        final MutableString descriptionLine = new MutableString();
        final MutableString sequence = new MutableString();
        final MutableString colorSpaceSequence = new MutableString();

        while (fastaParser.hasNext()) {
            fastaParser.next(descriptionLine, sequence);
            outputWriter.print('>');
            outputWriter.println(descriptionLine);

            convert(sequence, colorSpaceSequence);
            CompactToFastaMode.writeSequence(outputWriter, colorSpaceSequence);

            sequenceCount++;
            if (verbose && sequenceCount % 10000 == 0) {
                System.out.println("Converted " + sequenceCount + " entries");
            }
        }

        if (verbose) {
            System.out.println("Conversion complete!");
        }
    } finally {
        IOUtils.closeQuietly(inputReader);
        IOUtils.closeQuietly(outputWriter);
    }
}

From source file:com.ifeng.sorter.NginxApp.java

public static void main(String[] args) {

    String input = "src/test/resources/data/nginx_report.txt";

    PrintWriter pw = null;

    Map<String, List<LogBean>> resultMap = new HashMap<String, List<LogBean>>();
    List<String> ips = new ArrayList<String>();

    try {/*  w  ww  .jav a 2  s . co m*/
        List<String> lines = FileUtils.readLines(new File(input));
        List<LogBean> items = new ArrayList<LogBean>();

        for (String line : lines) {
            String[] values = line.split("\t");

            if (values != null && values.length == 3) {// ip total seria
                try {
                    String ip = values[0].trim();
                    String total = values[1].trim();
                    String seria = values[2].trim();

                    LogBean bean = new LogBean(ip, Integer.parseInt(total), seria);

                    items.add(bean);

                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
        }

        Collections.sort(items);

        for (LogBean bean : items) {
            String key = bean.getIp();

            if (resultMap.containsKey(key)) {
                resultMap.get(key).add(bean);
            } else {
                List<LogBean> keyList = new ArrayList<LogBean>();
                keyList.add(bean);
                resultMap.put(key, keyList);

                ips.add(key);
            }
        }

        pw = new PrintWriter("src/test/resources/output/result.txt", "UTF-8");

        for (String ip : ips) {
            List<LogBean> list = resultMap.get(ip);

            for (LogBean bean : list) {
                pw.append(bean.toString());
                pw.println();
            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        pw.flush();
        pw.close();
    }
}

From source file:Main.java

private static void writeFile(String filePath, String content) throws IOException {
    FileWriter fw = new FileWriter(filePath);
    PrintWriter out = new PrintWriter(fw);
    out.write(content);//from w  w  w . j a  va2s .co m
    out.println();
    fw.close();
    out.close();
}

From source file:com.kaaprotech.satu.util.CmdLineUtil.java

public static String getCommandLineUsageMessage() {
    final HelpFormatter formatter = new HelpFormatter();
    final StringWriter sw = new StringWriter();
    final PrintWriter pw = new PrintWriter(sw, true);
    pw.println();
    formatter.printHelp(pw, 120, "Satu", null, getOptions(), 2, 10, null);
    return sw.getBuffer().toString();
}

From source file:com.azaptree.services.spring.application.SpringApplicationService.java

private static void validate(final String[] args) {
    if (args.length == 0) {
        final StringWriter sw = new StringWriter(256);
        final PrintWriter pw = new PrintWriter(sw);
        pw.println();
        pw.println(StringUtils.repeat("=", 160));
        pw.println(// w  w w  .  j  av a2  s. com
                "Usage: java com.azaptree.services.spring.application.SpringApplicationService <config.xml>");
        pw.println();
        pw.println(
                "       where <config.xml> = XML file location (loaded using Spring ResourceLoader) that must validate against spring-application-service.xsd");
        pw.println(StringUtils.repeat("=", 160));
        throw new IllegalArgumentException(sw.toString());
    }
}

From source file:PrintWriterDemo.java

public static void main() throws Exception {
    PrintWriter pw = new PrintWriter(new FileWriter("dice.txt"));
    for (int i = 1; i <= 1000; i++) {
        int die = (int) (1 + 6 * Math.random());
        pw.print(die);//from   w  w w .ja  va2 s .c o  m
        pw.print(' ');
        if (i % 20 == 0)
            pw.println();
    }
    pw.println();
    pw.close(); // Without this, the output file may be empty
}

From source file:de.tudarmstadt.ukp.dkpro.c4corpus.hadoop.statistics.StatisticsTableCreator.java

public static void saveTableToCsv(Table<String, String, Long> table, OutputStream outputStream) {
    PrintWriter pw = new PrintWriter(outputStream);
    pw.write(";");
    for (String columnKey : table.columnKeySet()) {
        pw.printf("%s;", columnKey);
    }/*from ww w.  j  a  va 2s .  co  m*/
    pw.println();

    for (String rowKey : table.rowKeySet()) {
        pw.printf("%s;", rowKey);
        for (String columnKey : table.columnKeySet()) {
            Long value = table.get(rowKey, columnKey);
            pw.printf("%d;", value != null ? value : 0);
        }
        pw.println();
    }

    IOUtils.closeQuietly(pw);
}

From source file:Main.java

public static String toString(Throwable e) {
    StringWriter w = new StringWriter();
    PrintWriter p = new PrintWriter(w);
    p.print(e.getClass().getName() + ": ");
    if (e.getMessage() != null) {
        p.print(e.getMessage() + "\n");
    }/*from   w ww .j av  a  2  s  .  c om*/
    p.println();
    try {
        e.printStackTrace(p);
        return w.toString();
    } finally {
        p.close();
    }
}

From source file:Main.java

/**
 * make throwable to string.//from w w w.j  a  va  2  s .com
 */
public static String toString(Throwable throwable) {
    StringWriter stringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stringWriter);
    printWriter.print(throwable.getClass().getName() + ": ");
    if (throwable.getMessage() != null) {
        printWriter.print(throwable.getMessage() + "\n");
    }
    printWriter.println();
    try {
        throwable.printStackTrace(printWriter);
        return stringWriter.toString();
    } finally {
        printWriter.close();
    }
}

From source file:com.amazonaws.eclipse.core.diagnostic.utils.AwsPortalFeedbackFormUtils.java

public static String getStackTraceFromThrowable(Throwable t) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);

    t.printStackTrace(pw);//from  w w  w  .jav  a2 s .co m
    pw.println();

    if (t.getCause() != null) {
        t.getCause().printStackTrace(pw);
    }

    return sw.toString();
}