简体   繁体   中英

copying a serialized file from one folder to other folder in every 2 minutes

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

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));

    }
}

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.


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:

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));

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM