Concatenate & Buffer File in Java

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  sit between programmer and source/destination file.

Buffer can be created by using following classes:

  • BufferedInputClass
  • BufferedOutputClass

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

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.                                                                

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.

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.

DataInputStream and DataOutputStream for primitive data

In java, basic input-output stream provides a method for reading & writing bytes or character in a file.

If we want to read-write primitive data typa (such as int , float etc.) than we can use filter classes to filter data in the original stream.

DataInputStream & DataOutputStream are two filter classes used for creating “data streams” for handling primitive data type.

1 DataInputStream Class

In java DataInputStream class is used to allow application to read primitive data from different sources.

DataInputStream is a sub type of FilterInputStream.

A. DataInputStream Constructor

Sr NoConstructor
1DataInputStream​(InputStream in)
Take InputStream object to create DataInputStream

Constructor call of DataInputStream

FileInputStream i = new FileInputStream("A.txt"); //open file A for read        
DataInputStream I = new DataInputStream(i);

B Methods in DataInputStream

Following are the methods of DataInputStream

Sr NoMethod
1public final int read(byte[] b) throws IOException
Reads number of bytes from input stream and store in buffered array b
2public final int read(byte[] b, int off, int len) throws IOException
Reads up to len bytes from input stream and store in buffered array b
3public final boolean readBoolean() throws IOException
Reads one input byte and return true if byte is non zero, else false
4public final byte readByte() throws IOException
Reads and return one input byte
5public final char readChar() throws IOException
Reads two input byte and return a char
6public final double readDouble() throws IOException
Reads eight input byte and return a double value
7public final float readFloat() throws IOException
Reads four input bytes and return a float value
8public final void readFully(byte[] b) throws IOException
Reads some bytes from input stream and store in buffer array b
9public final int readInt() throws IOException
Reads four input bytes and return a integer value
10public final long readLong() throws IOException
Reads eight input bytes and returns a long value
11public final short readShort() throws IOException
Reads one input bytes and returns a short value
12public final int readUnsignedByte() throws IOException
Reads two byte and return unsigned byte value
13public final String readUTF() throws IOException
Reads a string that has been encoded in modified utf-8 format
15public static final String readUTF(DataInput in) throws IOException
Reads from a stream that has been encoded in modified utf-8 format
16public final int skipBytes(int n) throws IOException
Makes attempt to skip n bytes of data from input stream

Example: Write a program to write & read a primitive data on a same file.

Character Stream I/O in Java

In a java programming, like a Byte stream Character stream is also used for input-output for 16 bit. But Byte streams are used to perform input and output of 8-bit.

Most common Character streams classes are, FileReader and FileWriter.

 FileReader- FileReader uses for reads two bytes at a time.

 FileWriter– FileWriter uses for writes two bytes at a time.

Example: Write a program, to copy the content of file A into file B using Byte stream class.

Note: if there is only file A in which data is written and no file B then, file B will be create automatically.

Suppose we have a File “A”

Hello students I am aditya.  

Output: When we run above program one new file create with Name “B” and content of file “A” will be copy into file “B”.

            File “B”

Hello students I am aditya.  

Byte Streams in Java

In java programming, to perform input and output of 8-bit, Byte stream is used.

Most common byte streams classes are, FileInputStream and FileOutputStream.

FileInputStream : FileInputStream is used for reads one byte at a time .

FileOutputStream : FileOutputStream is used for writes one byte at a time

Writing Bytes

Example: Write a program, to write a bytes to a  file using Byte stream class.

Result

checking file A.txt it will show following content.

Note: If there is no file A then, file A will be create automatically

Reading bytes from a file

Suppose we have a file “A.txt”

Hello Students I am Aditya

Example: Write a program, to read a bytes from file A using Byte stream class and print on output screen.

Example: Write a program, to copy the content of file A into file B using Byte stream class.

Note: if there is only file A in which data is written and no file B then, file B will be create automatically.

Suppose we have a File “A”

Hello students I am aditya.  

Output: When we run above program one new file create with Name “B” and content of file “A” will be copy into file “B”.

File “B”

Hello students I am aditya.  

File and Input Output in Java

In Java programming, to perform the input-output in file handling  Java has a java.io package which contain contains nearly every class you might ever need.

In file handling streams represent an input source and an output destination.

Stream

In java programming, a sequence of data defined as a “stream”.

Simply  there are two type of streams:

  1. InputStream −  To read data from a source InputStream is used.
  2. OutputStream-  To write a data to a destination OutputStream is used.

Stream Classes

In java,  package “java.io”  contains a large number of stream classes and these classes can be divided into two groups:

  1. Byte stream
  2. Character stream