Write a Program That Reads a File Containing Text
File handling plays a major role in doing so as the first essential step is writing content to a file. For this is one must know how to write content in a file using the FileWriter class. The secondary step is reading content from a file and print the same. For this, one must have good hands on File Reader class to do so.
Now in order to read content from one file and write it into another file, it is already discussed achieving the same how to write content on a file and also how to read contents from a file. Now, the time to combine both of them. Now we will use FileReader class to read the contents from a class and the FileWriter class to write it on another file.
Attention reader! Don't stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course .
Methods: In order to read contents from a file and write it into another file, one must have to know how to read a file or write a file.
- Using the variable
- Without using any variable
Method 1: Using the variable
Example 1:
Java
import
java.io.FileReader;
import
java.io.FileWriter;
import
java.io.IOException;
class
GFG {
public
static
void
main(String[] args)
{
try
{
FileReader fr =
new
FileReader(
"gfgInput.txt"
);
FileWriter fw =
new
FileWriter(
"gfgOutput.txt"
);
String str =
""
;
int
i;
while
((i = fr.read()) != -
1
) {
str += (
char
)i;
}
System.out.println(str);
fw.write(str);
fr.close();
fw.close();
System.out.println(
"File reading and writing both done"
);
}
catch
(IOException e) {
System.out.println(
"There are some IOException"
);
}
}
}
Output: As this code is accessing internal storage to save that file, so it wouldn't run on the compiler so the output is hard-coded below as shown
The program prints the content in that file, and then in the next line, it will print File reading and writing done(if there is no error occurred), and the contents of the input file will be written in the new output file. If there is some error, then it will print There are some IOException.
Method 2: Without using any variable
In the previous program, we were storing all the contents of the input file in a variable, and then we were writing the string in the output file. Now we can directly store those characters in the output file directly.
Java
import
java.io.FileWriter;
import
java.io.IOException;
class
GFG {
public
static
void
main(String[] args)
{
try
{
FileWriter fw =
new
FileWriter(
"gfg.txt"
);
fw.write(
"We love GeeksForGeeks"
);
fw.close();
System.out.println(
"\nFile write done"
);
}
catch
(IOException e) {
System.out.println(
"There are some IOException"
);
}
}
}
Output: As this code is accessing internal storage to save that file, so it wouldn't run on the compiler so the output is hard-coded below as shown
As output the program will print FIle write done(if there is no error), and will create a file with the same name given given as file name, i.e, 'gfg.text'
Write a Program That Reads a File Containing Text
Source: https://www.geeksforgeeks.org/java-program-to-read-content-from-one-file-and-write-it-into-another-file/
0 Response to "Write a Program That Reads a File Containing Text"
Post a Comment