使用SeekableByteChannel从开始到结束复制文件的一部分
import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.SeekableByteChannel; import java.nio.file.Files;// 来 自 N o w J a v a . c o m - 时 代 Java import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.EnumSet; public class Main { public static void main(String[] args) { Path path = Paths.get("C:/folder1/folder2/folder4", "test.txt"); ByteBuffer copy = ByteBuffer.allocate(25); copy.put("\ntest".getBytes()); try (SeekableByteChannel seekableByteChannel = (Files.newByteChannel(path, /** NowJava.com - 时 代 Java 提供 **/ EnumSet.of(StandardOpenOption.READ, StandardOpenOption.WRITE)))) { int nbytes; do { nbytes = seekableByteChannel.read(copy); } while (nbytes != -1 && copy.hasRemaining()); copy.flip(); seekableByteChannel.positi