Example usage for java.io PrintWriter printf

List of usage examples for java.io PrintWriter printf

Introduction

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

Prototype

public PrintWriter printf(String format, Object... args) 

Source Link

Document

A convenience method to write a formatted string to this writer using the specified format string and arguments.

Usage

From source file:Main.java

public static void main(String[] args) {
    String s = "tutorial from java2s.com";
    try {/* www  .  j ava2  s  .  co  m*/

        PrintWriter pw = new PrintWriter(System.out);

        // %s indicates a string will be placed there, which is s
        pw.printf("This is a %s program", s);

        // flush the writer
        pw.flush();

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:com.aestel.chemistry.openEye.fp.DistMatrix.java

public static void main(String... args) throws IOException {
    long start = System.currentTimeMillis();

    // create command line Options object
    Options options = new Options();
    Option opt = new Option("i", true, "input file [.tsv from FingerPrinter]");
    opt.setRequired(true);//from w ww  . j  a v a  2  s.  c om
    options.addOption(opt);

    opt = new Option("o", true, "outpur file [.tsv ");
    opt.setRequired(true);
    options.addOption(opt);

    CommandLineParser parser = new PosixParser();
    CommandLine cmd = null;
    try {
        cmd = parser.parse(options, args);
    } catch (Exception e) {
        System.err.println(e.getMessage());
        exitWithHelp(options);
    }
    args = cmd.getArgs();

    if (args.length != 0)
        exitWithHelp(options);

    String file = cmd.getOptionValue("i");
    BufferedReader in = new BufferedReader(new FileReader(file));

    file = cmd.getOptionValue("o");
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file)));

    ArrayList<Fingerprint> fps = new ArrayList<Fingerprint>();
    ArrayList<String> ids = new ArrayList<String>();
    String line;
    while ((line = in.readLine()) != null) {
        String[] parts = line.split("\t");
        if (parts.length == 3) {
            ids.add(parts[0]);
            fps.add(new ByteFingerprint(parts[2]));
        }
    }
    in.close();

    out.print("ID");
    for (int i = 0; i < ids.size(); i++) {
        out.print('\t');
        out.print(ids.get(i));
    }
    out.println();

    for (int i = 0; i < ids.size(); i++) {
        out.print(ids.get(i));
        Fingerprint fp1 = fps.get(i);

        for (int j = 0; j <= i; j++) {
            out.printf("\t%.4g", fp1.tanimoto(fps.get(j)));
        }
        out.println();
    }
    out.close();

    System.err.printf("Done %d fingerprints in %.2gsec\n", fps.size(),
            (System.currentTimeMillis() - start) / 1000D);
}

From source file:MainClass.java

public static void premain(final Instrumentation inst) {
    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            try {
                PrintWriter out = new PrintWriter(System.err);

                ThreadMXBean tb = ManagementFactory.getThreadMXBean();
                out.printf("Current thread count: %d%n", tb.getThreadCount());
                out.printf("Peak thread count: %d%n", tb.getPeakThreadCount());

                List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
                for (MemoryPoolMXBean pool : pools) {
                    MemoryUsage peak = pool.getPeakUsage();
                    out.printf("Peak %s memory used: %,d%n", pool.getName(), peak.getUsed());
                    out.printf("Peak %s memory reserved: %,d%n", pool.getName(), peak.getCommitted());
                }/*from www  . j  a v  a2s  .  com*/

                Class[] loaded = inst.getAllLoadedClasses();
                out.println("Loaded classes:");
                for (Class c : loaded)
                    out.println(c.getName());
                out.close();
            } catch (Throwable t) {
                System.err.println("Exception in agent: " + t);
            }
        }
    });
}

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  w  w w .jav a 2  s  . c  o 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:ca.cutterslade.match.scheduler.Main.java

