-2

I have to make a program to copy the serialized files from a source folder to target folder

source folder is C:\ter\ (it have 5 different serialized files gfr.ser,asd.ser,hgf.ser,kiu.ser,uew.ser) target folder is C:\bvg\

files to be transferred are gfr.ser,asd.ser,hgf.ser,kiu.ser,uew.ser

I have come up with this below program but it copies only one serialized file gfr.ser only ,please advise how can I copy all the five serialized files in one go itself .

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

    }

I have come up with this approach but still it is not working please advise as the files are finally not get copied...below the stacktrace ...

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)

1 Answers1

0

If you want to copy all files of source folder to destination, you can check out solution mentioned in below link.

Solution-Link

Here is the working solution which I tested: ( I took some code snippet from another StackOverFlow answer)

Reference: StackOverFlow Answer

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();
            }
        }
    }
}
Community
  • 1
  • 1
Amit
  • 797
  • 9
  • 32
  • what I have understood is that they have kept all files in an array , could you please make changes in my above shown program for more clearity. – user2035385 Feb 03 '13 at 08:03
  • 1
    The problems is the curly brace after outStream = new FileOutputStream(target);. Curly brace should be after outStream.close(); – Amit Feb 03 '13 at 08:43
  • done that but still found exception file not found exception access is denied – user2035385 Feb 03 '13 at 08:54