天天看點

JAVA 檔案操作之複制

位元組流複制

//檔案複制
File f1=new File("copy.txt");
File f2=new File("m.txt");
//位元組流
FileInputStream fis=new FileInputStream(f1);
FileOutputStream fos=new FileOutputStream(f2);
byte[] b=new byte[1024];
int n=fis.read(b);
System.out.println(b);
fos.write(b);
fr.close();
fw.close();
           

字元流複制

//檔案複制
File f1=new File("copy.txt");
File f2=new File("m.txt");
//字元流
String line=null;
FileReader fr=new FileReader(f1);
Writer fw=new FileWriter(f2);
BufferedReader br=new BufferedReader(fr);
while((line=br.readLine())!=null) {
	fw.write(line+"\r\n");
}
fr.close();
fw.close();