Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Read The Data From One File And Write The Data In Another File Using Java
WhatsApp
Senthilvelan Sambamoorthy
5y
60.8
k
0
0
25
Blog
file1.rar
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.
Read The Data
Java
Up Next
Copy one file to another in java
Ebook Download
View all
Coding Principles
Read by 2.7k people
Download Now!
Learn
View all
Membership not found