What Is Printstream in Java?

What Is Printstream in Java? thumbnail
Java input and output classes read and write different types of data.

The PrintStream class is part of the input and output resource library within the Java language. The PrintStream class provides data output functionality in conjunction with other output objects. Developers can create objects of the PrintStream class within their applications, calling methods on these objects to produce data. The PrintStream class is appropriate in a specific set of cases, with a dedicated set of constructors and methods providing access to its use.

  1. Importing

    • To make use of the PrintStream class, applications must import Java's main package for input and output operations. The following code demonstrates importing the package for the PrintStream class:

      import java.io.*;

      Developers need to include this line at the top of any class file in which they wish to use the PrintStream class. If a file contains references to the PrintStream class but does not use this import statement, the program will fail to compile and run correctly. When programming in an Integrated Development Environment for Java, developers will see error messages until they add the correct import statement.

    Creation

    • To create objects of the PrintStream class, developers can choose from a number of constructor methods. The following sample code demonstrates one of them:

      PrintStream myPrintStream = new PrintStream(myOutputStream);

      This code uses the constructor method of the PrintStream class that accepts an OutputStream object. The use of the "new" keyword causes the constructor method to execute, returning an instance of the class, which the code stores using a variable reference. Other versions of the PrintStream constructor method take file and string type parameters, with the strings representing file names.

    Methods

    • The PrintStream class provides a range of output methods. The append method adds a character to the current stream as follows:

      myPrintStream.append('a');

      The write methods allow programmers to add bytes to the stream, while the format methods allow data to be formatted prior to output. The print methods take various different parameter types, including strings, characters, booleans, objects and number types. The following sample code demonstrates calling the print method with a string parameter:

      myPrintStream.print("Hello");

    Considerations

    • The PrintStream class, like many of Java's input and output classes, does not provide output functionality on its own, but rather works in conjunction with other classes in the package. The PrintStream class converts its output to bytes, which is not always suited to the needs of particular programs. Many of the output classes in Java throw Input Output exceptions when something goes wrong with the output process. However the PrintStream class does not do this, reducing the amount of exception handling code programmers need to implement when using the class.

Related Searches:

References

Resources

  • Photo Credit Thinkstock Images/Comstock/Getty Images

Comments

Related Ads

Featured