Java 一次复制五个不同的序列化文件
我必须制作一个程序,将序列化文件从源文件夹复制到目标文件夹 源文件夹是C:\ter\n它有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,请告诉我如何一次复制所有五个序列化文件 本身 我已经提出了这种方法,但仍然不起作用,请建议,因为文件最终不会被复制…在堆栈跟踪下Java 一次复制五个不同的序列化文件,java,Java,我必须制作一个程序,将序列化文件从源文件夹复制到目标文件夹 源文件夹是C:\ter\n它有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,请告诉我如何一次复制所有五个序列化文件 本身 我已经提出了这种方法,但仍然不起作用,请建议,因为文件最终不会被复制…在堆
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)
若要将源文件夹的所有文件复制到目标文件夹,可以签出下面链接中提到的解决方案 以下是我测试的工作解决方案: 我从另一个StackOverFlow答案中提取了一些代码片段 参考:
我所了解的是,他们将所有文件保存在一个数组中,请您在我上面显示的程序中进行更改,以获得更清晰的信息。问题是,在outStream=new FileOutputStreamtarget;之后出现了大括号;。花括号应在扩眼后关闭;已执行此操作,但仍发现异常文件未发现异常访问被拒绝
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)
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.TimerTask;
class ScheduledTask extends TimerTask {
public static void main(String[] args) {
new ScheduledTask().run();
}
public void run() {
try {
File source = new File("c:\\ter\\");
File target = new File("c:\\bvg\\");
if (target.exists()) { // Already exists. do not copy
return;
}
else
{
target.mkdir();
}
File[] files = source.listFiles();
for (File file : files) {
File fileSource = new File(source + "\\"+file.getName());
File fileTarget = new File(target + "\\"+file.getName());
copyFile(fileSource,fileTarget);
System.out.println("File is copied successful! :" + file.getName());
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void copyFile(File sourceFile, File destFile) throws IOException {
if(!destFile.exists()) {
destFile.createNewFile();
}
FileChannel source = null;
FileChannel destination = null;
try {
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
destination.transferFrom(source, 0, source.size());
}
finally {
if(source != null) {
source.close();
}
if(destination != null) {
destination.close();
}
}
}
}