Show
with line numbers
// Demo of using the java.nio package.
// This application copies a file, efficiently.
// (Note the buffer size chosen; If you know your operating
// system's block (or "cluster") size, using that will be the
// most efficient.
//
// Written 1/2009 by Wayne Pollock, Tampa Florida USA
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
public class NioDemo
{
private static final int BUF_SIZE = 4096;
public static void main ( String [] args ) throws Exception
{
if ( args.length != 2 )
{ System.err.println( "Usage: NioDemo inputFile outputFile" );
return;
}
FileInputStream fin = new FileInputStream( args[0] );
FileChannel cin = fin.getChannel();
FileOutputStream fout = new FileOutputStream( args[1] );
FileChannel cout = fout.getChannel();
ByteBuffer buf = ByteBuffer.allocate( BUF_SIZE );
System.out.print( "Copying \"" + args[0] + "\" to \""
+ args[1] + "\":." );
buf.clear();
while ( cin.read(buf) != -1 ) {
System.out.print( "." );
buf.flip();
cout.write(buf);
buf.clear();
}
System.out.println( "Done!" );
fin.close(); fout.close();
}
}