Results 1 to 9 of 9
  1. #1
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    4

    Default 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..

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    So which example is this?

  3. #3
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    4

    Default

    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.
    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]));
      }
    }
    I dont understand what is maxCount for, why such operation "(64 * 1024 * 1024) - (32 * 1024)"...

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    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.
    long position = 0;
    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 :D

  5. #5
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    4

    Default

    Thanks for that explanation.

    Java Code:
        int i = 480;
        [b]//Is this the same with?[/b]
        int i = (128 * 2 * 2) - (16 * 2)

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    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)

  7. #7
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    4

    Default

    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...



    Quote Originally Posted by quad64bit View Post
    When I have done file copy in the past, I pick an arbitrary buffer size such as 4MB.
    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]));
      }
    }

  8. #8
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    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.

  9. #9
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    4

    Default

    Thanks for your time. I will mark it solved. Thanks again. :)

Similar Threads

  1. Replies: 2
    Last Post: 12-13-2010, 01:34 PM
  2. Replies: 3
    Last Post: 12-13-2010, 07:22 AM
  3. Replies: 5
    Last Post: 12-23-2009, 11:36 AM
  4. Copy a file to a folder.
    By leric in forum New To Java
    Replies: 7
    Last Post: 07-29-2009, 05:11 AM
  5. java file copy
    By hknyo in forum New To Java
    Replies: 1
    Last Post: 06-12-2008, 04:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •