Example usage for java.io PrintWriter format

List of usage examples for java.io PrintWriter format

Introduction

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

Prototype

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

Source Link

Document

Writes 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 {//from   ww  w. ja v a  2s.  c  o m

        PrintWriter pw = new PrintWriter(System.out);

        // format text with default locale
        // %s indicates a string will be placed there, which is s
        pw.format("This is a %s program", s);

        // flush the writer
        pw.flush();

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

From source file:org.sonatype.nexus.internal.scheduling.NexusTaskFailureAlertEmailSender.java

private void sendEmail(final String address, final String taskId, final String taskName, final Throwable cause)
        throws Exception {
    Email mail = new SimpleEmail();
    mail.setSubject("Task execution failure");
    mail.addTo(address);/*from ww w .  j  a v  a  2 s.  co m*/

    // FIXME: This should ideally render a user-configurable template
    StringWriter buff = new StringWriter();
    PrintWriter out = new PrintWriter(buff);
    if (taskId != null) {
        out.format("Task ID: %s%n", taskId);
    }
    if (taskName != null) {
        out.format("Task Name: %s%n", taskName);
    }
    if (cause != null) {
        out.println("Stack-trace:");
        cause.printStackTrace(out);
    }
    mail.setMsg(buff.toString());

    emailManager.get().send(mail);
}

From source file:com.javiermoreno.springboot.mvc.minimal.httpd.PingController.java

@RequestMapping(value = "/long", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)// ww w .j a  v  a2s  . com
void longPing(HttpServletResponse response) throws IOException, InterruptedException {
    response.setBufferSize(0);
    PrintWriter out = response.getWriter();
    out.println();
    out.flush();
    for (int i = 0; i < 60 * 5; i++) {
        out.format("Current server time: %s.\r\n", new Date());
        out.flush();
        Thread.sleep(1000);
    }
}

From source file:sf.net.experimaestro.server.XPMServlet.java

public void show404(HttpServletResponse response, String format, Object... objects) throws IOException {
    response.setContentType("text/html");
    response.setStatus(HttpServletResponse.SC_NOT_FOUND);
    PrintWriter writer = response.getWriter();
    header(writer, "Page not found");
    writer.format("<div class=\"error\">%s</div>", escapeHtml(String.format(format, objects)));
}

From source file:sf.net.experimaestro.server.XPMServlet.java

public void error404(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setContentType("text/html");
    response.setStatus(HttpServletResponse.SC_NOT_FOUND);
    final PrintWriter out = response.getWriter();
    header(out, "Error");
    out.println("<h1>Page not found</h1>");
    out.format("<p>This URI was not found: %s</p>", request.getRequestURI());
    out.println("</body></html>");
}

From source file:org.andromda.cartridges.java.EnumClassGenerator.java

/**
 * Generate the private constructor.//from  www  . j  av a 2  s.co m
 * 
 * @param Class
 *              clazz the UML class
 * @param PrintWriter
 *              the writer used to write the code
 */
protected void generateConstructor(Classifier clazz, PrintWriter writer) {
    writer.format("private final %s enumValue;", javaType);
    writer.println();
    generateSimpleComment(writer, PRIVATE_CONSTRUCTOR_COMMENT);
    writer.format("private %s(%s value) { this.enumValue = value; }", clazz.getName(), javaType);
    writer.println();
}

From source file:mbenson.annotationprocessing.ProcessorBase.java

/**
 * Generate an error message./*w  ww  . j a v a2 s. c om*/
 * 
 * @param cause
 * @param message
 * @param args
 */
protected void error(Throwable cause, String message, Object... args) {
    StringWriter msg = new StringWriter();
    PrintWriter w = new PrintWriter(msg);
    w.format(message, args);
    w.println();
    if (cause != null) {
        cause.printStackTrace(w);
    }
    printMessage(Kind.ERROR, msg.toString());
}

From source file:org.apache.jackrabbit.performance.AbstractPerformanceTest.java

private void writeReport(String test, String name, DescriptiveStatistics statistics) throws IOException {
    File report = new File("target", test + ".txt");

    boolean needsPrefix = !report.exists();
    PrintWriter writer = new PrintWriter(new FileWriterWithEncoding(report, "UTF-8", true));
    try {/*  www  . j ava  2  s  .  c  o  m*/
        if (needsPrefix) {
            writer.format("# %-34.34s     min     10%%     50%%     90%%     max%n", test);
        }

        writer.format("%-36.36s  %6.0f  %6.0f  %6.0f  %6.0f  %6.0f%n", name, statistics.getMin(),
                statistics.getPercentile(10.0), statistics.getPercentile(50.0), statistics.getPercentile(90.0),
                statistics.getMax());
    } finally {
        writer.close();
    }
}

From source file:org.andromda.cartridges.java.EnumClassGenerator.java

/**
 * Generate the value method./* w  w w  .  ja v a  2  s.co m*/
 * 
 * @param Class
 *              clazz the UML class
 * @param PrintWriter
 *              the writer used to write the code
 */
protected void generateValueMethod(Classifier clazz, PrintWriter writer) {
    generateSimpleComment(writer, String.format(VALUE_COMMENT, clazz.getName()));
    writer.format("public %s value() { return this.enumValue; }", javaType);
    writer.println();
}

From source file:lv.semti.morphology.analyzer.Word.java

public void print(PrintWriter stream) {
        stream.format("Aprakstam v?rdu '%s'%n", token);
        if (wordforms.isEmpty()) {
            stream.println("\tV?rds nav atpazts.\n");
        } else {//from w w w  . j  a  va2 s.c o m
            if (wordforms.size() == 1) {
                stream.println("\tV?rds ir atpazts viennozmgi.\n");
                wordforms.get(0).describe(stream);
            } else {
                stream.format("\tV?rds ir atpazts %d variantos%n", wordforms.size());
                for (Wordform variants : wordforms) {
                    stream.format("\tVariants %d%n", wordforms.indexOf(variants) + 1);
                    variants.describe(stream);
                }
            }
        }
        stream.flush();
    }