我正在学习Java,正在处理文件IO问题,目前遇到了从一个文件中读取文本并将其写入另一个文件的困境。我使用了两种不同的方法,第一种方法用于从文件#1中读取和显示文本,并使用另一种方法将其写入文件#2。
我可以成功地从文件#1中读取和显示内容,但不确定如何将文本写入文件#2。
以下是我迄今为止编写的代码:
import java.io.*;
public class ReadnWrite {
public static void readFile() throws IOException {
BufferedReader inputStream = new BufferedReader(new FileReader(
"original.txt"));
String count;
while ((count = inputStream.readLine()) != null) {
System.out.println(count);
}
inputStream.close();
}
public static void writeFile() throws IOException{
BufferedWriter outputStream = new BufferedWriter(new FileWriter(
"numbers.txt"));
//Not sure what comes here
}
public static void main(String[] args) throws IOException {
checkFileExists();
readFile();
}
}
这只是为了我自己的学习而做的,因为有很多例子可以阅读和编写,而不使用不同的方法,但我想学习如何通过不同的方法实现。
任何帮助都将不胜感激。
此致敬礼,