Example usage for java.io PrintWriter PrintWriter

List of usage examples for java.io PrintWriter PrintWriter

Introduction

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

Prototype

public PrintWriter(File file) throws FileNotFoundException 

Source Link

Document

Creates a new PrintWriter, without automatic line flushing, with the specified file.

Usage

From source file:Main.java

public static void main(String[] args) {
    try {/*from   w  w  w .  j  av a  2s.c o m*/
        PrintWriter pw = new PrintWriter(new FileWriter("c:/text.txt"));

        // append chars
        pw.append('H');
        pw.append('e');
        pw.append('l');
        pw.append('l');
        pw.append('o');

        // flush the writer
        pw.flush();
        pw.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {
    boolean bool = false;
    try {//  w ww .  j  a v  a  2  s.  c  o m

        PrintWriter pw = new PrintWriter(System.out);

        // print a boolean
        pw.print(true);

        // change the line
        pw.println();

        // print another boolean
        pw.print(bool);

        // flush the writer
        pw.flush();

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

From source file:Main.java

public static void main(String[] args) {
    String str = "tutorial from java2s.com!";

    Writer writer = new PrintWriter(System.out);

    try {//from  w  w w.  ja v  a2  s  .c o m
        // write a string
        writer.write(str);

        // flush the writer
        writer.flush();

        // change line and write another string
        writer.write("\nThis is an example");

        // flush the stream again
        writer.flush();

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

From source file:Main.java

public static void main(String[] args) {
    String str = "tutorial from java2s.com!";

    Writer writer = new PrintWriter(System.out);

    try {/*from  w w w . java2 s  .c o  m*/
        // write a string portion
        writer.write(str, 0, 5);

        // flush the writer
        writer.flush();

        // write another string portion
        writer.write(str, 5, 6);

        // flush the stream again
        writer.flush();

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

From source file:Main.java

public static void main(String[] args) {
    String s = "tutorial from java2s.com";
    try {//from w w w . ja va2 s .  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:Main.java

public static void main(String[] args) {
    String s = "tutorial from java2s.com";
    try {/*from ww w.  j av a  2s . c o  m*/

        PrintWriter pw = new PrintWriter(System.out);

        // write substrings
        pw.write(s, 0, 5);
        pw.write(s, 6, 5);

        // flush the writer
        pw.flush();

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

From source file:Main.java

public static void main(String[] args) {
    CharSequence sq1 = "Hello";
    CharSequence sq2 = " World";

    PrintWriter pw = new PrintWriter(System.out);

    // append the sequence
    pw.append(sq1, 1, 3);/*ww  w  . j av a 2 s .c  o  m*/
    pw.append(sq2, 1, 4);

    // flush the writer
    pw.flush();

}

From source file:Main.java

public static void main(String[] args) {
    CharSequence sq1 = "Hello";
    CharSequence sq2 = " World";

    PrintWriter pw = new PrintWriter(System.out);

    // append the sequence
    pw.append(sq1);//www .ja  va2s  .c om
    pw.append(sq2);

    // flush the writer
    pw.flush();

}

From source file:Main.java

public static void main(String[] args) {
    String s = "tutorial from java2s.com.";
    try {/*from w w w.  j av a  2  s  .  c  o m*/

        PrintWriter pw = new PrintWriter(System.out);

        // print string
        pw.print(s);

        // change the line
        pw.println();

        // print another string
        pw.print("This is an example.");

        // flush the writer
        pw.flush();

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

From source file:Main.java

public static void main(String[] args) {
    char[] c = { 'j', 'a', 'v', 'a', '2', 's', '.', 'c', 'o', 'm' };

    Writer writer = new PrintWriter(System.out);

    try {//w w  w  . j av  a  2  s. com
        // write a portion of a char array
        writer.write(c, 0, 5);

        // flush the writer
        writer.flush();

        // write another portion of a char array
        writer.write(c, 5, 5);

        // flush the stream again
        writer.close();

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