温馨提示×

byte java用于文件IO的示例

小樊
82
2024-07-05 21:51:24
栏目: 编程语言

import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;

public class FileIOExample { public static void main(String[] args) { File inputFile = new File(“input.txt”); File outputFile = new File(“output.txt”);

    try {
        // 读取文件内容并写入到输出文件
        FileInputStream input = new FileInputStream(inputFile);
        FileOutputStream output = new FileOutputStream(outputFile);

        byte[] buffer = new byte[1024];
        int bytesRead;

        while ((bytesRead = input.read(buffer)) != -1) {
            output.write(buffer, 0, bytesRead);
        }

        // 关闭输入输出流
        input.close();
        output.close();

        System.out.println("文件复制成功!");

    } catch (IOException e) {
        System.out.println("文件复制失败:" + e.getMessage());
    }
}

}

0