1. 程式人生 > >編寫程式,完成檔案複製功能,即將a.txt檔案內容複製到b.txt檔案中去

編寫程式,完成檔案複製功能,即將a.txt檔案內容複製到b.txt檔案中去

import java.io.*;
public class copyfile {

/**
* @param args
*/
public static void main(String[] args) {
String oldPath="c://a.txt";
String newPath="c://b.txt";


try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //檔案存在時
InputStream inStream = new FileInputStream(oldPath); //讀入原檔案
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
int length;
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //位元組數 檔案大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
}
catch (Exception e) {
System.out.println("複製單個檔案操作出錯");
e.printStackTrace();

}

}