Results 1 to 9 of 9
- 01-17-2011, 03:31 AM #1
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Copy file for than 64MB, can someone explain
Hi, found this page, it discussed about copying files. I am more concern about the second example since it is faster the first example was an old technique. It says there that windows OS will give an error if I try to copy a file that is greater that 64MB, so it gives the third example. My problem is I cannot understand the third example?
Java Code:import java.io.*; import java.nio.channels.*; public class FileUtils{ public static void copyFile(File in, File out) throws IOException { FileChannel inChannel = new FileInputStream(in).getChannel(); FileChannel outChannel = new FileOutputStream(out).getChannel(); try { // magic number for Windows, 64Mb - 32Kb) int maxCount = (64 * 1024 * 1024) - (32 * 1024); //This is the part that I dont understand long size = inChannel.size(); long position = 0; //This is the part that I dont understand while (position < size) { position += inChannel.transferTo(position, maxCount, outChannel); } catch (IOException e) { throw e; } finally { if (inChannel != null) inChannel.close(); if (outChannel != null) outChannel.close(); } } public static void main(String args[]) throws IOException{ FileUtils.copyFile(new File(args[0]),new File(args[1])); } }
I thought that this example is very useful for me but I dont want to use this
unless I understand its logic or how he came up for that solution (sorry, can't express what I want to say).
Thanks in advance..Last edited by mine0926; 01-17-2011 at 03:35 AM. Reason: EDIT: I add the code..
- 01-17-2011, 04:40 AM #2
So which example is this?
- 01-17-2011, 04:58 AM #3
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
this is the third example.
And here is the site: Copy a file - Real's Java How-to
My mistake, forgot to put the link...
I add comment on the code and make the letters bold.
I dont understand what is maxCount for, why such operation "(64 * 1024 * 1024) - (32 * 1024)"...Java Code:import java.io.*; import java.nio.channels.*; public class FileUtils{ public static void copyFile(File in, File out) throws IOException { FileChannel inChannel = new FileInputStream(in).getChannel(); FileChannel outChannel = new FileOutputStream(out).getChannel(); try { // magic number for Windows, 64Mb - 32Kb) [b]int maxCount = (64 * 1024 * 1024) - (32 * 1024); //This is the part that I dont understand[/b] long size = inChannel.size(); [b]long position = 0; //This is the part that I dont understand[/b] while (position < size) { position += inChannel.transferTo(position, maxCount, outChannel); } catch (IOException e) { throw e; } finally { if (inChannel != null) inChannel.close(); if (outChannel != null) outChannel.close(); } } public static void main(String args[]) throws IOException{ FileUtils.copyFile(new File(args[0]),new File(args[1])); } }
- 01-17-2011, 05:06 AM #4
well, 1024*1024*64 is 64MB (1024B * 1024KB * 64MB) and 32 * 1024 would be 32KB. So its setting the maximum read size to 64MB-32KB. That is the maximum amount of data that is transferred at any one time. Why this specific value? No idea. Might be based on some windows OS limitation.
Now that part, thats the cursor. Imagine the file is being read one byte at a time. The cursor shows the program where the last byte read is. We start at 0, and go to the end of the file. The cursor hangs out where ever the last read left off. Like a bookmark :Dlong position = 0;
- 01-17-2011, 05:25 AM #5
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Thanks for that explanation.
Java Code:int i = 480; [b]//Is this the same with?[/b] int i = (128 * 2 * 2) - (16 * 2)
- 01-17-2011, 05:31 AM #6
Seems to be - but again, not sure as to the reason of the subtraction. When I have done file copy in the past, I pick an arbitrary buffer size such as 4MB. I haven't had problems with it, and performance is acceptable. Whatever the reasoning, the example your book gave worked well on my system (MacOS X)
- 01-17-2011, 05:46 AM #7
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
I understand the third example now. But I am curious for the reason of the
author why that kind of initialization. Why not simply put the value needed...
Java Code:import java.io.*; public class FileUtils{ public static void copyFile(File in, File out) throws Exception { FileInputStream fis = new FileInputStream(in); FileOutputStream fos = new FileOutputStream(out); try { byte[] buf = new byte[4096]; int i = 0; while ((i = fis.read(buf)) != -1) { fos.write(buf, 0, i); } } catch (Exception e) { throw e; } finally { if (fis != null) fis.close(); if (fos != null) fos.close(); } } public static void main(String args[]) throws Exception{ FileUtils.copyFile(new File(args[0]),new File(args[1])); } }
- 01-17-2011, 03:46 PM #8
Ohwell, the reason for that is probably just for the viewer - the compiler will optimize the statement, so it won't be any slower, but a flat number is somewhat meaningless to the person reading the code. The break down shows where and how the value is derived.
- 01-18-2011, 12:15 AM #9
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Similar Threads
-
Please Explain me how i got this output.. I am new to java .. so please Explain me...
By vicky82 in forum New To JavaReplies: 2Last Post: 12-13-2010, 01:34 PM -
Please Explain me how i got this output.. I am new to java .. so please Explain me...
By vicky82 in forum New To JavaReplies: 3Last Post: 12-13-2010, 07:22 AM -
Copy file from 1 dir to another dir of the same system
By newjava in forum New To JavaReplies: 5Last Post: 12-23-2009, 11:36 AM -
Copy a file to a folder.
By leric in forum New To JavaReplies: 7Last Post: 07-29-2009, 05:11 AM -
java file copy
By hknyo in forum New To JavaReplies: 1Last Post: 06-12-2008, 04:42 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks