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 {
        // 讀取文件內(nèi)容并寫(xiě)入到輸出文件
        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);
        }

        // 關(guān)閉輸入輸出流
        input.close();
        output.close();

        System.out.println("文件復(fù)制成功!");

    } catch (IOException e) {
        System.out.println("文件復(fù)制失?。?quot; + e.getMessage());
    }
}

}

0