Share with others

File Handling and IO Handling in Java Programming


This Java turorial basically focus on the File handling operation and IO portion if java programming . Till we have covered some of the Basic of Core java programming .


We will cover following topics during this part of java tutorial . 

  • File Handling in Java
  • IO Handling in Java

File Handling in Java


Files are the primary source and destination for data within most programs. File are also used for orgainising the data in file structures .


Java devotes a whole range of methods found in a class called File in the java.io package to perform these operation.


Constructor for Files 


Objects of the file class can be created using three types of File constructor.


File f1= new File (“c:\\java\\sample.txt”)
File f1= new File (“c:\\java”, “sample.txt”)
File f1= new File (“java”, “sample.txt”)


Methods of File Class


The method present in File class are :

  1. String getName();
  2. String getPath();
  3. String getParent();
  4. boolean exists()
  5. boolean isFile() 
  6. boolean isDirectory()

Other Methods of File Class

  1. boolean canRead()
  2. boolean canWrite() 
  3. boolean delete() 
  4. long lastModified()
  5. long length()
  6. String [] list()
  1. boolean mkdir()
  2. boolean renameTo (File dest) 

Streams in Java File Handling 


A Stream is a path of communication between the source of information and the destination.


Streams can be divided under three categories as follows :

  1. InputStream
  2. OutputStream
  3. Reader/Writer

Example 1


byte c;
c=(byte) System.in.read();
System.out.println (“Entered Character is ” );
System.out.println ((char )c);


Example 1 (modified)


byte c[]= new byte[10];
for (int i=0; i<10; i++)
c[i]=(byte) System.in.read();


System.out.println (“Entered Character is ” );
for (int i=0; i<10; i++)
System.out.println ((char )c[i]);
Example 1 (modified)
byte c[]= new byte[10];
for (int i=0; i<10; i++)
c[i]=(byte) System.in.read();


System.out.println (“Entered String  is ” );
System.out.write(c);




File Streams in Java 


There are various types of streams found in the java.io package

  1. FileInputStream
  2. FileOutputStream
  3. BufferedInputStream
  4. BufferedOutputStream
  5. BufferedReader
  6. BufferedWriter

Example 2


byte b[]= {65,66,67,68,65,66,67,68,65,66};


FileOutputStream f= new FileOutputStream (“c:\\abc.txt”);


for (int i=0;i<10; i++)
f.write(b[i]);


Example 2 (modified)




char c[]= {‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G‘};


FileOutputStream f= new FileOutputStream (“c:\\abc.txt”);


for (int i=0;i<5; i++)
f.write(c[i]);




Example 2 (modified)


FileInputStream f= new FileInputStream (“c://abc.txt”);


for (int i=0; i
System.out.print( (char) f.read());


Example 3


byte b[]= {65,66,67,68,65,66,67,68,65,66};
BufferedOutputStream f= new BufferedOutputStream( new FileOutputStream (“c:\\abc.txt”);
f.write(b);


Example 3 (Modified)


char c[]= {‘A”, ‘B’, ‘C’, ‘D’ };


BufferedOutputStream f= new BufferedOutputStream( new FileOutputStream (“c:\\abc.txt”);
f.write(c);


Example 4


To take String from User during the program . You can use bufferreader in java programs to take input from the user during the program execution .


So, here is the complete code to take input from user .


BufferedReader b= new BufferedReader( new InputStreamReader (System.in);
String str1= b.readLine();
————————-
String str2=b.readLine();
Example 4
To take Integer from User 
BufferedReader b= new BufferedReader( new InputStreamReader (System.in);
String str1= b.readLine();
Int a= Integer.parseInt (str1);
————————-
String str2=b.readLine();




Questions for Quick Revision 


Explain three different ways for creating File Object? 


Explain different ways for taking Input from the User?


Share with others