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)