企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
课件代码:https://gitee.com/flymini/codes01/tree/master/example_/com-learn-nio02 **** **1. 传统的IO读写文件** ```java public class IOUtils { /** * 文件拷贝 */ public final static void traditionalCopy(String sourcePath, String destPath) throws IOException { File source = new File(sourcePath); File dest = new File(destPath); if (!dest.exists()) { dest.createNewFile(); } FileInputStream fis = new FileInputStream(source); FileOutputStream fos = new FileOutputStream(dest); byte[] buf = new byte[1024]; int len = 0; while ((len = fis.read(buf)) != -1) { fos.write(buf); } fis.close(); fos.close(); } } ``` **2. NIO读写文件** ```java public class NIOFileUtils { /** * 文件拷贝 */ public final static void nioCopy(String sourcePath, String destPath, int allocate) throws IOException { ByteBuffer byteBuffer = ByteBuffer.allocate(allocate); FileInputStream fis = new FileInputStream(sourcePath); FileOutputStream fos = new FileOutputStream(destPath); FileChannel inChannel = fis.getChannel(); FileChannel outChannel = fos.getChannel(); int length = inChannel.read(byteBuffer); while (length != -1) { byteBuffer.flip(); // 读取模式转写入模式 outChannel.write(byteBuffer); byteBuffer.clear(); // 情况缓存,等待下次写入 length = inChannel.read(byteBuffer); } fos.close(); outChannel.close(); fis.close(); inChannel.close(); } /** * 文件拷贝 */ public final static void nioCopy(String sourcePath, String destPath) { File source = new File(sourcePath); File dest = new File(destPath); FileInputStream fis = null; FileOutputStream fos = null; FileChannel inChannel = null; FileChannel outChannel = null; try { fis = new FileInputStream(source); fos = new FileOutputStream(dest); inChannel = fis.getChannel(); outChannel = fos.getChannel(); int position = 0; long size = inChannel.size(); while (size > 0) { //数据超过2G,transferTo方法不能做到一次性转移,所以这里弄个循环 long count = inChannel.transferTo(0, inChannel.size() * 2, outChannel); if (count > 0) { position += count; size -= count; } } } catch (IOException e) { e.printStackTrace(); } finally { try { fis.close(); fos.close(); inChannel.close(); outChannel.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 读文件 */ public final static void read(String file, int allocate) throws IOException { //以只读模式打开文件 RandomAccessFile accessFile = new RandomAccessFile(file, "r"); FileChannel fileChannel = accessFile.getChannel(); ByteBuffer byteBuffer = ByteBuffer.allocate(allocate); CharBuffer charBuffer = CharBuffer.allocate(allocate); Charset charset = Charset.forName("UTF-8"); CharsetDecoder decoder = charset.newDecoder(); int length = fileChannel.read(byteBuffer); while (length != -1) { //读模式转换为写模式 byteBuffer.flip(); decoder.decode(byteBuffer, charBuffer, true); //写模式转换为读模式 System.out.println(charBuffer.toString()); //清空缓存 byteBuffer.clear(); charBuffer.clear(); length = fileChannel.read(byteBuffer); } fileChannel.close(); if (accessFile != null) { accessFile.close(); } } /** * 写文件 */ public final static void write(String context, String file, int allocate) throws IOException { //rws:每当进行写操作,同步的刷新到磁盘,刷新内容和元数据 RandomAccessFile accessFile = new RandomAccessFile(file, "rws"); FileChannel fileChannel = accessFile.getChannel(); ByteBuffer byteBuffer = ByteBuffer.allocate(allocate); //往缓冲区推送数据 byteBuffer.put(context == null ? "".getBytes() : context.getBytes()); //读模式转换为写模式 byteBuffer.flip(); int length = fileChannel.write(byteBuffer); fileChannel.close(); if (accessFile != null) { accessFile.close(); } } } ``` **3. 比较两种方式读写文件的性能** ```java public class MainTest { //CentOS-7.0-1406-x86_64-DVD.iso 文件大约为 4G String sourcePath = "E:\\CentOS-7.0-1406-x86_64-DVD.iso"; String destPath = "E:\\CentOS-7.0-1406-x86_64-DVD-dest.iso"; @Test public void testTraditionalCopy() throws IOException { long start = System.currentTimeMillis(); IOUtils.traditionalCopy(sourcePath, destPath); long end = System.currentTimeMillis(); System.out.println("消耗时间:" + (end - start)); //消耗时间:220100 } @Test public void testNioCopy1() throws IOException { long start = System.currentTimeMillis(); NIOFileUtils.nioCopy(sourcePath, destPath, 1024); long end = System.currentTimeMillis(); System.out.println("消耗时间:" + (end - start)); //消耗时间:37186 } @Test public void testNioCopy2() throws IOException { long start = System.currentTimeMillis(); NIOFileUtils.nioCopy(sourcePath, destPath); long end = System.currentTimeMillis(); System.out.println("消耗时间:" + (end - start)); //消耗时间:71007 } } ``` 很明显,当文件较大时,NIO的优势就越能够体现出来。