我必须制作一个程序来将序列化文件从源文件夹复制到目标文件夹
源文件夹是 C:\ter\ (它有 5 个不同的序列化文件 gfr.ser,asd.ser,hgf.ser,kiu.ser,uew.ser) 目标文件夹是 C:\bvg\
要传输的文件是 gfr.ser,asd.ser,hgf.ser,kiu.ser,uew.ser
我想出了下面这个程序,但它只复制一个序列化文件 gfr.ser,请告知我如何一次复制所有五个序列化文件。
class ScheduledTask extends TimerTask {
public void run() {
InputStream inStream = null;
OutputStream outStream = null;
try {
File source = new File("C:\\ter\\");
File target = new File(" C:\\bvg\\");
if (target.exists()){ // Already exists. do not copy
return;
}
File[] files = source.listFiles();
for(File file:files){
inStream = new FileInputStream(file);
outStream = new FileOutputStream(target);
}
byte[] buffer = new byte[1024];
int length;
// copy the file content in bytes
while ((length = inStream.read(buffer)) > 0) {
outStream.write(buffer, 0, length);
}
inStream.close();
outStream.close();
System.out.println("File is copied successful!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Copycache {
public static void main(String args[]) throws InterruptedException {
Timer time = new Timer();
ScheduledTask task = new ScheduledTask();
time.schedule(task, new Date(), TimeUnit.SECONDS.toMillis(1));
}
}
我想出了这种方法,但仍然无法正常工作,请告知,因为文件最终没有被复制...在堆栈跟踪下方...
java.io.FileNotFoundException: C:\bvg\ (Access is denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at com.bvb.ScheduledTask.run(Copycache.java:31)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)