Reading from File

Read operation from file can be done using byte stream or using character stream. Using byte stream all we get is binary sequence of data. Which we need to convert into character format. On the other side character stream will retrieve data directly in characters.

1.Reading using Byte Stream

1.1 Program to read from a file using Byte stream


import java.io.*;
class fileread
{
public static void main(String arg[])
{
try
{
FileInputStream fis=new FileInputStream("one.txt");
int b;
do
{
b=fis.read();
System.out.println((char)b);
}while(b!=-1);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

1.2 Program to read from a file using BufferedInputStream

BufferedInputStream adds buffer capability to above program. Now instead of reading one byte at a time, now it will read multiple bytes. BufferedInputStream has a method read. This method require three parameter. First parameter is byte array to store content, second and third parameter is starting and end index of byte array. It will specify range of array index which will store data from file.


import java.io.*;
class fileread
{
public static void main(String arg[])
{
try
{
FileInputStream fis=new FileInputStream("one.txt");
BufferedInputStream bs=new BufferedInputStream(fis);
byte[] data=new byte[50];
bs.read(data,0,data.length);
for(byte b:data)
System.out.println((char)b);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

1.3 Read from file using DataInputStream

DataInputStream class allow reading data from file directly into primitive data type. For example if we are using byte stream to read content from file. It will read bytes then after reading those data has to be converted into required primitive data type.

If DataInputStream is used then data from file will be read into primitive data type. DataInputStream is used to read data that is written using DataOutputStream class.


import java.io.*;
class fileread
{
public static void main(String arg[])
{
try
{
FileInputStream fis=new FileInputStream("one.txt");
BufferedInputStream bs=new BufferedInputStream(fis);
DataInputStream dis=new DataInputStream(bs);
int a=dis.readFloat();
int b=dis.readInt();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}



2.Reading using Character Stream

2.1 Program to read from a file using character stream

Character stream (FileReader) read character by character from file. One character is read at a time.



import java.io.*;
class two
{
public static void main(String arg[])
{
try
{
FileReader fis=new FileReader("one.txt");
char []b=new char[50];
fis.read(b);
for(char c:b)
System.out.print(c);

}
catch(Exception e)
{
e.printStackTrace();
}
}
}

1.2 Program to read from a file using BufferedReader

BufferedReader adds buffer capability to above program. Now instead of reading one character at a time, now it will read multiple characters using buffer. BufferedReader has a method readLine, which can read entire line.


import java.io.*;
class fileread
{
public static void main(String arg[])
{
try
{
FileReader fis=new FileReader("one.txt");
BufferedReader br=new BufferedReader(fis);
String a=br.readLine();
System.out.println("data :"+a);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}