public class FileOutputStreamTest
{
public static void main(String[] args)
{
try (
// 創建字節輸入流
var fis = new FileInputStream("FileOutputStreamTest.java");
// 創建字節輸出流
var fos = new FileOutputStream("newFile.txt"))
{
var bbuf = new byte[32];
var hasRead = 0;
// 循環從輸入流中取出數據
while ((hasRead = fis.read(bbuf)) > 0)
{
// 每讀取一次,即寫入文件輸出流,讀了多少,就寫多少。
fos.write(bbuf, 0, hasRead);
}
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}