Concatenate & Buffer File in Java

Here we will concatenating and store buffering files in java using BufferedInputClass and BufferedOutputClass.

Concatenate: It is possible to concatenate two or more files and save in a different file.

In java, by using SequenceInputStream class we can concatenate two or more files.

Buffer Files: In java, we can create a buffer to store temporary data that is read from & written to a stream and this process known as i/o buffer operation.

Buffer is  contiguous block of memory between programmer and source/destination file.

Buffer can be created by using following classes:

  • BufferedInputClass
  • BufferedOutputClass

concatenating and buffering files in java

Example: write a program to concatenate two file A & B and concatenated data print on output screen.

import java.io.*;
public class FileConcat {
    public static void main(String arg[]) throws IOException {
        FileInputStream I1 = null;
        FileInputStream I2 = null;
        I1 = new FileInputStream("A.txt"); //open file A for concatenate	
        I2 = new FileInputStream("B.txt"); //open file B for concatenate
        //	declare sis to store combined file		
        SequenceInputStream sis = null;
        sis = new SequenceInputStream(I1, I2);
        // create buffer for input output	
        BufferedInputStream ibs = new BufferedInputStream(sis);
        BufferedOutputStream obs = new BufferedOutputStream(System.out);
        int c;
        while ((c = ibs.read()) != -1) // read& write till the end of buffer
        {
            obs.write((char) c);
        }
        ibs.close();
        obs.close();
        I1.close();
        I2.close();
    }
}

Suppose we have a File “A.txt”

Hello students I am aditya.  

 and                         File “B.txt”

Hello  

Output: Hello students I am aditya.                                                                                           Hello

Example: Write a program to copy the content of one file to another file using buffer/BufferReader.                                                                

import java.io.*;
public class FileCopy1 {
    public static void main(String[] args) throws IOException {
        PrintWriter pw = new PrintWriter("B.txt"); // PrintWriter  for write on file  B
        BufferedReader br = new BufferedReader(new FileReader("A.txt"));
        String line = br.readLine(); // read first line from file A
        while (line != null) // loop to copy each line of A.txt to  C.txt
        {
            pw.println(line);
            line = br.readLine();
        }
        pw.flush();
        // closing resources 
        br.close();
        pw.close();
    }
}

Suppose we have a File “A .txt”                               

Hello students I am aditya.  

Output:                                              File “B”

Hello students I am aditya.  

Note: If there is no file “B” it will be create automatically.

Example: Write a program to concatenate two file A & B and save the concatenated data on a file C.

import java.io.*;
public class FileMerge {
    public static void main(String[] args) throws IOException {
        PrintWriter pw = new PrintWriter("C.txt"); // PrintWriter  for write on C.txt

        // BufferedReader object for A.txt 
        BufferedReader br = new BufferedReader(new FileReader("A.txt"));

        String line = br.readLine(); // read first line from file A
        while (line != null) // loop to copy each line of A.txt to  C.txt
        {
            pw.println(line);
            line = br.readLine();
        }

        br = new BufferedReader(new FileReader("B.txt"));

        line = br.readLine(); // read first line from file B
        while (line != null) // loop to copy each line of B.txt to  C.txt
        {
            pw.println(line);
            line = br.readLine();
        }

        pw.flush();

        // closing resources 
        br.close();
        pw.close();
    }
}

Suppose we have a File “A.txt” and File “B.txt”

Hello students I am aditya.  
Hello  

Output:               File “C”

Hello students I am aditya. Hello  

Note: Ifthere is no file “C” it will be create automatically.

Read More

File and Input Output in Java

Byte Streams in Java

Character Stream I/O in Java

File I/O with Primitive Data Type

Reference

Class BufferedInputStream