How to read data from one file and print to another file in Java?


Java provides I/O Streams to read and write data where a Stream represents an input source or an output destination which could be a file, i/o devise, other programs, etc.

In general, a Stream will be an input stream or, an output stream.

  • InputStream − This is used to read data from a source.

  • OutputStream − This is used to write data to a destination.

Based on the data they handle there are two types of streams −

  • Byte Streams − These handle data in bytes (8 bits) i.e., the byte stream classes read/write data of 8 bits. Using these you can store characters, videos, audios, images, etc.

  • Character Streams − These handle data in 16 bit Unicode. Using these you can read and write text data only.

The following diagram illustrates all the input and output Streams (classes) in Java.

Among these, you can read the contents of a file using Scanner, BufferedReader and, FileReader classes.

In the same way, you can write data into a file using BufferedWriter, FileOutputStream, FileWriter.

Writing contents of one file to other

Following is a Java program that reads data from a file to a String using the Scanner class and writes it to another file using the FileWriter class.

Example

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class CopyContent {
   public static void main(String[] args) throws IOException {
      //Instantiating a file class
      File file = new File("D:\\sampleData.txt");
      //Instantiate an FileInputStream class
      FileInputStream inputStream = new FileInputStream(file);
      //Instantiating the Scanner class
      Scanner sc = new Scanner(inputStream);
      //StringBuffer to store the contents
      StringBuffer buffer = new StringBuffer();
      //Appending each line to the buffer
      while(sc.hasNext()) {
         buffer.append(" "+sc.nextLine());
      }
      System.out.println("Contents of the file: "+buffer);
      //Creating a File object to hold the destination file
      File dest = new File("D:\\outputFile.txt");
      //Instantiating an FileWriter object
      FileWriter writer = new FileWriter(dest);
      //Writing content to the destination
      writer.write(buffer.toString());
      writer.flush();
      System.out.println("File copied successfully.......");
   }
}

Output

Contents of the file: Tutorials Point originated from the idea that there exists a 
class of readers who respond better to online content and prefer to learn new skills 
at their own pace from the comforts of their drawing rooms. The journey commenced 
with a single tutorial on HTML in 2006 and elated by the response it generated, 
we worked our way to adding fresh tutorials to our repository which now proudly 
flaunts a wealth of tutorials and allied articles on topics ranging from programming 
languages to web designing to academics and much more. 40 million readers read 100 
million pages every month. Our content and resources are freely available and we prefer 
to keep it that way to encourage our readers acquire as many skills as they would like to. 
We don’t force our readers to sign up with us or submit their details either. 
No preconditions and no impediments. Simply Easy Learning!
File copied successfully.......
raja
Published on 10-Sep-2019 16:08:40
Advertisements