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:InputOutputDemo.java

public static void main(String[] a) throws Exception {
    PrintWriter pwr = new PrintWriter(new FileWriter("java2s.txt"));
    pwr.print(4711);/* ww  w  .  ja  va 2s. c  o  m*/
    pwr.print(' ');
    pwr.print("Java Source and Support at www.java2s.com");
    pwr.close();

    StreamTokenizer stok = new StreamTokenizer(new FileReader("java2s.txt"));
    int tok = stok.nextToken();
    while (tok != StreamTokenizer.TT_EOF) {
        System.out.println(stok.sval);
        tok = stok.nextToken();
    }
}

From source file:Main.java

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

        // create a new stream at specified file
        PrintWriter pw = new PrintWriter(System.out);

        // write the string in the file
        pw.write(s);

        // flush the writer
        pw.flush();

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

From source file:Main.java

public static void main(String[] args) {
    Object obj1 = "Object";
    Object obj2 = 2;//  w w w  . j a v a  2s. com
    Date date = new Date();
    try {
        PrintWriter pw = new PrintWriter(System.out);

        // print object
        pw.print(obj1);

        // print another object
        pw.print(obj2);

        // print a date (it is an object)
        pw.print(date);

        pw.flush();

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

From source file:Main.java

public static void main(String[] args) {
    Object obj1 = "Object";
    Object obj2 = 2;// w ww. j a va 2  s.c  o m
    Date date = new Date();
    try {

        PrintWriter pw = new PrintWriter(System.out);

        // print object
        pw.println(obj1);

        // print another object
        pw.println(obj2);

        // print a date (it is an object)
        pw.print(date);

        // flush the writer
        pw.flush();

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

From source file:MainClass.java

public static void main(String[] args) {
    try {/*from www  . j  a  v  a 2  s . c o  m*/
        FileWriter fw = new FileWriter("LPT1:");

        PrintWriter pw = new PrintWriter(fw);
        String s = "www.java2s.com";

        int i, len = s.length();

        for (i = 0; len > 80; i += 80) {
            pw.print(s.substring(i, i + 80));
            pw.print("\r\n");
            len -= 80;
        }

        if (len > 0) {
            pw.print(s.substring(i));
            pw.print("\r\n");
        }

        pw.close();
    } catch (IOException e) {
        System.out.println(e);
    }
}

From source file:Main.java

public static void main(String args[]) {
    try {//from  w w w  .j  av  a 2  s.  c  om
        throw new Exception("for no reason!");
    } catch (Exception e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        System.out.println(sw.toString().toUpperCase());
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    FileWriter file = new FileWriter("a.html");
    BufferedWriter buffer = new BufferedWriter(file);
    PrintWriter out = new PrintWriter(buffer);
    out.println("<html>\n\t<body>");
    out.println("\t</body>\n</html>");
    out.close();//from   ww  w  . j  av  a  2s.co  m
    buffer.close();
    file.close();
}

From source file:Main.java

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

    PrintWriter pw = new PrintWriter(new FileWriter("logfile.txt", true));
    timeStamp("File opened", pw);
    pw.close();//  www  . j a va 2  s.c  o  m
    if (pw.checkError())
        System.out.println("I/O error occurred.");
}

From source file:MainClass.java

public static void main(String[] a) throws Exception {

    URL url = new URL("http://www.yourdomain.com/form.jsp");
    URLConnection connection = url.openConnection();
    connection.setDoOutput(true);//  ww w . j a v  a  2 s  . c om
    PrintWriter out = new PrintWriter(connection.getOutputStream());
    out.println("firstName=Joe");
    out.println("lastName=Average");
    out.close();

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String filename = "fileName.txt";
    String[] linesToWrite = new String[] { "a", "b" };
    boolean appendToFile = true;

    PrintWriter pw = null;/*from  www  .j a va  2s.c o  m*/
    if (appendToFile) {
        pw = new PrintWriter(new FileWriter(filename, true));
    } else {
        pw = new PrintWriter(new FileWriter(filename));
        // pw = new PrintWriter(new FileWriter(filename, false));
    }
    for (int i = 0; i < linesToWrite.length; i++) {
        pw.println(linesToWrite[i]);
    }
    pw.flush();
    pw.close();
}