Java 使用方法从一个文件读取并写入另一个文件

Java read from one file and write into another file using methods

我正在学习 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();
    }
}

这只是为了我自己的学习,因为有很多示例可以在不使用不同方法的情况下阅读和编写,但我想了解如何通过不同的方法实现。

任何帮助将不胜感激。

此致,

您可以使用以下方式写入另一个文件: outputStream.write()。当你完成后 outputStream.flush()outputStream.close().

编辑:

public void readAndWriteFromfile() throws IOException {
    BufferedReader inputStream = new BufferedReader(new FileReader(
            "original.txt"));
     File UIFile = new File("numbers.txt");
        // if File doesnt exists, then create it
        if (!UIFile.exists()) {
            UIFile.createNewFile();
        }
    FileWriter filewriter = new FileWriter(UIFile.getAbsoluteFile());
    BufferedWriter outputStream= new BufferedWriter(filewriter);
    String count;
    while ((count = inputStream.readLine()) != null) {
        outputStream.write(count);
    }
    outputStream.flush();
    outputStream.close();
    inputStream.close();

这是我复制文件的方法:

BufferedReader br = null;
BufferedWriter bw = null;

try {
    br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("origin.txt"))));
    bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("target.txt"))));

    int i;
    do {
        i = br.read();
        if (i != -1) {
            bw.write(i);
        }
    } while (i != -1);

} catch (IOException e) {
    System.err.println("error during copying: "+ e.getMessage());
} finally {
    try {
        if (br != null) br.close();
        if (bw != null) bw.close();
    } catch (IOException e) {
        System.err.println("error during closing: "+ e.getMessage());
    }
}

我会使用 BufferedReader 包裹 FileReaderBufferedWriter 包裹 FileWriter.

由于您想使用两种不同的方法来完成此操作,因此您必须将数据存储在 List<String> data 中以在方法之间传递它。

public class ReadnWrite {

    public static List<String> readFile() throws IOException {
        try(BufferedReader br = new BufferedReader(new FileReader("original.txt"))){
            List<String> listOfData = new ArrayList<>();
            String d;
            while((d = br.readLine()) != null){
                listOfData.add(d);
            }
            return listOfData;
        }
    }

    public static void writeFile(List<String> listOfData) throws IOException{
        try(BufferedWriter bw = new BufferedWriter(new FileWriter("numbers.txt"))){
            for(String str: listOfData){
                bw.write(str);
                bw.newLine();
            }
        }
    }

    public static void main(String[] args) throws IOException {
        List<String> data = readFile();
        writeFile(data);
    }
}

作为替代方案,如果您不必在读取和写入之间对数据进行操作,我也可以使用一种方法进行操作。

public class CopyFileBufferedRW {

    public static void main(String[] args) {
        File originalFile = new File("original.txt");
        File newFile = new File(originalFile.getParent(), "numbers.txt");

        try (BufferedReader br = new BufferedReader(new FileReader(originalFile));
             BufferedWriter bw = new BufferedWriter(new FileWriter(newFile))) {
            String s;
            while ((s = br.readLine()) != null) {
                bw.write(s);
                bw.newLine();
            }
        } catch (IOException e) {
            System.err.println("error during copying: " + e.getMessage());
        }
    }
}