每2分钟将序列化文件从一个文件夹复制到另一个文件夹

时间:2020-12-25 00:29:39

Please do not consider this question as repeated one ,I have few files in a folder ter , that is ter folder is in c : drive and it contain a seriliazed file named gfr.ser so the complete path is (C:\ter\gfr.ser) , Now i want this gfr.ser file to be copied in another folder inside C: itself named bvg so I want file to copied to path (C:\bvg\gfr.ser) below is the java class , please advise can I achieve the same ,please advise

请不要将此问题视为重复的问题,我在文件夹ter中有少量文件,即ter文件夹位于c:驱动器中,它包含名为gfr.ser的seriliazed文件,因此完整路径为(C:\ ter \ gfr) .ser),现在我希望将这个gfr.ser文件复制到C里面的另一个文件夹中:本身名为bvg所以我想将文件复制到路径(C:\ bvg \ gfr.ser)下面是java类,请指教我可以达到同样的目的,请指教

import java.util.TimerTask;
import java.util.Date;
import java.util.Timer;


// Create a class extends with TimerTask
public class ScheduledTask extends TimerTask {

    // Add your task here
    public void run() {
        Runtime.getRuntime().exec("cmd.exe /c start c:\\ter\\gfr.ser");
    }
}

//Main class
public class SchedulerMain {
    public static void main(String args[]) throws InterruptedException {

        Timer time = new Timer(); // Instantiate Timer Object
        ScheduledTask st = new ScheduledTask(); // Instantiate SheduledTask class
        time.schedule(task, now ,TimeUnit.SECONDS.toMillis(2));

    }
}

1 个解决方案

#1


0  

I would say that easier option for you is probably first option from my link in comment (you wouldn't have to download additional libraries to your project). Here is simple code that you can use in your application

我会说更简单的选项可能是我在评论链接中的第一个选项(您不必将额外的库下载到您的项目中)。这是您可以在应用程序中使用的简单代码

Path source = new File("C:\\ter\\gfr.ser").toPath();
Path target = new File("C:\\bvg\\gfr.ser").toPath();
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);

But make sure that C:\bvg folder exists before you start copping.

但是,在开始copping之前,请确保存在C:\ bvg文件夹。


Since you are using JDK 1.6 this example is better. I assume that you are trying to replace old file with new one. Here is how you can do it:

由于您使用的是JDK 1.6,因此这个示例更好。我假设您正在尝试用新文件替换旧文件。以下是如何做到这一点:

import java.io.*;
import java.util.*;
import java.util.concurrent.TimeUnit;

class ScheduledTask extends TimerTask {

    public void run() {
        InputStream inStream = null;
        OutputStream outStream = null;

        try {
            File targetDirectory = new File("C:\\bvg");
            if (!targetDirectory.exists()) targetDirectory.mkdirs();

            File source = new File("C:\\ter\\gfr.ser");
            File target = new File("C:\\bvg\\gfr.ser");

            inStream = new FileInputStream(source);
            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();
        }
    }
}

// Main class
public class SchedulerMain {

    public static void main(String args[]) throws InterruptedException {

        Timer time = new Timer();
        ScheduledTask task = new ScheduledTask();

        time.schedule(task, new Date(), TimeUnit.MINUTES.toMillis(2));

    }
}

#1


0  

I would say that easier option for you is probably first option from my link in comment (you wouldn't have to download additional libraries to your project). Here is simple code that you can use in your application

我会说更简单的选项可能是我在评论链接中的第一个选项(您不必将额外的库下载到您的项目中)。这是您可以在应用程序中使用的简单代码

Path source = new File("C:\\ter\\gfr.ser").toPath();
Path target = new File("C:\\bvg\\gfr.ser").toPath();
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);

But make sure that C:\bvg folder exists before you start copping.

但是,在开始copping之前,请确保存在C:\ bvg文件夹。


Since you are using JDK 1.6 this example is better. I assume that you are trying to replace old file with new one. Here is how you can do it:

由于您使用的是JDK 1.6,因此这个示例更好。我假设您正在尝试用新文件替换旧文件。以下是如何做到这一点:

import java.io.*;
import java.util.*;
import java.util.concurrent.TimeUnit;

class ScheduledTask extends TimerTask {

    public void run() {
        InputStream inStream = null;
        OutputStream outStream = null;

        try {
            File targetDirectory = new File("C:\\bvg");
            if (!targetDirectory.exists()) targetDirectory.mkdirs();

            File source = new File("C:\\ter\\gfr.ser");
            File target = new File("C:\\bvg\\gfr.ser");

            inStream = new FileInputStream(source);
            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();
        }
    }
}

// Main class
public class SchedulerMain {

    public static void main(String args[]) throws InterruptedException {

        Timer time = new Timer();
        ScheduledTask task = new ScheduledTask();

        time.schedule(task, new Date(), TimeUnit.MINUTES.toMillis(2));

    }
}