1. 程式人生 > >通過FileReader和FileWriter實現複製檔案的方法。

通過FileReader和FileWriter實現複製檔案的方法。

public class CopyDemo {

 

public static void main(String []args)

{

copyd(); 

}

 

public static  void copyd() {

FileWriter fw = null;

FileReader fd = null;

try

{

fw = new FileWriter("/Users/dongbo/Downloads/a_copy.txt");

fd = new FileReader("/Users/dongbo/Downloads/a.txt");

 

char [] ch = new char[1024];

int num = 0;

while((num=fd.read(ch))!=-1)

{

fw.write(ch,0,num);

}

 

}

catch(IOException e)

{

System.out.println( e.toString());

}

finally

{

try {

if(fw!=null)

fw.close();

}

catch (IOException e)

{

e.printStackTrace();

}

try

{

if(fd!=null)

fd.close();

}

catch(IOException e)

{

e.printStackTrace();

}

}

 

 

}

}