Let Us Know About File Handling In Java


This resource contains the information about 'file handling' in java etc.

Let Us Know About File Handling In Java



Java supports Unicode character set. It contains all types of languages characters. ASCII is subset of UNICODE character set. Byte data type supports 1 byte. It handles ASCII character set. Char data type occupies 2 bytes because it stores UNICODE characters.

Java contains two types of classes to do file handling.
1. Byte stream classes.
2. Char stream classes.


1. Byte stream classes: It supports ASCII characters. OutputStream, InputStream are parent classes for byte stream programming. OutputStream family classes are used to store ASCII data in the background file.
Example: DataOutputStream, FileOutputStream,.. .
InputStream family classes are used to read ASCII data from background file.
Example: DataInputStream, FileInputStream, . . .

2. Char stream classes: It supports UNICODE characters. Writer, Reader is parent classes for char stream programming. Writer family classes are used to write UNICODE data in the background file.
Example: FileWriter, PrintWriter,.. .
Reader family classes are used to read UNICODE data from background file.
Example: FileReader, BufferedReader, . . .

NOTE:
1. File handling classes and interfaces are in java.io package.
2. File handling methods may raise checked exceptions called FileNotFoundException, IOException.

WriteBytes.java

import java.io.*;
class WriteBytes
{
public static void main(String args[])
throws Exception
{
byte cities[]={'D','E','L','H','I','\n','M','A','D','R','A','S'};
FileOutputStream outfile=new FileOutputStream("city.txt",true);
for(int i=0;i<12;i++)
System.out.print(cities[i]+" ");

outfile.write(cities);
outfile.close();

}
}

FileOutputStream class object is created and initialized with background file. Its constructor takes background file name, true for appending / false for overwriting.
write() method takes byte array and store in background file in the form of characters.
close() method closes background file.

ReadBytes.java

import java.io.*;
class ReadBytes
{
public static void main(String args[])
{

int ch;
try
{
FileInputStream infile=new FileInputStream("city.txt");
FileOutputStream outfile=new FileOutputStream("myfile");
while( (ch=infile.read()) != -1)
{
outfile.write(ch);
System.out.print(ch+" ");
}
infile.close();
outfile.close();
}
catch(FileNotFoundException e)
{
System.out.println("exception");
}

catch(IOException e)
{
System.out.println("exception");
}
}
}

FileInputStream class object is created and initialized with background file. read() method reads ASCII values of data from background file and returns. When data reading is completed, it returns -1. write() method takes ASCII number, converts as character and store in the background file.

CopyCharacters.java

import java .io.*;
class CopyCharacters
{
public static void main(String args[])
{
try
{
FileReader ins=new FileReader("lepakshi" );
FileWriter outs=new FileWriter( "file3.dat" );

int ch;
while( (ch=ins.read()) != -1)
{
outs.write(ch);
}

ins.close();
outs.close();
}
catch(Exception e)
{
System.out.println("exception");
}

}
}

FileWriter, FileReader classes objects are created and initialized with background files. Data is read from one file and stored in another file. It is called file copy operation.

ReadWriteIntegers.java
import java.io.*;
class ReadWriteIntegers
{
public static void main(String args[])
throws Exception
{
FileOutputStream fos= new FileOutputStream("rand");
DataOutputStream dos=new DataOutputStream(fos);
for(int i=0;i<20;i++)
{
//dos.writeDouble( Math.random());
double dd=Math.random();
System.out.print(dd+" ");
dos.writeInt( (int) (dd * 100));
} //
dos.close();

DataInputStream dis=new DataInputStream(new FileInputStream("rand"));

System.out.println();
for(int i=0;i<20;i++)
{

int n=dis.readInt();
System.out.print(n+" ");
}
dis.close();
}

}

DataOutputStream:

outstream
DataOutputStream class object is created and initialized with FileOutputStream class object. This FileOutputStream class object is initialized with background file. DataOutputStream class contains methods to write primitive data in the background file.
Example: writeInt(), writeDouble(), writeChar(), ...

inputstream

DataInputStream:


DataInputStream class object is created and initialized with FileInputStream class object. This FileInputStream class object is initialized with background file. DataIutputStream class contains methods to read primitive data from the background file.
Example: readInt(), readDouble(), readChar(), ...

random() is static method of Math class, returns double type value which is in between 0 and 1.





ReadWritePrimitive.java

import java.io.*;
class ReadWritePrimitive
{
public static void main(String args[])
throws Exception
{
//FileOutputStream fos=new FileOutputStream("00000prim.txt"); DataOutputStream dos=new DataOutputStream(new FileOutputStream("prim.txt"));
dos.writeInt(1999);
dos.writeDouble(23.85);
dos.writeBoolean(false);
dos.writeChar('x');
dos.close();//
fos.close();
FileInputStream fis=new FileInputStream("prim.txt");
DataInputStream dis=new DataInputStream(fis);
System.out.println(dis.readInt()); System.out.println(dis.readDouble());
System.out.println(dis.readBoolean()); System.out.println(dis.readChar());

dis.close(); fis.close();
}
}

RandomIO.java

import java.io.*;

class RandomIO
{
public static void main(String args[])
throws Exception
{
RandomAccessFile file=new RandomAccessFile("rand","rw"); file.writeChar('x');
file.writeInt(444);
file.writeDouble(4.321);

file.seek(1);
System.out.println(file.readChar()); System.out.println(file.readInt());
System.out.println(file.readDouble());

file.seek(2);

System.out.println(file.readInt());

file.seek(4);
file.writeBoolean(false);

file.seek(4);

System.out.println(file.readBoolean());

file.close();
}
}

RandomAccessFile class contains methods of DataOutputStream, DataInputStream classes. It is used to open background file in multiple modes. Its constructor takes background file name, file opening modes. seek() method is used to move control from one place to any other place in the background file.

SequenceBuffer.java

import java.io.*;
class SequenceBuffer
{
public static void main(String args[])
throws Exception
{
FileInputStream file1= new FileInputStream("text1.txt"); FileInputStream file2=new FileInputStream("text2.txt");

SequenceInputStream file3=new SequenceInputStream(file1,file2); /*System.out.println("file1="+file1+"\nfile2= "+file2);

BufferedInputStream inBuffer=new BufferedInputStream(file3); BufferedOutputStream outBuffer=new BufferedOutputStream(System.out);

int ch;

while( (ch=inBuffer.read() ) != -1)
{
//System.out.print((char)ch); outBuffer.write(ch);
}
file1.close();
file2.close();
inBuffer.close();

outBuffer.close();

}
}

FileInputStream class objects are created and initialized with background files. SequenceInputStream class object is created and initialized with FileInputStream class objects. BufferedInputStream class object is created and initialized with SequenceInputStream class object. BufferedOutputStream class object is created and initialized with System.out. Data is read from BufferedInputStream class object and written to BufferedOutputStream class object. That means data are displayed on monitor.


Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: