-2

我必须制作一个程序来将序列化文件从源文件夹复制到目标文件夹

源文件夹是 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)
4

1 回答 1

0

如果要将源文件夹的所有文件复制到目标,可以查看以下链接中提到的解决方案。

解决方案-链接

这是我测试的工作解决方案:(我从另一个 StackOverFlow 答案中获取了一些代码片段)

参考:StackOverFlow 答案

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();
            }
        }
    }
}
于 2013-02-03T07:29:51.933 回答