Java. I/O system. Streams. Byte streams. Character streams. Standard streams

Java I/O system. Streams. Byte streams. Character streams. Standard streams


Contents


Search other websites:




1. General concepts of Java I/O. Stream. Stream definition

In the Java programming language, input/output of information is based on the concept of stream. A stream is an abstract concept that symbolizes a source or receiver of data that can pass or receive some information. Any stream hides operations on data that are performed at lower levels directly in the input/output devices.

According to the purpose of the streams, in the Java language classes are also classified. Some classes implement input operations, while others implement output operations. To use I/O stream classes you need to import the java.io package

import java.io.*;

Java class objects that are used for I/O are layered to provide the necessary functionality. Such a model of interaction of objects is supported in the Decorator pattern. In this pattern, when creating a stream, you need to use several objects.

 

2. Types of streams in Java

In Java, there are two types of streams:

  • byte streams. This is an analogue of binary data streams that allow compact storage of information;
  • character streams. These are streams that are presented in a convenient way (for people) to encode information in the form of understandable text characters. In many programming languages, character streams are associated with a text format for representing information.

 

3. Byte I/O streams. Overview of byte stream classes. Classes InputStream, OutputStream

Byte I/O streams are implemented at the lowest level (as opposed to character streams). There are a number of classes in the Java language that provide I/O byte streams. All byte stream classes are divided into two hierarchies. At the top of one hierarchy (corresponds to the input) is the class InputStream. At the top of the second hierarchy (corresponding to the output) is the class OutputStream.

Classes that implement byte input streams are inherited from the abstract class InputStream:

  • InputStream is an abstract class that describes an input stream. This class is the base for all other classes of the input system;
  • BufferedInputStream – a class that describes a buffered input stream;
  • ByteArrayInputStream – a class that describes an input stream that reads bytes from an array;
  • DataInputStream – a class that implements methods for reading data of standard types defined in Java (int, double, float, etc.);
  • FileInputStream – a class that implements an input stream that reads data from a file;
  • FilterInputStream is an implementation of the abstract class InputStream;
  • ObjectInputStream – a class that implements the input stream of objects;
  • PipedInputStream – a class corresponding to the input channel;
  • PushbackInputStream – a class corresponding to the input stream that supports the return of one byte back to the input stream;
  • SequenceInputStream – a class that implements an input stream consisting of two or more input streams, data from which are read in turn.

Classes that implement byte output streams are inherited from the abstract class OutputStream:

  • OutputStream is an abstract class that describes the output stream. All other classes of the output system are subclasses of the class OutputStream;
  • BufferedOutputStream – a class that implements a buffered output stream;
  • ByteArrayOutputStream – a class that implements an output stream that writes bytes to an array;
  • DataOutputStream – a class that implements an output stream containing methods for reading data of standard types defined in Java (int, float, double, etc.);
  • FileOutputStream – a class that corresponds to the output stream writing data to a file;
  • FilterOutputStream – a class that implements the abstract class OutputStream;
  • ObjectOutputStream – a class corresponding to the output stream of objects;
  • PipedOutputStream – the class that is associated with the output channel;
  • PrintStream is a class that represents an output stream containing the print() and println() methods.

 

4. Character I/O streams. Overview of classes of character streams

Classes that are designed to describe character streams are divided into two types:

  • input stream classes. These classes are inherited from the abstract class Reader;
  • output stream classes that inherit from the abstract Writer class.

The classes of input streams are as follows:

  • Reader is an abstract class that describes a character input stream. This class is a superclass for all of the following subclasses;
  • BufferedReader – a class that describes a buffered character input stream;
  • CharArrayReader – a class that implements an input stream that reads characters from an array;
  • FileReader – a class that describes the input stream associated with a character file;
  • FilterReader – a class representing a filtered read stream;
  • InputStreamReader – a class that represents an input stream that converts bytes into characters;
  • LineNumberReader – a class corresponding to an input stream that counts lines;
  • PipedReader – a class that is associated with the input channel;
  • PushbackReader – a class corresponding to the input stream, which allows you to return characters back to the input stream;
  • StringReader is a class that implements an input stream that reads characters from a string.

