Writing to file

Write operation to file can be done using byte stream or using character stream. If byte stream is used to perform write operation then first our data need to be converted into bytes then those bytes will be written to file. Character stream will write data directly to file.

1.Writing using Byte Stream

1.1 Program to write to a file using Byte stream


import java.io.*;
class fileread
{
public static void main(String arg[])
{
try
{
FileOutputStream fis=new FileOutputStream("one.txt");
fis.write((int)'J');
fis.write((int)'A');
fis.write((int)'V');
fis.write((int)'A');

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

1.2 Program to write to file using BufferedOutputStream

BufferedOutputStream adds buffer capability to above program. Now instead of writing one byte at a time, now it can write multiple bytes. BufferedOutputStream has a method write. This method require three parameter. First parameter is byte array, second and third parameter is starting and end index of byte array. It will specify range of array index.


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();
}
}
}