private static String summary(final Scheduler scheduler) {
    final StringWriter w = new StringWriter();
    final PrintWriter p = new PrintWriter(w);
    final String columnFormat = " %15s";
    p.printf(columnFormat, "Day");
    p.printf(columnFormat, "Time");
    for (final Court c : scheduler.getCourts()) {
        p.printf(columnFormat, c.getGym().getName() + '-' + c.getName());
    }//from ww w . j  a  va 2 s  . c  om
    p.println();
    for (final Day d : scheduler.getDays())
        for (final Time t : scheduler.getTimes()) {
            p.printf(columnFormat, d.getName());
            p.printf(columnFormat, t.getName());
            for (final Court c : scheduler.getCourts()) {
                final Match m = scheduler.getMatch(d, t, c);
                p.printf(columnFormat, teamsString(m));
            }
            p.println();
        }
    p.close();
    return w.toString();
}

From source file:com.rsmart.rfabric.logging.FormattedLogger.java

/**
 * Applies a pattern with parameters to create a {@link String} used as a logging message
 *  //from   w  w  w  . ja v  a2 s.c o m
 * 
 * @param pattern to format against
 * @param objs an array of objects used as parameters to the <code>pattern</code>
 * @return Logging Message
 */
private static final String getMessage(String pattern, Object... objs) {
    StringWriter retval = new StringWriter();
    PrintWriter writer = new PrintWriter(retval);

    writer.printf(pattern, objs);

    return retval.toString();
}

From source file:Main.java

/**
 * dump bipartite community affiliation into a text file with node names
 * /*from   w w  w  . j  a  va  2 s. co  m*/
 * @param OutFNm
 * @param CmtyVV
 * @param NIDNmH
 */
static void dumpCmtyVV(final String OutFNm, Vector<Vector<Integer>> CmtyVV, Hashtable<Integer, String> NIDNmH) {
    PrintWriter f;
    try {
        f = new PrintWriter(OutFNm);

        for (int c = 0; c < CmtyVV.size(); c++) {
            for (int u = 0; u < CmtyVV.get(c).size(); u++) {
                if (NIDNmH.containsKey(CmtyVV.get(c).get(u))) {
                    f.printf("%s\t", NIDNmH.get(CmtyVV.get(c).get(u)));
                } else {
                    f.printf("%d\t", (int) CmtyVV.get(c).get(u));
                }
            }
            f.printf("\n");
        }
        f.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:com.tamingtext.tagging.LuceneCategoryExtractor.java

/** dump the values stored in the specified field for each document.
 * //from  ww w  .j av  a2 s . c  o m
 *  <pre>term(tab)document_frequency</pre>
 *  
 * @param indexDir the index to read.
 * @param field the name of the field.
 * @param out the print writer output will be written to
 * @throws IOException
 */
public static void dumpDocumentFields(File indexDir, String field, long maxDocs, PrintWriter out)
        throws IOException {
    Directory dir = FSDirectory.open(indexDir);
    IndexReader reader = IndexReader.open(dir, true);
    int max = reader.maxDoc();
    for (int i = 0; i < max; i++) {
        if (!reader.isDeleted(i)) {
            Document d = reader.document(i);
            for (Field f : d.getFields(field)) {
                if (f.isStored() && !f.isBinary()) {
                    String value = f.stringValue();
                    if (value != null) {
                        out.printf("%s\n", value);
                    }
                }
            }
        }
    }
}

From source file:org.isatools.isatab.commandline.permission_manager.PermissionManager.java

/**
 * Pretty printing of a study, for command line printing.
 *///from  w  w w.ja va 2s . com
public static String formatStudy(Study study) {
    StringWriter sw = new StringWriter();
    PrintWriter out = new PrintWriter(sw);
    out.println("ACCESSION: " + study.getAcc());
    out.printf("  Title: %50.50s\n", study.getTitle());
    out.println(
            "  Submission Date: " + study.getSubmissionDate() + ", Release Date: " + study.getReleaseDate());
    out.println("  Visibility: " + study.getStatus());
    out.println("  Users:");
    out.print("  ");
    for (User u : study.getUsers()) {
        out.printf("%15.15s  ", u.getUserName());
    }
    out.println();
    out.println();
    out.flush();
    return sw.toString();
}

From source file:com.zimbra.kabuki.tools.img.ImageMerger.java

private static void print(PrintWriter out, String format, Object... args) {
    if (out != null)
        out.printf(format, args);
}