从一个文件读取内容并将其写入另一个文件的Java程序
文件处理在此过程中起着重要作用,因为第一步是将内容写入文件。为此,必须知道如何使用 FileWriter 类在文件中写入内容。第二个步骤是从文件中读取内容并打印出来。为此,必须熟练掌握 File Reader 类才能做到这一点。
现在为了从一个文件中读取内容并将其写入另一个文件,已经讨论了如何在文件上写入内容以及如何从文件中读取内容。现在,是时候将两者结合起来了。现在我们将使用 FileReader 类从一个类中读取内容,并使用 FileWriter 类将其写入另一个文件。
方法:为了从文件中读取内容并将其写入另一个文件,必须知道如何读取文件或写入文件。
- 使用变量
- 不使用任何变量
方法一:使用变量
示例 1:
Java
// Java program to read content from one file
// and write it into another file
// Custom paths for this program
// Reading from - gfgInput.txt
// Writing to - gfgOutput.txt
// Importing input output classes
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
// Class
class GFG {
// Main driver method
public static void main(String[] args)
{
// The file reading process may sometimes give
// IOException
// Try block to check for exceptions
try {
// Creating a FileReader object and
// file to be read is passed as in parameters
// from the local directory of computer
FileReader fr = new FileReader("gfgInput.txt");
// FileReader will open that file from that
// directory, if there is no file found it will
// through an IOException
// Creating a FileWriter object
FileWriter fw = new FileWriter("gfgOutput.txt");
// It will create a new file with name
// "gfgOutput.text", if it is already available,
// then it will open that instead
// Declearing a blank string in which
// whole content of file is to be stored
String str = "";
int i;
// read() method will read the file character by
// character and print it until it end the end
// of the file
// Condition check
// Reading the file using read() method which
// returns -1 at EOF while reading
while ((i = fr.read()) != -1) {
// Storing every character in the string
str += (char)i;
}
// Print and display the string that
// contains file data
System.out.println(str);
// Writing above string data to
// FileWriter object
fw.write(str);
// Closing the file using close() method
// of Reader class which closes the stream &
// release resources that were busy in stream
fr.close();
fw.close();
// Display message
System.out.println(
"File reading and writing both done");
}
// Catch block to handle the exception
catch (IOException e) {
// If there is no file in specified path or
// any other error occured during runtime
// then it will print IOException
// Display message
System.out.println(
"There are some IOException");
}
}
}
Java
// Java program to read content from one file
// and write it into another file
// Custom paths for this program
// Reading from - gfgInput.txt
// Writing to - gfgOutput.txt
// Importing FileWriter class
// to write into a file
import java.io.FileWriter;
// Also importing IOException class to
// throw exception if occurs
import java.io.IOException;
// Class
class GFG {
// Main driver method
public static void main(String[] args)
{
// The file writing and creating process may give
// some IOException, that's why it is mandatory to
// use try block
// Try block to check for exception/s
try {
// Creating a FileWriter object which will
// create a new file and if already available
// it will open it
FileWriter fw = new FileWriter("gfg.txt");
// Content to be written on file
// Custom input string
// write() method will write the string
// in the file
fw.write("We love GeeksForGeeks");
// Closing the file freeing up resources
// in the memory
fw.close();
// Print and display message
System.out.println("\nFile write done");
}
// Catch block to catch if exception/s occurs
catch (IOException e) {
// Print and display message
System.out.println(
"There are some IOException");
}
}
}
输出:由于此代码正在访问内部存储以保存该文件,因此它不会在编译器上运行,因此输出硬编码如下所示
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.
方法二:不使用任何变量
在前面的程序中,我们将输入文件的所有内容存储在一个变量中,然后我们将字符串写入输出文件。现在我们可以直接将这些字符存储在输出文件中。
Java
// Java program to read content from one file
// and write it into another file
// Custom paths for this program
// Reading from - gfgInput.txt
// Writing to - gfgOutput.txt
// Importing FileWriter class
// to write into a file
import java.io.FileWriter;
// Also importing IOException class to
// throw exception if occurs
import java.io.IOException;
// Class
class GFG {
// Main driver method
public static void main(String[] args)
{
// The file writing and creating process may give
// some IOException, that's why it is mandatory to
// use try block
// Try block to check for exception/s
try {
// Creating a FileWriter object which will
// create a new file and if already available
// it will open it
FileWriter fw = new FileWriter("gfg.txt");
// Content to be written on file
// Custom input string
// write() method will write the string
// in the file
fw.write("We love GeeksForGeeks");
// Closing the file freeing up resources
// in the memory
fw.close();
// Print and display message
System.out.println("\nFile write done");
}
// Catch block to catch if exception/s occurs
catch (IOException e) {
// Print and display message
System.out.println(
"There are some IOException");
}
}
}
输出:由于此代码正在访问内部存储以保存该文件,因此它不会在编译器上运行,因此输出硬编码如下所示
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’