The classes of output streams are as follows:

  • Writer is an abstract class that describes the stream of character output. All of the following classes are subclasses of the Writer class;
  • BufferedWriter – a class that describes a buffered character output stream;
  • CharArrayWriter – a class that corresponds to an output stream writing characters to an array;
  • FileWriter – a class that corresponds to the output stream that writes characters to a file;
  • FilterWriter – a class that implements a filtered recording stream;
  • OutputStreamWriter – a class that implements the means of converting characters to bytes;
  • PipedWriter – the class that is associated with the output channel;
  • StringWriter is a class that implements an output stream that writes characters to a string.

 

5. Standard input/output streams from java.lang package

As you know, the java.lang package contains many tools for organizing work. This package is imported automatically (it is not necessary to use the import directive).
The base class defined in this package is the System class. The System class implements three variables that are associated with standard (obviously defined) I/O streams:

  • the System.in variable is a reference to the standard input stream to which the keyboard corresponds;
  • the System.out variable – a reference to the standard output stream to which the console corresponds;
  • the System.err variable – a reference to the standard error output stream. The console corresponds to this stream.

 

6. Standard input stream. Variable System.in. Features of use. Example

In Java, the standard input stream is represented by the variable System.in. This variable (object) is of type InputStrem. The InputStream class is abstract and is located at the top of the hierarchy of input classes (see point 3).

An example that uses the System.in variable. In the example, it is first proposed to enter a character from the standard input stream (keyboard), then this character is displayed on the screen.

import java.io.*;

...

public static void main(String[] args) throws IOException
{
  // Demo of using the System.in variable
  InputStream is = System.in; // get a reference to System.in

  // Get code from console
  System.out.println("Enter code:");
  int code = is.read(); // Get the code from the console

  // Print the code of the entered character
  System.out.println("You are entered character with code: " + code);
}

...

The result of the program

Enter code:
A
You are entered character with code: 65

 

7. Standard output stream. System.out variable. Features of use. Example

The standard output stream is associated with a System.out variable (object) whose type is PrintStream. The PrintStream class contains console output methods print() and println().

Example. The example demonstrates the redirection from the System.in stream to the System.out stream.

import java.io.*;

...

public static void main(String[] args) throws IOException
{
  // Using variables System.out and System.in
  InputStream input = System.in; // get a reference to System.in
  PrintStream output = System.out; // get a reference to System.out

  // The input cycle from System.in and the display
  // of the entered character in System.out.
  // The end of the input is the character '.'
  int endCode = (int)'.';
  int Code;

  // Print the information message
  output.print("Please, enter characters: ");

  do {
    // Get character from keyboard
    Code = input.read();

    // Display (duplicate) a character on the screen
    output.write(Code); // the write() method is used
  } while (Code!=endCode); // the loop runs until '.' is entered
}

...

The result of the program

Please, enter characters: a
a
b
b
0
0
Z
Z
.

 

8. Standard output stream. System.err variable. Features of use. Example

The System.err variable is of type PrintStream and is associated with the standard error output stream, which, by default, is a console. In general, working with System.err is no different from working with System.out.

Example. Demonstrates the use of a variable (object) System.err.

import java.io.*;

...

public static void main(String[] args) throws IOException
{
  // Using the System.err variable
  // The solution of the quadratic equation.
  double a, b, c, D, x1, x2;
  PrintStream error = System.err; // If there are errors, then messages will be displayed here
  Scanner input = new Scanner(System.in);

  System.out.println("Please, enter a, b, c: ");

  System.out.print("a = ");
  a = input.nextDouble();

  System.out.print("b = ");
  b = input.nextDouble();

  System.out.print("c = ");
  c = input.nextDouble();

  // Calculate the discriminant
  D = b*b - 4*a*c;

  // If the discriminant is negative
  if (D<0) {
    // Use System.err error console
    error.println("The equation has no solution."); // display errors on the console
  }
  else {
    x1 = (-b - Math.sqrt(D)) / (2*a);
    x2 = (-b + Math.sqrt(D)) / (2*a);
    System.out.println("x1 = " + x1);
    System.out.println("x2 = " + x2);
  }
}

...

The result of the program

Please, enter a, b, c:
a = 5
b = 5
c = 5
The equation has no solution.

 


Related topics