This tutorial aims to present the package java.io describing the different classes within them and by specifying when and how to use them. By going through this tutorial you will explore classes in java.io more clearly. This tutorial is about the basic java I/O classes and one can implement java.io package more than this.
Introduction
An input / output in Java is to exchange data between the program and another source that can be like, memory, a file or the program itself. To achieve this, Java employs what is called a stream (i.e flow of data) between the source and destination data. Any transaction input / output in Java follows the following pattern:
- Opening a stream
- Reading or writing data
- Close the streams
The java.io package provides all the necessary classes to create, read and write the data, from the destination to the source.
The java.io package
There are several kinds of flows that can be classified according to several criteria
- There are inflows and outflows
- The flow of characters and the binary stream (because as Java uses uni-code so characters are coded 2 bytes, not one)
- The communication flow and process flow
- The flows with or without buffer
In this tutorial first the Bytestream flow of communication is explained and then the Character flow of communication is explained.
A] The Byte Streams
The bytestreams are used, as the name implies to handle bytes, that is to say that is exchanged between the bytes program and another entity outside it. Such flow can be used to load images into memory or to save them to disk, they are also used for register objects (a process called serialization) or loading (de-serialization).
The bytestreams are derived from two main abstract classes:
- InputStream : for the input stream.
- OutputStream : to the output stream.
Both classes are abstract. Mostly these classes are not directly but with their subclasses. Note the presence of a number of methods, namely the different methods write for OutputStream and different methods read for InputStream.
A-1] The Input Stream
The InputStream can be classified into two categories
- The flow of communication (data) is focused on creation between the program and the another entity.
- The flow of treatment is the process of data exchange between two nodes.
Here are some of the classes most commonly used for InputStream
A-1.a] FileInputStream
It creates a stream with a file from the file system. This class has a Constructor taking as parameter an object of type File and a another taking a String as a parameter, which represents the path to the file. Here is some code to load the binary content of a file and display it on standard output (that is the console)
A-1.b] ByteArrayInputStreamCode:import java.io. *; Public class TFileInputStream { Public static void hand(String [] args){ FileInputStream flinstr; int bt; try{ //creation a flow Input with for source a file appointed //source, this instantiation may remove a except of type //FileNotFound flinstr=new FileInputStream("source"); //the method read () returns a int representative byte read the worth (-1) //represents the end of file. // read may remove a except of type IOException try{ while((bt=flinstr.read())!= -1){ System.out.System.out.println(bt); } } //Do not forget of close the flow to of RELEASE the Resources it //uses finally{ flinstr.close(); } } catch(FileNotFoundException ef){ System.out.System.out.println("file Not Found"); } catch(IOException e){ System.out.System.out.println(e+"mistake at of the reading of file"); } } }
It can read binary data from a byte array. The source of this flow is the program itself. Here is a sample code illustrating its use.
A-1.c] BufferedInputStreamCode:import java.io. *; Public class TestByteArrayInputStream { Public static void hnd(String [] args){ ByteArrayInputStream bis; int bty; byte[] buff=new byte[10]; //filling of buffer for(byte i=0, i<10, i++) { buffer [i]=i; } //creation a flow Input with for source a board of bytes, here //buffer bis=new ByteArrayInputStream(buffer); //reading of flow. // note that the values returned are of type int and no of type byte while((bty=bis.read())!=-1) { System.out.System.out.println(bty); } } }
This class allows reading of data using a buffer. When instantiated, a table of bytes is created to serve as a buffer. This table is automatically re-sized to each reading to contain data from the input stream. Here is the same code for it.
A-1.d] DataInputStreamCode:import java.io. *; Public class TestBufferedInputStream { Public static void hand(String [] args){ BufferedInputStream bufinstr; int byt; Byte [] buffer; try{ //creation a BufferedInputStream on a InputStream with for //source a file appointed source bufinstr=new BufferedInputStream(new FileInputStream("source")); //the method available Permet of know the number of bytes who //may Must be a read a way no blocking. System.out.System.out.println(bufinstr.available()); //reading of data System.out.System.out.println("start reading"); try{ long chr=System.currentTimeMillis(); while((byt=bufinstr.read())!=-1); System.out.System.out.println("end reading"); System.out.System.out.println("duration ="+(System.out.printlncurrentTimeMillis()-chr)); } finally{ //not not forget of close the flow bufinstr.close(); } }catch(FileNotFoundException ef){ System.out.System.out.println("file Not Found"); } catch(IOException e){ System.out.System.out.println(e); } } }
This is used to read data representing the types Java primitives (int, boolean, double, byte etc) Thus, this class has methods like readInt (), readBoolean () etc.
A-1.e] SequenceInputStream
It can concatenate two or more InputStream. The reading is sequential, beginning with the first and passing the following as soon as we reached the end of the current stream, while calling its method close ().
A-1.f] ObjectInputStream
It can "serialize" an object, that is to say, restore an object previously saved using a ObjectOutputStream. The serialization will view more details when addressing the class ObjectOutputStream.
Bookmarks