Wednesday, February 3, 2016

Topic 35: Input-Output in Java

  • The java.io package supports Java’s basic I/O (input/output) system, including file I/O.

  • Java programs perform I/O through streams. 

A stream is an abstraction that either produces or consumes information. A stream is linked to a physical device by the Java I/O system. All streams behave in the same manner, even if the actual physical devices to which they are linked differ. Thus, the same I/O classes and methods can be applied to any type of device. 


  • Java defines two types of streams: byte and character. 

    Byte streams are used for handling input and output of bytes. Byte streams are used, for example, when reading or writing binary data. 

    Character streams are used for handling input and output of characters. They use Unicode and, therefore, can be internationalized.

At the lowest level, all I/O is still byte-oriented. The character-based streams simply provide a convenient and efficient means for handling characters.


  • Byte streams are defined by using two class hierarchies. At the top are two abstract classes: InputStream and OutputStream. Each of these abstract classes has several concrete subclasses that handle the differences between various devices, such as disk files, network connections, and even memory buffers. Two important methods read( ) and write( ), which, respectively, read and write bytes of data.

  • Character streams are defined by using two class hierarchies. At the top are two abstract classes, Reader and Writer. These abstract classes handle Unicode character streams. Two important methods are read( ) and write( ), which read and write characters of data.

  • Java programs automatically import the java.lang package. This package defines a class called System, which encapsulates several aspects of the run-time environment. For example, using some of its methods, we can obtain the current time and the settings of various properties associated with the system. System also contains three predefined stream variables: in, out, and err. These fields are declared as public, static, and final within System.  

    System.out refers to the standard output stream. By default, this is the console. System.in refers to standard input, which is the keyboard by default. System.err refers to the standard error stream, which also is the console by default. However, these streams may be redirected to any compatible I/O device.

System.in is an object of type InputStream; System.out and System.err are objects of type PrintStream. These are byte streams, even though they typically are used to read and write characters from and to the console.


  • The preferred method of reading console input is to use a character-oriented stream, which makes your program easier to internationalize and maintain.

    To obtain a character- based stream that is attached to the console, wrap System.in in a BufferedReader object. BufferedReader supports a buffered input stream. Its most commonly used constructor is shown here:

BufferedReader(Reader inputReader)

Here, inputReader is the stream that is linked to the instance of BufferedReader that is being created. Reader is an abstract class. One of its concrete subclasses is InputStreamReader, which converts bytes to characters. To obtain an InputStreamReader object that is linked to System.in, use the following constructor:

InputStreamReader(InputStream inputStream)

Because System.in refers to an object of type InputStream, it can be used for inputStream. Putting it all together, the following line of code creates a BufferedReader that is connected to the keyboard:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

After this statement executes, br is a character-based stream that is linked to the console through System.in.

  • To read a character from a BufferedReader, use read( ).

int read( ) throws IOException

Each time that read( ) is called, it reads a character from the input stream and returns it as an integer value. It returns -1 when the end of the stream is encountered. It can throw an IOException.

The following program demonstrates read( ) by reading characters from the console until the user types a "q.” Notice that any I/O exceptions that might be generated are simply thrown out of main( ). Such an approach is common when reading from the console, but we can handle these types of errors.

// Use a BufferedReader to read characters from the console.

import java.io.*;
class BRRead
{
public static void main(String args[]) throws IOException
{
char c;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter characters, 'q' to quit.");
// read characters
do
{
c = (char) br.read();
System.out.println(c);
} while(c != 'q');
}
}

System.in is line buffered, by default. This means that no input is actually passed to the program until we press ENTER. This does not make read( ) particularly valuable for interactive console input.

  • To read a string from the keyboard, use readLine( ) that is a member of the BufferedReader class. Its general form is shown here:

String readLine( ) throws IOException

Sample code fragment:


