黑馬程式設計師------IO流----複製
阿新 • • 發佈:2018-11-05
將C盤檔案1.txt,複製到D盤。
步驟:1:在D盤裡面建立一個1.txt檔案,用於接受C盤裡面的檔案的資料
2;: 用FileReader fr = new FileReader("c:\\1.txt");,把C盤1.txt檔案關聯起來
3: 建立一個緩衝區陣列,用於存放目標檔案的資料
4:: 把緩衝區資料寫入D盤檔案中
5:: 拋異常
package com.itheima;
//將c盤一個檔案複製到d盤
/**
*
* @author wwh
*
*/
import java.io.*;
public class write {
//於已有檔案相關聯
FileReader fr = new FileReader("c:\\1.txt");
//建立目的地
FileWriter fw = new FileWriter("d:\\1.txt");
char[] ch = new char[1024];
int len = 0;
while((len = fr.read(ch))!=-1){
fw.write(ch,0,len);
}
fr.close();
fw.close();
}
}