FileChannel - continue on error

Hi

I have a script that copies files from a directory(and all subs) to another directory. If trying to copy a file that "cannot be found" (that be that the filesystem is unable to read the file because of uncompatible characters), the script exits with the java.io.FileNotFound exception

In WinXP the xcopy-command have a "/C parameter" that make the copy-process to continue copying if errors occur - is it possible to get the java FileChannel command to continue also if error occur?

my code:

publicstaticvoid copyFiles(String strPath, String dstPath)throws IOException

{

File src =new File(strPath);

File dest =new File(dstPath);

if (src.isDirectory())

{

//if(dest.exists()!=true)

dest.mkdirs();

String list[] = src.list();

for (int i = 0; i < list.length; i++)

{

String dest1 = dest.getAbsolutePath() +"\\" + list[i];

String src1 = src.getAbsolutePath() +"\\" + list[i];

value++;

progressBar.setValue(value);

copyFiles(src1 , dest1);

}

}

else

{

FileChannel in = null, out =null;

try{

in =new FileInputStream(src).getChannel();

out =new FileOutputStream(dest).getChannel();

long size = in.size();

MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY,0,size);

out.write(buf);

}finally{

if (in !=null) in.close();

if (out !=null) out.close();

}

in.close();

out.close();

}

}

Thanks, Peter

[2605 byte] By [Bliza] at [2007-10-3 4:32:00]
# 1
Just catch IOException before your finally block. It should probably close both files and delete the target, for niceness. If you want that kind of behaviour the copyFiles() routine probably shouldn't throw IOException at all.
ejpa at 2007-7-14 22:35:29 > top of Java-index,Core,Core APIs...
# 2
Great, that worked :-)Next question,I want to monitor how much data that have been written -in realtime- in the out.write(buf) command.What do i need to add to my code to get this functionality?Thanks, Peter
Bliza at 2007-7-14 22:35:29 > top of Java-index,Core,Core APIs...
# 3

Btw, may this work?

Instead of "out.write(buf);" :

for(long j=0;j<size;j++){

in.transferTo(j,size,out);

System.out.println(j+" bytes out-of "+ size+" written");

}

?>

Bliza at 2007-7-14 22:35:29 > top of Java-index,Core,Core APIs...
# 4
it wont - so don't bother replying :-/Any other "good" ideas?
Bliza at 2007-7-14 22:35:29 > top of Java-index,Core,Core APIs...
# 5
It will if ou change 'size' in the call to 'size-j', and if you don't try to transfer gigabytes with this call - some platforms have an upper limit and throw RTEs if you exceed it.As to your original question, write() returns a count, just add them up!
ejpa at 2007-7-14 22:35:29 > top of Java-index,Core,Core APIs...
# 6

Thanks, ejp

Just to get this right,

1. for(long j=0;j<size;j++){

2. in.transferTo(j,size,out);

3. System.out.println(j+" bytes out-of "+ size+" written");

}

I which line do you suggest that I change size to size-j ?

(and to my original question - i'm gonna use this "j"-number to update a progressbar and i'm aware of the "gigabyte" limit )>

Bliza at 2007-7-14 22:35:29 > top of Java-index,Core,Core APIs...
# 7

Makes no sense at all. The loop should go like this:

long size = file.length();

long count = 0;

while (count < size)

{

count += fch.transferTo(count,size-count,out);

}

The 2nd parameter has to count down as you go through the file.

ejpa at 2007-7-14 22:35:29 > top of Java-index,Core,Core APIs...
# 8
Hmm...Something isn't quite right in your codeThe filetransfer did transfer all files, but all files ended up with a filze of 0 bytesAny ideas how to fix this?
Bliza at 2007-7-14 22:35:29 > top of Java-index,Core,Core APIs...
# 9
'file' is the input file not the output file of course ...
ejpa at 2007-7-14 22:35:29 > top of Java-index,Core,Core APIs...
# 10
Hi,yes, i forgot the 'long count = 0' lineBtw, how do i retain the filedate&filetime of the file that is beeing copied?Now, all filedates&times are set to this(now) date/timeThanks, Peter
Bliza at 2007-7-14 22:35:29 > top of Java-index,Core,Core APIs...
# 11
File.setLastModified()
ejpa at 2007-7-14 22:35:29 > top of Java-index,Core,Core APIs...
# 12
Thanks, but i dont now how to implement this into the filechannel-copy, since this is all about sourceDir and destDirEpj, any ideas?
Bliza at 2007-7-14 22:35:29 > top of Java-index,Core,Core APIs...
# 13

If I add the following line after this:

...

File src = new File(strPath);

File dest = new File(dstPath);

...

dest.setLastModified(src.lastModified());

Could this be the solution?

Message was edited by:

Bliz

Bliza at 2007-7-14 22:35:29 > top of Java-index,Core,Core APIs...
# 14
Could it possibly be worth while trying it yourself to find out?
ejpa at 2007-7-14 22:35:29 > top of Java-index,Core,Core APIs...
# 15
:-)Propably, but I dont ask if I already know the answer.I'm stuck and need help...
Bliza at 2007-7-21 10:34:04 > top of Java-index,Core,Core APIs...
# 16
You don't believe in experiment? You just write code and wait for someone else to say it's OK to run it? and do nothing in the meantime?WHY NOT RUN IT AND SEE FOR YOURSELF?
ejpa at 2007-7-21 10:34:04 > top of Java-index,Core,Core APIs...
# 17
hmm...I'm a bit novice in this area, and i have tested my suggested code, but it did not work, so all I'm doing is ask for help, isn't that why we have this forum - to ask for help?
Bliza at 2007-7-21 10:34:04 > top of Java-index,Core,Core APIs...
# 18
Then I think you should have said so. The appearance was that you had come up with some code but couldn't be bothered running it.If the File objects are both pointing to the right files and the output file is already closed it should work. What symptom did you get?
ejpa at 2007-7-21 10:34:04 > top of Java-index,Core,Core APIs...
# 19

I'm sorry if that was how it looked

Some of my code is other, some is my own, and i do compile it and run it. I'm not that lazy as I might look ;-)

I rewrote my last code, hopefully to fix this, but no...

(see page 1 in this thread for the rest of the code)

String dest1 = dest.getAbsolutePath() + "\\" + list[i];

File a = new File(dest1);

long Bdate = a.lastModified();

String src1 = src.getAbsolutePath() + "\\" + list[i];

File Adate = new File(src1);

Adate.setLastModified(Bdate);

Then i added a System.out.println(Bdate);

to check that the filedate actually changed, parsing through the files in the Dirs - and it did. But the destination files, still get the date and timestamp as it is right now, not the expected <Bdate>-time

I really dont get why this is....?

Regards, Peter

Bliza at 2007-7-21 10:34:04 > top of Java-index,Core,Core APIs...
# 20
So did setLastModified() return true or false?
ejpa at 2007-7-21 10:34:04 > top of Java-index,Core,Core APIs...
# 21
false...
Bliza at 2007-7-21 10:34:04 > top of Java-index,Core,Core APIs...
# 22
Found the solution ;-)I had to put the b.setLastModified(); after the last out.close, instead of the out.close() in the finally{...}Thanks for helping me out so far :-)
Bliza at 2007-7-21 10:34:04 > top of Java-index,Core,Core APIs...