File Handling in Java

  • By Pooja Nandode-Bhavsar
  • September 30, 2023
  • JAVA Programming
File Handling in Java

File Handling in Java

File handling in Java means performing create, open, read, write, and close operations on a file through an application. 

Basically, in Java, we can read and write two types of data into the file

1. Byte Oriented Data

2. Character Oriented Data

In c and C++, we can read/write only character type of data but in Java, we can read/write character as well as byte type of data because byte type of data can be read and written faster as compared to character type of data. 

Java consists of some inbuilt classes come under java.io package which can help us to perform operations on the file.

 

For Free, Demo classes Call: 020-71173125

Registration Link: Click Here!

 

These classes are divided into 2 categories. 

1.byte stream  classes 

2.character stream glasses. 

 

Here, stream is nothing but it stores a sequence of characters. 

There are 3 types of steam

1. Output stream

Represent in the system as System. Out

2. Input stream

Represent in the system as System.in

3. Error stream

Represent in the system as System.err

 

Now, let’s first learn byte stream classes which allow programmers to perform read/write operations on the byte type of data in the file

Classes related to File Handling

 

1.FileOutputStream class

It is used to perform create, open, write, and close operations on the file

 

package file_handling;

 

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

public class Writing_byte_data_into_the_file {

public static void main(String[] args) throws IOException {

// TODO Auto-generated method stub

FileOutputStreamfout=new FileOutputStream(“C:\\Users\\Admin\\Desktop\\file_handling\\sample.txt”);

//Writing bytes into file

fout.write(65);     

fout.write(80);

fout.write(80);

fout.write(76);

fout.write(69);

//we cannot write this string directly into the file bcz,here fout.write() does //not accept strings..so we have to convert string into byte

String s=”hello,this is java session\nwelcome to this session\nthank u”;

//String to byte conversion/casting

byte b[]=s.getBytes();  //here,getBytes() method read characters from the string and convert that characters into bytes

fout.write(b);;

fout.flush(); //flushing data from file output stream

fout.close();

System.out.println(“success”);

}

}

 

After executing the above code, there will be one file created in the background on the given path “C:\\Users\\Admin\\Desktop\\file_handling\\sample.txt”. With name sample.txt

And then given data will be written into that file.

 

But here, one thing to remember is that we cannot directly write characters or strings into the file. We can only write byte or integer type of data directly. To write the String/character type of data into the file we need to convert that string into byte data and then we can write that bytes into the file. Are you interested in learning Java, one of the most popular programming languages in the world? Join our Java classes in Pune and take the first step towards becoming a skilled Java developer!

 

To perform string-to-byte conversion we can use getBytes() method.

 

For Free, Demo classes Call: 020-71173125

Registration Link: Click Here!

 

2.BufferedOutputtStream class

 

In the above program we have not used Buffer with FileOutputStream so in that case the data/bytes will be written into the file as one byte at a time meaning it does not write all bytes in one go into the file. But if we are using a buffer with FileOutputStream so in that case all bytes will be written into the buffer in one go or at a time and from that buffer bytes will be written into file. So bytes can be written faster as if we are using a buffer. So the FileOutputStream with buffer is represented by BufferedOutputStream.

 

package file_handling;

import java.io.BufferedOutputStream;

import java.io.FileOutputStream;

import java.io.IOException;

public class BufferedOutputStream_class {

public static void main(String[] args) throws IOException {

FileOutputStreamfout=new FileOutputStream(“C:\\Users\\Admin\\Desktop\\file_handling\\sample.txt”);

//Buffer is a temporary memory in which data gets stored internally…means we are writing a 

//data first in the buffer and then that buffer writes a data into file..so that data can be written faster

//if we are not using buffer so in that case data will be written in the form of one byte at a time

//but we are using buffer then whole data will be write in the buffer in a one go and then from buffer it will be written into the file

BufferedOutputStream bout =new BufferedOutputStream(fout);

//Writing bytes into file

bout.write(65);   //Apple, hello,this is java session\nwelcome to this sess //A=65,B=66,C=67 DEFGHIJKLMNOP…….a=97,b=98….0=48,1=49

bout.write(80);

bout.write(80);

bout.write(76);

bout.write(69);

//we cannot write this string directly into the file bcz,here fout.write() does not accept strings..so we have to convert string into byte

String s=”Good evening,this is java session\nwelcome to this session\nthank u”;

//String to byte conversion/casting

byte b[]=s.getBytes();  //here,getBytes() method read characters from the string and convert that characters into bytes

bout.write(b);;

 

bout.flush(); //flushing data from file output stream

bout.close();

System.out.println(“success”);

 

}

 

}

2.FileInputStream class

It is used to perform  open, read, close operations on the file. 

 

package file_handling;

 

import java.io.BufferedInputStream;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

 

public class Reading_bytes_from_file {

 

public static void main(String[] args) throws IOException {

 

FileInputStream 

fin=new FileInputStream(“C:\\Users\\Admin\\Desktop\\file_handling\\sample.txt”);

BufferedInputStream biin =new BufferedInputStream(fin);

int i;

do

{

      i=biin.read();      //here,read() method reads single byte at a time in integer format

if(i != -1)

{

  System.out.print((char)i); //converting int i into character format

}

}while(i != -1); //checking whether i is not equal to end of file

 

 

         biin.close();

}

 

}

 

Do watch the video on Java: Click Here 

 

In the above code, we have used read() method of the FileInputStream class to read the data from a file. Here,read() method reads single byte at a time in integer format. But here we want to read multiple bytes from a file so we have put this biin.read() method into the do while loop so that this read() method gets called multiple times until all bytes get read from the file. Here, this loop will be executed until the given condition is false. The condition is “ i != -1 ” meaning this loop gets execute till the value of i is not equal to -1 means the control reaches at the end of the file. In the above code itself we have used BufferedInputStream also so that all of the bytes will get read in one go into the buffer and from that buffer we can read data into our application. SevenMentor’s Java Training in Pune includes hands-on projects that allow you to apply what you have learned and gain practical experience. 

Author:-

Pooja Nandode-Bhavsar
Call the Trainer and Book your free demo class for Java now!!!

© Copyright 2020 | SevenMentor Pvt Ltd.

Submit Comment

Your email address will not be published. Required fields are marked *

*
*