一、使用NIO复制文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package javanio; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.util.Scanner; public class CopyFile { public static void main(String[] args) throws IOException { CopyFile cf = new CopyFile(); cf.startCopy(); } public void startCopy() throws IOException { /* 默认目标文件路径, "." 表示在当前工程目录下 */ String des = ".\\src\\javanio\\Test.txt"; System.out.println("请输入源文件路径:"); Scanner sc = new Scanner(System.in); String src = sc.next(); sc.close(); File srcFile = new File(src); File desFile = new File(des); if (!srcFile.exists()) { throw new RuntimeException("源文件不存在!"); } if (!srcFile.canRead()) { throw new RuntimeException("源文件不可读!"); } if (!srcFile.exists()) { throw new RuntimeException("源文件不存在!"); } if (!srcFile.canRead()) { throw new RuntimeException("源文件不可读!"); } /* 获取源文件和目标文件的输入输出流 */ FileInputStream fis = new FileInputStream(srcFile); FileOutputStream fos = new FileOutputStream(desFile); /* 获取输入输出通道 */ FileChannel fisChannel = fis.getChannel(); FileChannel fosChannel = fos.getChannel(); /* 创建缓冲区 */ ByteBuffer buffer = ByteBuffer.allocate(1024); while (true) { // clear方法重设缓冲区,使它可以接受读入的数据 buffer.clear(); // 从输入通道中将数据读到缓冲区 int r = fisChannel.read(buffer); // read方法返回读取的字节数,可能为零,如果该通道已到达流的末尾,则返回-1 if (r == -1) { break; } // flip方法让缓冲区可以将新读入的数据写入另一个通道 buffer.flip(); // 从输出通道中将数据写入缓冲区 fosChannel.write(buffer); } if (desFile.exists()) { System.out.println("复制成功!"); } fis.close(); fos.close(); } }
二、IO与NIO比较
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package javanio; import java.io.File; import java.io.FileInputStream; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class CompIO { private static String PATH = ".\\src\\javanio\\Test.txt"; public static void main(String[] args) throws Exception { useIO(); useNIO(); } private static void useIO() throws Exception { File file = new File(PATH); FileInputStream in = new FileInputStream(file); byte[] b = new byte[1024]; in.read(b); in.close(); System.out.println(new String(b, "UTF-8")); } private static void useNIO() throws Exception { File file = new File(PATH); FileInputStream in = new FileInputStream(file); FileChannel channel = in.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(1024); channel.read(buffer); byte[] b = buffer.array(); in.close(); System.out.println(new String(b, "UTF-8")); } }
三、NIO
a、区别
面向流的I/O系统一次一个字节处理数据。一个输入流产生一个字节数据,一个输出流消费一个字节数据。
NIO使用块I/O的处理方式。每一个操作都在一步中产生或者消费一个数据块。按块处理数据比按字节处理数据要快。
b、通道
Channel是对原I/O包中的流模拟。到任何目的地或来自任何地方的所有数据都必须通过一个Channel对象。
c、缓冲区
Buffer实质是一个容器对象。所有数据的处理都在缓冲区中。发送给一个通道的所有对象都必须先发到缓冲区中。同样,从通道中读取的任何数据都要读到缓冲区中。
缓冲区类型:ByteBuffer、CharBuffer、IntBuffer、FloatBuffer等等。
d、读取
1.从FileInputStream获取Channel。
2.创建Buffer。
3.将数据从Channel中读到Buffer中。
e、写入
1.从FileOutputStream中获取一个通道。
2.创建缓冲区,并在其中放入数据。
3.写入缓冲区中。
来源:https://www.cnblogs.com/wscy/p/4753238.html