在Java中打开一个文件流,可以使用FileInputStream和FileOutputStream类来实现。以下是一个示例代码:

import java.io.*;

public class FileIO {

    public static void main(String[] args) {
        try {
            // 打开输入流
            FileInputStream in = new FileInputStream("input.txt");
            // 打开输出流
            FileOutputStream out = new FileOutputStream("output.txt");
            
            // 读取文件内容并写入输出流中
            int c;
            while ((c = in.read()) != -1) {
                out.write(c);
            }
            
            // 关闭流
            in.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

在这个示例代码中,我们使用FileInputStream类打开了一个名为“input.txt”的输入流,并使用FileOutputStream类打开了一个名为“output.txt”的输出流。然后,我们读取了输入流中的所有内容,并将其写入输出流中。最后,我们关闭了两个流

如何打开一个文件流

原文地址: https://www.cveoy.top/t/topic/fFtw 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录