java的新I/O流,有点意思,看看相关的类,其实就这几个就能解决解了。对于Buffer与ByteBuffer比较重要,看看来自JDK的说法http://happyprince.iteye.com/blog/1692137,对于ByteChallnel的FileChannel都是来自ReadByteChannel与WriteByteChannel的方法。ByteChannel中没有什么方法的。
具体一点把JDK弄过来查一查,如下(FileChannel的方法):
例子(两个文件的复制):
String fname1 = "src/data.txt", fname2 = "src/data2.txt";
File f1 = new File(fname1), f2 = new File(fname2);
FileChannel in = new FileInputStream(f1).getChannel();
FileChannel out = new FileOutputStream(f2).getChannel();
ByteBuffer b = ByteBuffer.allocate(1024);
while (in.read(b) != -1) {
b.flip();
out.write(b);
b.clear();
}
in.close() ;
out.close() ;
另一个例子也是文件的复制:
FileChannel inn = new FileInputStream("src/data.txt").getChannel(),
outt = new FileOutputStream("src/data1.txt").getChannel();
inn.transferTo(0, inn.size(), outt);
inn.close() ;
outt.close() ;
两个都可实现文件的复制功能。