Example usage for java.io PrintWriter close

List of usage examples for java.io PrintWriter close

Introduction

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

Prototype

public void close() 

Source Link

Document

Closes the stream and releases any system resources associated with it.

Usage

From source file:DataFileTest.java

public static void main(String[] args) {
    Employee staff = new Employee("Java Source", 35500);

    staff.raiseSalary(5.25);/*from w w  w . j  a  va 2 s.c o  m*/

    try {
        PrintWriter out = new PrintWriter(new FileWriter("employee.dat"));
        writeData(staff, out);
        out.close();
    } catch (IOException e) {
        System.out.print("Error: " + e);
        System.exit(1);
    }

    try {
        BufferedReader in = new BufferedReader(new FileReader("employee.dat"));
        Employee e = readData(in);
        e.print();
        in.close();
    } catch (IOException e) {
        System.out.print("Error: " + e);
        System.exit(1);
    }
}

From source file:Reverse.java

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

    if (args.length != 1) {
        System.err.println("Usage:  java Reverse " + "string_to_reverse");
        System.exit(1);/*from  w  ww  .j a  v a2 s.c o  m*/
    }

    String stringToReverse = URLEncoder.encode(args[0]);

    URL url = new URL("http://java.sun.com/cgi-bin/backwards");
    URLConnection connection = url.openConnection();
    connection.setDoOutput(true);

    PrintWriter out = new PrintWriter(connection.getOutputStream());
    out.println("string=" + stringToReverse);
    out.close();

    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String inputLine;

    while ((inputLine = in.readLine()) != null)
        System.out.println(inputLine);

    in.close();
}

From source file:InputOutputDemo.java

public static void main(String[] a) throws Exception {
    PrintWriter pwr = new PrintWriter(new FileWriter("java2s.txt"));
    pwr.print(4711);//from w w  w  . ja va 2  s. 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:ThreadLister.java

/**
 * The main() method create a simple graphical user interface to display the
 * threads in. This allows us to see the "event dispatch thread" used by AWT
 * and Swing.//from  w ww. j a  v a 2s .com
 */
public static void main(String[] args) {
    // Create a simple Swing GUI
    JFrame frame = new JFrame("ThreadLister Demo");
    JTextArea textarea = new JTextArea();
    frame.getContentPane().add(new JScrollPane(textarea), BorderLayout.CENTER);
    frame.setSize(500, 400);
    frame.setVisible(true);

    // Get the threadlisting as a string using a StringWriter stream
    StringWriter sout = new StringWriter(); // To capture the listing
    PrintWriter out = new PrintWriter(sout);
    ThreadLister.listAllThreads(out); // List threads to stream
    out.close();
    String threadListing = sout.toString(); // Get listing as a string

    // Finally, display the thread listing in the GUI
    textarea.setText(threadListing);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    System.out.println(stdin.readLine());
    BufferedReader in = new BufferedReader(new FileReader("Main.java"));
    String s, s2 = new String();
    while ((s = in.readLine()) != null)
        s2 += s + "\n";
    in.close();/*from   ww w  .  j  a v  a  2 s.c  o m*/
    StringReader in1 = new StringReader(s2);
    int c;
    while ((c = in1.read()) != -1)
        System.out.print((char) c);
    BufferedReader in2 = new BufferedReader(new StringReader(s2));
    PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("IODemo.out")));
    int lineCount = 1;
    while ((s = in2.readLine()) != null)
        out1.println(lineCount++ + ": " + s);
    out1.close();
}

From source file:TextFileTest.java

public static void main(String[] args) {
    Employee[] staff = new Employee[3];

    staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15);
    staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
    staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);

    try {//from w  w  w.  j  a  v a  2  s. co  m
        // save all employee records to the file employee.dat
        PrintWriter out = new PrintWriter("employee.dat");
        writeData(staff, out);
        out.close();

        // retrieve all records into a new array
        Scanner in = new Scanner(new FileReader("employee.dat"));
        Employee[] newStaff = readData(in);
        in.close();

        // print the newly read employee records
        for (Employee e : newStaff)
            System.out.println(e);
    } catch (IOException exception) {
        exception.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {
    try {//from  w  w w . j ava  2s . c  om
        BufferedReader input = new BufferedReader(new FileReader(args[0]));
        ArrayList list = new ArrayList();
        String line;
        while ((line = input.readLine()) != null) {
            list.add(line);
        }
        input.close();

        Collections.reverse(list);

        PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter(args[1])));
        for (Iterator i = list.iterator(); i.hasNext();) {
            output.println((String) i.next());
        }
        output.close();
    } catch (IOException e) {
        System.err.println(e);
    }
}

From source file:MainClass.java

License:asdf

public static void main(String[] args) throws IOException {
    try {/*from  ww  w  .ja  v  a  2s  .  c o m*/
        BufferedReader in4 = new BufferedReader(new StringReader("asdf"));
        PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("IODemo.out")));
        int lineCount = 1;
        String s = null;
        while ((s = in4.readLine()) != null)
            out1.println(lineCount++ + ": " + s);
        out1.close();
    } catch (EOFException e) {
        System.err.println("End of stream");
    }

}

From source file:Random4.java

public static void main(String[] argv) throws IOException {
    // java.util.Random methods are non-static, do need to construct Math
    Random r = new Random();
    PrintWriter file1 = new PrintWriter(new FileWriter("file1"));
    PrintWriter file2 = new PrintWriter(new FileWriter("file2"));
    for (int i = 0; i < 10000; i++) {
        file1.println(r.nextDouble());//from w w w  .  j a va2 s . c  o m
        file2.println(r.nextGaussian());
    }
    file1.close();
    file2.close();
}

From source file:MainClass.java

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

    FileWriter fw = new FileWriter(args[0]);
    BufferedWriter bw = new BufferedWriter(fw);
    PrintWriter pw = new PrintWriter(bw, false);

    pw.println(true);//w w w.j a  va  2s  .  com
    pw.println('A');
    pw.println(500);
    pw.println(40000L);
    pw.println(45.67f);
    pw.println(45.67);
    pw.println("Hello");
    pw.println(new Integer("99"));

    pw.close();
}