拷貝文本檔案
package com.io;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* 将c盤一個文本檔案複制到D盤
* @author 小明
*1、在D盤建立一個檔案.用于存儲c盤檔案中的資料
*2、定義讀取流
*3、
*4、關閉資源
*/
public class CopyDemo {
public static void main(String[] args) throws IOException {
copy_1();
}
public static void copy_2(){
FileWriter fw =null;
FileReader fr =null;
try {
fw = new FileWriter("C:\\Users\\小明\\Desktop\\peoplexxx.txt");
fr = new FileReader("C:\\Users\\小明\\Desktop\\people2.txt");
char[] buf = new char[1024];
int len =0;
while((len=fr.read(buf))!=-1){
fw.write(buf, 0, len);
}
} catch (Exception e) {
}finally{
if(fw!=null){
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(fr!=null){
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//從c盤讀取一個字元就往D盤寫一個字元
public static void copy_1() throws IOException{
//建立目的檔案
FileWriter fw = new FileWriter("C:\\Users\\小明\\Desktop\\peoplexxx.txt");
//與已有檔案關聯
FileReader fr = new FileReader("C:\\Users\\小明\\Desktop\\people2.txt");
int ch =0;
while((ch=fr.read())!=-1){
fw.write(ch);
}
fw.close();
fr.close();
}
}