Export (0) Print
Expand All

PrintWriter Class

Visual Studio 2005

Provides an implementation of the abstract Writer class specific to printing various primitive types and other objects to an underlying output stream.

Package: java.io

Assembly: vjslib (in vjslib.dll)


public class java.io.PrintWriter
    extends java.io.Writer

The following example demonstrates the checkError, close, flush, and println methods of the PrintWriter class.

// printwriter_overview.jsl

import java.io.*;
import java.util.*;

public class Program
{
    public static void main(String[] args)
    {
        // Create a new instance of a PrintWriter object along with
        // an underlying StringWriter object.
        StringWriter stringWriter = new StringWriter();
        PrintWriter printWriter = new PrintWriter(stringWriter);

        // Print some random data to the StringWriter.
        Date date = new Date();
        Random rand = new Random(date.getTime());

        printWriter.println(rand.nextInt());
        printWriter.println(rand.nextLong());
        printWriter.println(rand.nextFloat());
        printWriter.println(rand.nextDouble());

        // Flush the buffer.
        printWriter.flush();

        // Display the contents of the StringWriter.
        if (!printWriter.checkError())
        {
            System.out.println(stringWriter.getBuffer());
        }

        // Close the PrintWriter and String Writer objects.
        stringWriter.close();
        printWriter.close();
    }
}

/*
Output:
919498402
6438634853843200856
0.220032334
0.07553678009202236
*/

java.lang.Object

   java.io.Writer

    java.io.PrintWriter

Community Additions

ADD
Show:
© 2014 Microsoft