File Handling in Java


File Handling in java

This topic is also known as IOStream or Java IO. Java I/O (Input and Output) is used to process the input and produce the output.
Java uses the concept of a stream to make I/O operation fast. The java.io package contains all the classes required for input and output operations.


Some input-output stream will be initialized automatically by the JVM and these streams are available in System class as in, out, and err variable.

In refers to the default input device, i.e. keyboard. (System.in…)
Out and err refers to the default output device, i.e. console. (System.out…..)

Streams:

Streams are the sequence of bits (data).

Types of Streams on the basis of flow of data:

  • Input Streams
  • Output Streams

Input Streams: Input streams are used to read the data from various input devices like keyboard, file, network, etc.
Output Streams: Output streams are used to write the data to various output devices like monitor, file, network, etc.

Types of streams based on type of data:

Byte Stream: used to read or write byte data.
Character Stream: used to read or write character data.


Byte Input Stream

These are used to read byte data from various input devices.
InputStream is an abstract class and it is the super class of all the input byte streams.

List of Byte Input Streams:

ObjectInputStream, ByteArrayInputStream, InputStream, DataInputStream, BufferedInput Stream, FileInputStream.

Useful methods of InputStream

1) public abstract int read()throws IOException: It reads the next byte of data from the input stream. It returns -1 at the end of the file.
2) public int available()throws IOException: It returns an estimate of the number of bytes that can be read from the current input stream.
3) public void close()throws IOException: It is used to close the current input stream.

Byte Output Stream:

These are used to write byte data to various output devices.
OutputStream is an abstract class and it is the superclass for all the output byte streams.

List of Byte Output Streams:

ObjectOutputStream, ByteArrayOutputStream, OutputStream, DataOutputStream, BufferedOutputStream, FileOutputStream

Useful methods of OutputStream:

1) public void write(int)throws IOException: It is used to write a byte to the current output stream.
2) public void write(byte[])throws IOException: It is used to write an array of byte to the current output stream.
3) public void flush()throws IOException: It flushes the current output stream.
4) public void close()throws IOException: It is used to close the current output stream.

Character Input Stream:

These are used to read char data from various input devices.
Reader is an abstract class and is the super class for all the character input streams.
List of Character Input Streams:

Character Output Stream: 

These are used to write char data to various output devices.
Writer is an abstract class and is the super class of all the character output streams.

Example to read contents of file using File Input Stream:


import java.io.*;
class Sudan{
public static void main(String args[]){
try{
FileInputStream fin=new FileInputStream(“hello.txt”);
int i=0;
while((i=fin.read())!=-1){
System.out.println((char)i);
}
fin.close();
} catch(Exception e){
system.out.println(e);}
}
}
}

 Example to write in a file using FileOutputStream:


import java.io.*;
public class Sudan{
public static void main(String args[]){
try{
FileOutputstream fo=new FileOutputStream(“hello.txt”);
String i=”Hello “;
byte b[]=i.getBytes();/ /converting string into byte array
fo.write(b);
fo.close();
}
catch(Exception e){
system.out.println(e);}
}
}


No comments:

Post a comment

All about journals and research paper | What is impact factor? | how the impact factor is calculated? | who calculate the impact factor? Scopous journals

Figure 1: One of my Research Papers When the Scholars are in their Master or PhD or in any research field. They are supposed to writ...