| 11 | public class ChannelCopy { |
| 12 | private static final int BSIZE = 1024; |
| 13 | public static void main(String[] args) { |
| 14 | if(args.length != 2) { |
| 15 | System.out.println( |
| 16 | "arguments: sourcefile destfile"); |
| 17 | System.exit(1); |
| 18 | } |
| 19 | try( |
| 20 | FileChannel in = new FileInputStream( |
| 21 | args[0]).getChannel(); |
| 22 | FileChannel out = new FileOutputStream( |
| 23 | args[1]).getChannel() |
| 24 | ) { |
| 25 | ByteBuffer buffer = ByteBuffer.allocate(BSIZE); |
| 26 | while(in.read(buffer) != -1) { |
| 27 | buffer.flip(); // Prepare for writing |
| 28 | out.write(buffer); |
| 29 | buffer.clear(); // Prepare for reading |
| 30 | } |
| 31 | } catch(IOException e) { |
| 32 | throw new RuntimeException(e); |
| 33 | } |
| 34 | } |
| 35 | } |