Show Mobile Navigation

Webring

Powered by WebRing.

Handling input/output im Java using io packeage.

Sachin R Kukale - 02:13
You may be required to read the data from input device or to write the data to output device. Input or output device will be treated as resources and they are managed by OS. So to interact with these devices we need to interact with OS.
We can also interact with input and output devices using the drivers but in that case you have to use the low level implementation which will platform as well as device independent and will lead to code maintenance problem as well.
To solve this problem java vendor has provided a package called io package for which implementation is provided by java vendor itself. The java.io package contains many classes and implementations that can be used effectively to interact with io devices. Also if you want to change the corresponding read or write device then you just have change the object related and all the process will be same.
JVM is started when we write ‘java...’ in consol and a object will be created by JVM to manage the io operations. The reference of this object will be stored in reference variable identified in System class.
Some references defined in system class to perform io operations are:
‘in’ –is reference which will specify the default input device.
‘out/err’ – will be the references for default output device.
You can change these devices by using the appropriate set method from system class if need arises.
While performing the io operations you can use the data as either as byte type or as character type.
The sequence of bits is called as ‘stream’ and we can either read or write the data in streams.
There are two types of stream depending upon the operation:
Input Stream and Output Stream.
And two types of stream depending on the type of data:
Byte stream and Character stream.
*      ByteInputStream: You can read the data from input using this stream as sequence of bytes.
·         java.io.InputStream: This is super class for err ByteInoutStream classes.
·         CharacterInputStream: By using this stream you can read the data from input devices as sequence of characters.
·         java.io.reader: This is super class for all character input stream classes.
To read the data from buffer you can use BufferedInputStream.
*    ByteOutputStream: This stream is used to write the data to the output devices from application. When you are using this stream you are sending the data as byte type.
·         java.io.OutputStream:  This is super class for character output stream classes.
·         CharacterOutputStream: Using this stream you can write the data to the output device as sequence of characters.
·         java.io.writer: This is super class for all character stream output classes.
Ø  All super classes in this package are abstract classes meaning that these superclasses do not have their own implementations.
Ø  There are other various sub classes which can be used for io operation.
Ø  Between input stream and reader class there is no inheritance relationship and you can use reader object to convert input stream into reader, if you have object of input stream. java.io.InputStreamReader class is subclass of reader and has constructor as input stream parameter.
Ø  In the same we you must remember that there is no inheritance relationship between output stream and writer class.
Ø  You can use the following class to convert output stream object into writer object:
java.io.OutptuStreamWriter clas.
Ø  While using the writer object for sending to the output device you need to invoke the flush() method to send the data to the output device.
You need to specify the device from where you are going to access the data.
You can use FileInputStream or FileReader to read the data from file:
FileInputStream(file f)
FileInoutStream(String fName)
File_Reader(file f)
File_Reader(String fname)
In the same way FileOutputStream and FileWriterClass are used to write the data to the file:
FileOutputStream (file f)
FileOutputStream (String fName)
FileOutputStream (file f,Boolean Operand)
FileOutputStream (String fName,Boolean operand)
FileWriter(file f)
FileWriter(String fName)
FileWriter(file f,Boolean operand)
To perform the operations on the file you can use java.io.file class.
You can also create constructor by using the following constructors:
public File(String fName)
public File(String parentDir,String fName)
public File(File parentDir,String fName)
Operations:
        i.            To create new file: public boolean CreateNewFile()
      ii.            To create one directory: public boolean mkdir()
    iii.            To create directories and sub-directories: public boolean mkdirs()
     iv.            To verify the file or directory: public boolean isFile()/public Boolean isDirectory()
       v.            To verify hidden file: public boolean isHidden()
     vi.            To get last modified information: public long last modified()
   vii.            To set up last modifier information: public void lastModified(long val)
 viii.            To verify file is readable or not: public boolean canRead()

     ix.            To verify file is writable or not: public boolean canWrite()

0 comments:

Post a Comment