Example usage for java.util Formatter format

List of usage examples for java.util Formatter format

Introduction

In this page you can find the example usage for java.util Formatter format.

Prototype

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

Source Link

Document

Writes a formatted string to this object's destination using the specified format string and arguments.

Usage

From source file:Main.java

public static void main(String[] args) throws FileNotFoundException {

    Formatter formatter = new Formatter(new File("generated/format.txt"));

    // format a new string
    String name = "from java2s.com";
    formatter.format("Hello %s !", name);

    // print the formatted string
    System.out.println(formatter);

    // flush the formatter. Here it does nothing.
    formatter.flush();/*from  w w w  .  j a va2  s.  c  o  m*/
    System.out.println("Formatter Flushed.");
}

From source file:Main.java

public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {

    Formatter formatter = new Formatter(new File("generated/format.txt"), "ASCII");

    // format a new string
    String name = "from java2s.com";
    formatter.format("Hello %s !", name);

    // print the formatted string
    System.out.println(formatter);

    // flush the formatter. Here it does nothing.
    formatter.flush();//from  w w w.  j a  va 2s  .c om
    System.out.println("Formatter Flushed.");
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Formatter fmtCon = new Formatter(System.out);
    Formatter fmtFile;/*  w  ww. j  a va  2s.c o  m*/
    fmtFile = new Formatter(new FileOutputStream("test.fmt"));
    fmtCon.format("a negative number: %(.2f\n\n", -123.34);
    fmtCon.format("%8s %8s\n", "Value", "Square");

    for (int i = 1; i < 20; i++)
        fmtCon.format("%8d %8d\n", i, i * i);

    // write to the file.
    fmtFile.format("This is a negative number: %(.2f\n\n", -123.34);

    fmtFile.format("%8s %8s\n", "Value", "Square");
    for (int i = 1; i < 20; i++)
        fmtFile.format("%8d %8d\n", i, i * i);

    fmtFile.close();

    if (fmtFile.ioException() != null) {
        System.out.println("File I/O Error Occurred");
    }
}

From source file:Main.java

public static void main(String[] args) throws FileNotFoundException {

    Formatter formatter = new Formatter(new FileOutputStream("generated/format.txt"));

    // format a new string
    String name = "from java2s.com";
    formatter.format("Hello %s !", name);

    // print the formatted string
    System.out.println(formatter);

    // flush the formatter. Here it does nothing.
    formatter.flush();// w w w .jav  a  2s .  c o  m
    System.out.println("Formatter Flushed.");
}

From source file:Main.java

public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {

    Formatter formatter = new Formatter("generated/format.txt", "ASCII", Locale.getDefault());

    // format a new string
    String name = "from java2s.com";
    formatter.format("Hello %s !", name);

    // print the formatted string
    System.out.println(formatter);

    // flush the formatter. Here it does nothing.
    formatter.flush();/* w w  w  .j  a  v  a  2  s . c o  m*/
    System.out.println("Formatter Flushed.");
}

From source file:Main.java

public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {

    Formatter formatter = new Formatter(new FileOutputStream("generated/format.txt"), "ASCII");

    // format a new string
    String name = "from java2s.com";
    formatter.format("Hello %s !", name);

    // print the formatted string
    System.out.println(formatter);

    // flush the formatter. Here it does nothing.
    formatter.flush();// www  .ja v a  2  s  .co m
    System.out.println("Formatter Flushed.");
}

From source file:Main.java

public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {

    Formatter formatter = new Formatter(new PrintStream(new FileOutputStream("generated/format.txt")));

    // format a new string
    String name = "from java2s.com";
    formatter.format("Hello %s !", name);

    // print the formatted string
    System.out.println(formatter);

    // flush the formatter. Here it does nothing.
    formatter.flush();/*from  w ww.j  a va2  s  . c o  m*/
    System.out.println("Formatter Flushed.");
}

From source file:Main.java

public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {

    Formatter formatter = new Formatter(new File("generated/format.txt"), "ASCII", Locale.getDefault());

    // format a new string
    String name = "from java2s.com";
    formatter.format("Hello %s !", name);

    // print the formatted string
    System.out.println(formatter);

    // flush the formatter. Here it does nothing.
    formatter.flush();/*from  w  w  w  . j av a2  s. c o m*/
    System.out.println("Formatter Flushed.");
}

From source file:Main.java

public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {

    Formatter formatter = new Formatter(new FileOutputStream("generated/format.txt"), "ASCII",
            Locale.getDefault());

    // format a new string
    String name = "from java2s.com";
    formatter.format("Hello %s !", name);

    // print the formatted string
    System.out.println(formatter);

    // flush the formatter. Here it does nothing.
    formatter.flush();/*from   w w  w .  j  av  a 2  s .  c  o  m*/
    System.out.println("Formatter Flushed.");
}

From source file:Main.java

private static String encodeHex(byte[] data) {
    StringBuilder sb = new StringBuilder(data.length * 2);
    Formatter formatter = new Formatter(sb);
    for (byte b : data) {
        formatter.format("%02x", b);
    }//from   www  .ja v a 2 s  .c o m
    return sb.toString();
}