package partialcopy; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.nio.channels.FileChannel.MapMode; /* * $Id: PartialCopyFile.java 29 2016-06-02 13:29:57Z conan $ */ public class PartialCopyFile { /** * @param args */ public static void main(String[] args) { if (args.length < 3) { System.err.println("PartialCopyFile needs a file name a starting byte and a size"); return; } try { long start, count; RandomAccessFile rin = new RandomAccessFile(args[0], "r"); FileChannel fcin = rin.getChannel(); RandomAccessFile rout = new RandomAccessFile("copy.out","rw"); FileChannel fcout = rout.getChannel(); start = Long.parseLong(args[1]); count = Integer.parseInt(args[2]); MappedByteBuffer bufin = fcin.map(MapMode.READ_ONLY, start, count); MappedByteBuffer bufout = fcout.map(MapMode.READ_WRITE, 0L, count); bufout.put(bufin); } catch (FileNotFoundException fnfe) { System.out.println(fnfe); } catch (IOException ioe) { System.out.println(ioe); } catch (NumberFormatException nfe){ System.out.println(nfe); } } }