List of usage examples for java.util Formatter Formatter
public Formatter(OutputStream os)
From source file:Main.java
public static void main(String[] argv) throws Exception { Formatter fmtCon = new Formatter(System.out); Formatter 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();// w w w.j a va2 s . com if (fmtFile.ioException() != null) { System.out.println("File I/O Error Occurred"); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { Formatter fmtCon = new Formatter(System.out); Formatter fmtFile;/*from w w w. j av a2 s . 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) { StringBuffer buffer = new StringBuffer(); Formatter formatter = new Formatter(buffer); // 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 2 s . c o m*/ System.out.println("Formatter Flushed."); }
From source file:Main.java
public static void main(String[] args) { StringBuilder sb = new StringBuilder(); Formatter fm = new Formatter(sb); // Formatting strings fm.format("%1$s, %2$s, and %3$s %n", "A", "B", "C"); fm.format("%3$s, %2$s, and %1$s %n", "D", "E", "F"); // Formatting numbers fm.format("%1$4d, %2$4d, %3$4d %n", 1, 10, 100); fm.format("%1$4d, %2$4d, %3$4d %n", 10, 100, 1000); fm.format("%1$-4d, %2$-4d, %3$-4d %n", 1, 10, 100); fm.format("%1$-4d, %2$-4d, %3$-4d %n", 10, 100, 1000); // Formatting date and time Date dt = new Date(); fm.format("Today is %tD %n", dt); fm.format("Today is %tF %n", dt); fm.format("Today is %tc %n", dt); // Display the entire formatted string System.out.println(sb.toString()); }
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();// w w w . j a v a 2 s . com System.out.println("Formatter Flushed."); }
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();/*from w w w. j av a 2s . c o m*/ System.out.println("Formatter Flushed."); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { StringBuilder buf = new StringBuilder(); // Buffer to hold output Formatter formatter = new Formatter(buf); // Formatter to format data into // buf formatter.format("%4d)%+7.2f", 2, 1234.1234); System.out.println(buf);//from w w w.j av a2s . c o m }
From source file:Main.java
public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException { Formatter formatter = new Formatter("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 va 2 s.c o m System.out.println("Formatter Flushed."); }
From source file:Main.java
public static void main(String[] args) { File file = new File("xyz.txt"); Formatter fm = null;// w w w .ja va2s . co m try { // Create a Formatter that will write the output the file fm = new Formatter(file); // Formatting strings fm.format("%1$s, %2$s, and %3$s %n", "A", "B", "C"); fm.format("%3$s, %2$s, and %1$s %n", "A", "B", "C"); // Format more text here... } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (fm != null) { fm.close(); } } }
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 w w . j a v a 2s . c om*/ System.out.println("Formatter Flushed."); }