public static void main(String[] args) throws IOException { RandomAccessFile file = new RandomAccessFile("1.png", "rw"); FileChannel channel = file.getChannel(); RandomAccessFile file1 = new RandomAccessFile("2.png", "rw"); FileChannel channel1 = file1.getChannel(); ByteBuffer buf = ByteBuffer.allocateDirect(32); for (; channel.read(buf) != -1;) { buf.flip(); for (; buf.hasRemaining();) { channel1.write(buf); } buf.clear(); } channel.close(); channel1.close(); file.close(); file1.close(); }
public static void main(String[] args) throws IOException { // RandomAccessFile file = new RandomAccessFile("1.png", "rw"); FileChannel channel = FileChannel.open(Paths.get("1.png"), StandardOpenOption.READ,StandardOpenOption.WRITE); // RandomAccessFile file1 = new RandomAccessFile("2.png", "rw"); FileChannel channel1 = FileChannel.open(Paths.get("5.png"), StandardOpenOption.READ,StandardOpenOption.WRITE,StandardOpenOption.CREATE); MappedByteBuffer buf = channel.map(MapMode.READ_WRITE, 0, channel.size()); MappedByteBuffer buf1 = channel1.map(MapMode.READ_WRITE, 0, channel.size()); // ByteBuffer buf = ByteBuffer.allocateDirect(32); // for (; channel.read(buf) != -1;) { // buf.flip(); // // for (; buf.hasRemaining();) { // channel1.write(buf); // } // // buf.clear(); // } byte[] dst = new byte[buf.limit()]; buf.get(dst); buf1.put(dst); channel.close(); channel1.close(); // file.close(); // file1.close(); }