Sunday, June 5, 2011

Stream, Reader, dan Writer

Stream, Reader, dan Writer

Computer programs can be useful if he could interact with other worlds. Interaction here means input / output or I / O. In this chapter, we will see the input / output on files and network connections (network). In Java, the input / output on the file and the network is based on the flow (stream), where all objects can do the I / O is the same. Standard output (System.out) and standard input (System.in) is an example of the flow.

To work with files and networks, we need knowledge about the exception, which has been discussed previously. Many subroutines are used to working with I / O throw an exception that must be addressed. This means that the subroutine must be called within the try statement ... so catch exceptions that occur can be handled properly.

Without being able to interact with the other world, a program is useless. Interaction of a program with other world is often called input / output or I / O. From the first, one of the biggest challenges for designing a new programming language is to prepare the facility to input and output. Computers can connect with various types of input and output of various devices. If the programming language must be created specifically for each type of device, the complexity will no longer be handled.

One of the biggest advances in the history of programming is the concept (or abstraction) to model the device I / O. In Java, abstraction is called the flow (stream). This section will introduce the flow, but does not explain the complete. For more details, please refer to official documents Java.

When dealing with input / output, we must remember that there are two general categories of data: data created by the machine and human readable data. The data that made the engine written with the same model with how data is stored in the computer, namely a series of zeros and ones. Human-readable data is data in the form of a series of letters. When we read a number is 3.13159, we read it as a series of letters which we translate as numbers. This number will be written in a computer as a series of bits that we do not understand.

To deal with both types of data, Java has two broad categories for streams: byte streams for machine data (byte stream), and flow character (character stream) for human-readable data. There are many classes derived from both these categories.

Any object that issued the data to a stream of bytes into a class derived from abstract class OutputStream. Objects that read data from a byte stream derived from the abstract class InputStream. If we write numbers to an OutputStream, we will not be able to read the data because it is written in machine language. However, these data can be read back by the InputStream. The process of reading and writing data will be very efficient, since there is no translation to be done: bits used to store data in computer memory is only copied from and to the flow.

To read and write character data that can be understood by humans, the main classes are Reader and Writer. All character stream classes are subclasses of one of these abstract classes. If a number Writer will be written in the stream, the computer must be able to translate it into a series of characters that can be read maunsia.

Read numbers from the Reader stream into a numeric variable must also be translated, from the sequence of characters into a series of bits that computers understand. (Although for data consisting of characters, like the text editor, there will be some translation done. Characters are stored in a computer in 16-bit Unicode value. For people who use the ordinary alphabet, character data is usually stored in files in ASCII code, that only uses 8-bit. Class Reader and Writer will handle the change from 16-bit to 8-bit and vice versa, and also handle other alphabets used in other countries.)

It is easy to determine whether we should use a byte stream or character stream. If we want the data we read / write for the human readable, so we use the character stream. If not, use a byte stream. System.in and System.out is actually a stream of bytes and not a character stream, hence can handle inputs in addition to the alphabet, for example the enter key, arrows, escape, etc..

Standard flow class that will be discussed later defined in the java.io package along with several other auxiliary classes. We have to import classes from this package if you want to use them in our program. This means that by using the "import java.io. *" at the beginning of our source code.

The flow is not used in the GUI, because the GUI has a flow of I / O itself. However, these classes are used also for the files or communications in the network. Or it could also be used for communication between the threads that work simultaneously. And there is also a flow class that is used to read and write data to and from computer memory.

No comments:

Post a Comment

 
THANK YOU FOR VISITING