Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Java filecopy using NIO

   1  
   2      public static void fileCopy( File in, File out )
   3              throws IOException
   4      {
   5          FileChannel inChannel = new FileInputStream( in ).getChannel();
   6          FileChannel outChannel = new FileOutputStream( out ).getChannel();
   7          try
   8          {
   9  //          inChannel.transferTo(0, inChannel.size(), outChannel);      // original -- apparently has trouble copying large files on Windows
  10  
  11              // magic number for Windows, 64Mb - 32Kb)
  12              int maxCount = (64 * 1024 * 1024) - (32 * 1024);
  13              long size = inChannel.size();
  14              long position = 0;
  15              while ( position < size )
  16              {
  17                 position += inChannel.transferTo( position, maxCount, outChannel );
  18              }
  19          }
  20          finally
  21          {
  22              if ( inChannel != null )
  23              {
  24                 inChannel.close();
  25              }
  26              if ( outChannel != null )
  27              {
  28                  outChannel.close();
  29              }
  30          }
  31      }
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS