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());
}
}
}