File Handling Concept in Java

  • By Pooja Nandode-Bhavsar
  • October 23, 2023
  • JAVA
File Handling Concept in Java

File Handling Concept in Java

In this blog, we will continue the File Handling Concept in Java. In the last blog, we covered types of streams, and the 3 classes related to file handling like FileOutputStreamClass, BufferedOutputStreamClass, and FileInputStreamClass. Java Excellence Begins Here! Join Our Leading Java Classes in Pune. Expert-Led Training for Java Developers. Start Your Journey Today!

 

Now let’s see the remaining classes.

 

For Free, Demo classes Call: 020-71173125

Registration Link: Click Here!

 

3. ByteArrayOuputStream class

 

ByteArrayOuputStream class is used to write common data between multiple files. If we want to write the same data in multiple files instead of creating separate files and writing data separately to each file, we can write data commonly in ByteArrayOuputStream and then we can write that common data in from ByteArrayOuputStream to each of the file

 

//Program that shows the use of ByteArrayOutputStream class

package file_handling;

 

import java.io.ByteArrayOutputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

 

public class Writing_common_data_into_multiple_files {

 

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

// TODO Auto-generated method stub

 

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

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

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

ByteArrayOutputStream bt=new ByteArrayOutputStream();

        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

bt.write(b);

bt.writeTo(fout1);

bt.writeTo(fout2);

         bt.writeTo(fout3);

       

       bt.flush();

       bt.close();

       System.out.println(“successfully writing data into multiple files..”);

}

 

}

 

In the above Program, we have created 3 output streams like fout1,fout2,fout3 and all of the three output streams are responsible for writing data in  Sample1.txt, Sample2.txt and Sample3.txt respectively. Now, here we wants to write COMMON data in all of these 3 input streams. So here, we are first writing data to ByteArrayOutputStream “bt” and then from that ByteArrayOutputStream we are writing data separately in each of the outputstream

 

For Free, Demo classes Call: 020-71173125

Registration Link: Click Here!

 

4. ByteArrayInputStream class

 

ByteArrayInputStream class is used to read byte type of an  array in java programming. It reads an array byte by byte and that each byte we can convert into character from that characters we can make a String. Means using this way we can convert byte to string format

 

package file_handling;

 

import java.io.ByteArrayInputStream;

import java.io.IOException;

 

public class ByteArrayInputStream_class {

 

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

// TODO Auto-generated method stub

 

//byte to string conversion

byte b[]= {80,79,79,74,65};  //byte=> -128 to 0 +127

ByteArrayInputStream biinArr =new ByteArrayInputStream(b);

int i;

do

{

i=biinArr.read();

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

 

 

biinArr.close();

}

 

}

 

In the above code, we created on byte array named b. Then we created an object of ByteArrayInputStream class as biinArr. In this binary, we are reading a byte array and printing that byte array using do while loop and at a time of printing this array. We are converting it into character format using the cast operator. Excel in Java with Our Comprehensive Java Training in Pune. Expert-Led Java Training for Aspiring Developers. Start Your Journey Today!

 

5. ObjectOuputStream class

 

ObjectOuputStream class is used to write an object into the file by converting that object into a byte stream because object can contain different types of data so we can’t write an object directly into a file..

In Java, we can only write bytes/characters directly into the file so we need to converts or perform serialization(object-to-byte stream conversion) on that object

 

package file_handling;

 

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectOutputStream;

import java.io.Serializable;

 

class Student implements Serializable //Marker/blank/Empty

{

int rno;           

String name; 

         double marks; 

char div;           

Student(int rno, String name, double marks, char div,boolean isPresent)

{

this.rno = rno;

this.name = name;

this.marks = marks;

this.div = div;

this.isPresent = isPresent;

}

  public String toString() {

        return rno+” “+name+” “+marks+” “+div+” “+isPresent;

    }

 

}

public class Serialization {

 

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

// TODO Auto-generated method stub

 

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

Student ram =new Student(23,”sham”,82.3,’B’,true);

ObjectOutputStream bout=new ObjectOutputStream(fout);

bout.writeObject(ram); //here,writeObject() method is used to write an object into file by converting that object into bytestream

    bout.flush();

    bout.close();

   

  System.out.println(“writing object into file…”);

}

 

}

 

For Free, Demo classes Call: 020-71173125

Registration Link: Click Here!

 

  1. ObjectInputStream class

 

ObjectInputStream class is used to read an object from the file in the form of a byte stream. This class has a readObject() method. And using this method we can read an object from a file. Means it performs deserialization on an object by converting byte stream back into the object

 

package file_handling;

 

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

 

public class De_Serialization {

 

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

// TODO Auto-generated method stub

 

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

ObjectInputStream biin=new ObjectInputStream(fin);

//here,readObject method is used to read/convert bytestream to object

       Student k  =(Student)biin.readObject(); //Object to Student conversion

  

             

  System.out.println(k);  //Student z

}

}

 

Do watch the video on Java: Click Here 

 

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 *

*
*