//Create a BufferedReader using System.in
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
System.out.println("Enter lines of text.");
System.out.println("Enter 'stop' to quit.");
do
{
str = br.readLine();
System.out.println(str);
} while(!str.equals("stop"));

  • Console output is most easily accomplished with print( ) and println( ) . These methods are defined by the class PrintStream (which is the type of object referenced by System.out). Because PrintStream is an output stream derived from OutputStream, it also implements the low-level method write( ). Thus, write( ) can be used to write to the console. The simplest form of write( ) defined by PrintStream is shown here:

void write(int byteval)

This method writes to the stream the byte specified by byteval. Although byteval is declared as an integer, only the low-order eight bits are written.

// Demonstrate System.out.write().
int b = 'A';
System.out.write(b + '\n');

We will not often use write( ) to perform console output because print( ) and println( ) are substantially easier to use.

  • Although using System.out to write to the console is acceptable, its use is recommended mostly for debugging purposes. For real-world programs, the recommended method of writing to the console when using Java is through a PrintWriter stream. PrintWriter is one of the character-based class for console output and makes it easier to internationalize our program. PrintWriter defines several constructors. The one we will use is shown here:

PrintWriter(OutputStream outputStream, boolean flushOnNewline)

Here, outputStream is an object of type OutputStream, and flushOnNewline controls whether Java flushes the output stream every time a println( ) method is called. If flushOnNewline is true, flushing automatically takes place. If false, flushing is not automatic.


PrintWriter supports the print( ) and println( ) methods for all types including Object. If an argument is not a simple type, the PrintWriter methods call the object’s toString( ) method and then print the result.


To write to the console by using a PrintWriter, specify System.out for the output stream and flush the stream after each newline. For example, this line of code creates a PrintWriter that is connected to console output:

PrintWriter pw = new PrintWriter(System.out, true);

// Demonstrate PrintWriter

PrintWriter pw = new PrintWriter(System.out, true);
int i = -7;
double d = 4.5e-7;
pw.println("This is a string");
pw.println(i);
pw.println(d);

  • In Java, all files are byte-oriented, and Java provides methods to read and write bytes from and to a file. However, Java allows us to wrap a byte-oriented file stream within a character-based object.
    Two of the most often-used stream classes are FileInputStream and FileOutputStream, which create byte streams linked to files. To open a file, we simply create an object of one of these classes, specifying the name of the file as an argument to the constructor. While both classes support additional, overridden constructors, the following are the forms that we will be using:

FileInputStream(String fileName) throws FileNotFoundException

FileOutputStream(String fileName) throws FileNotFoundException

Here, fileName specifies the name of the file that we want to open. When we create an input stream, if the file does not exist and for output streams, if the file cannot be created, then FileNotFoundException is thrown. When an output file is opened, any preexisting file by the same name is destroyed.


When we are done with a file, we should close it by calling close( ). It is defined by both FileInputStream and FileOutputStream, as shown here:


void close( ) throws IOException

To read from a file, we can use a version of read( ) that is defined within FileInputStream. The one that we will use is shown here:

int read( ) throws IOException

Each time that it is called, it reads a single byte from the file and returns the byte as an integer value. read( ) returns -1 when the end of the file is encountered. It can throw an IOException.

/* Display a text file. To see a file called TEST.TXT, use the following commandline. java ShowFile TEST.TXT */
import java.io.*;
//Inside main()
int i;
FileInputStream fin;
try
{
fin = new FileInputStream(args[0]);
}
catch(FileNotFoundException e)
{
System.out.println("File Not Found");
return;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Usage: ShowFile File");
return;
}
// read characters until EOF(-1) is encountered
do
{
i = fin.read();
if(i != -1)
System.out.print((char) i);
} while(i != -1);
fin.close();

  • To write to a file, we can use the write( ) method defined by FileOutputStream. Its simplest form is shown here: 

    void write(int byteval) throws IOException

This method writes the byte specified by byteval to the file. Although byteval is declared as an integer, only the low-order eight bits are written to the file. If an error occurs during writing, an IOException is thrown. 

 

Click for NEXT article.

 

Please feel free to correct me by commenting your suggestions and feedback.

No comments:

Post a Comment