Introduction
In this blog, I will explain, how to read data from one file and write the data in another file, using Java program. It is very simple in Java Programming. The output will be displayed in the Run module.
Software Requirement
JDK1.3.
Simple Program
- import java.io.*;
- class file1
- {
- public static void main(String arg[])
- {
- File inf = new File("in.dat");
- File outf = new File("out.dat");
-
- FileReader ins = null;
- FileWriter outs = null;
-
- try
- {
- ins = new FileReader(inf);
- outs = new FileWriter(outf);
-
- int ch;
-
- while((ch=ins.read())!=-1)
- {
- outs.write(ch);
- }
- }catch(IOException e)
- {
- System.out.println(e);
- System.exit(-1);
- }
-
- finally
- {
- try
- {
- ins.close();
- outs.close();
- }
- catch(IOException e)
- { }
- }
- }
- }
-
Step 1: Open the Notepad and write the content “Have a Nice Day” and save the file in “in.dat”.
Step 2: Open another Notepad and save the empty file in “out.dat”.
Output: Go to the command prompt, compile and execute Java program.
Step 1: Open the "out.dat" file. The content was copied in the file.
Explanation
In this blog, I explained how to read the data from one file and write the data in another file, using a Java program. The output will be displayed in the Run module.