1、读文件
package com.zhi.test; import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.channels.FileChannel; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; /** * 文件读取,缓冲区大小对NIO的性能影响特别大,对BIO无影响<br> * 1024缓冲区大小下:2K的文件,BIO耗时1毫秒,NIO耗时5毫秒;5M的文件,BIO耗时46毫秒,NIO耗时37毫秒<br> * * @author 张远志 * @since 2020年5月9日19:20:49 * */ public class FileRead { /** * 缓冲区大小 */ private static final int BF_SIZE = 1024; /** * 使用BIO读取文件 * * @param fileName 待读文件名 * @return * @throws IOException */ public static String bioRead(String fileName) throws IOException { long startTime = System.currentTimeMillis(); try { FileReader reader = new FileReader(fileName); StringBuffer buf = new StringBuffer(); char[] cbuf = new char[BF_SIZE]; while (reader.read(cbuf) != -1) { buf.append(cbuf); } reader.close(); return buf.toString(); } finally { System.out.println("使用BIO读取文件耗时:" + (System.currentTimeMillis() - startTime) + "毫秒"); } } /** * 使用NIO读取文件 * * @param fileName 待读文件名 * @return * @throws IOException */ public static String nioRead(String fileName) throws IOException { long startTime = System.currentTimeMillis(); try { FileInputStream input = new FileInputStream(fileName); FileChannel channel = input.getChannel(); CharsetDecoder decoder = Charset.defaultCharset().newDecoder(); StringBuffer buf = new StringBuffer(); CharBuffer cBuf = CharBuffer.allocate(BF_SIZE); ByteBuffer bBuf = ByteBuffer.allocate(BF_SIZE); while (channel.read(bBuf) != -1) { bBuf.flip(); decoder.decode(bBuf, cBuf, false); // 解码,byte转char,最后一个参数非常关键 bBuf.clear(); buf.append(cBuf.array(), 0, cBuf.position()); cBuf.compact(); // 压缩 } input.close(); return buf.toString(); } finally { System.out.println("使用NIO读取文件耗时:" + (System.currentTimeMillis() - startTime) + "毫秒"); } } public static void main(String[] args) throws IOException { String fileName = "E:/source.txt"; FileRead.bioRead(fileName); FileRead.nioRead(fileName); } }
2、写文件
package com.zhi.test; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; /** * 文件写<br> * 10M的数据,BIO耗时61毫秒,NIO耗时33毫秒 * * @author 张远志 * @since 2020年5月9日21:04:40 * */ public class FileWrite { /** * 使用BIO进行文件写 * * @param fileName 文件名称 * @param content 待写内存 * @throws IOException */ public static void bioWrite(String fileName, String content) throws IOException { long startTime = System.currentTimeMillis(); try { FileWriter writer = new FileWriter(fileName); writer.write(content); writer.close(); } finally { System.out.println("使用BIO写文件耗时:" + (System.currentTimeMillis() - startTime) + "毫秒"); } } /** * 使用NIO进行文件写 * * @param fileName 文件名称 * @param content 待写内存 * @throws IOException */ public static void nioWrite(String fileName, String content) throws IOException { long startTime = System.currentTimeMillis(); try { FileOutputStream out = new FileOutputStream(fileName); FileChannel channel = out.getChannel(); ByteBuffer buf = ByteBuffer.wrap(content.getBytes()); channel.write(buf); out.close(); } finally { System.out.println("使用NIO写文件耗时:" + (System.currentTimeMillis() - startTime) + "毫秒"); } } public static void main(String[] args) throws IOException { String content = FileRead.nioRead("E:/source.txt"); FileWrite.bioWrite("E:/target1.txt", content); FileWrite.nioWrite("E:/target2.txt", content); } }
3、复制文件
package com.zhi.test; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; /** * 文件复制<br> * 5M的文件,bio耗时29毫秒,nio耗时9毫秒 * * @author 张远志 * @since 2020年5月9日17:18:01 * */ public class FileCopy { /** * 使用BIO复制一个文件 * * @param target 源文件 * @param source 目标文件 * * @throws IOException */ public static void bioCopy(String source, String target) throws IOException { long startTime = System.currentTimeMillis(); FileInputStream fin = new FileInputStream(source); FileOutputStream fout = new FileOutputStream(target); byte[] byt = new byte[1024]; while (fin.read(byt) > -1) { fout.write(byt); } fin.close(); fout.close(); System.out.println("使用BIO复制文件耗时:" + (System.currentTimeMillis() - startTime) + "毫秒"); } /** * 使用NIO复制一个文件 * * @param target 源文件 * @param source 目标文件 * * @throws IOException */ public static void nioCopy(String source, String target) throws IOException { long startTime = System.currentTimeMillis(); FileInputStream fin = new FileInputStream(source); FileChannel inChannel = fin.getChannel(); FileOutputStream fout = new FileOutputStream(target); FileChannel outChannel = fout.getChannel(); inChannel.transferTo(0, inChannel.size(), outChannel); fin.close(); fout.close(); System.out.println("使用NIO复制文件耗时:" + (System.currentTimeMillis() - startTime) + "毫秒"); } public static void main(String[] args) throws IOException { String source = "E:/source.txt", target1 = "E:/target1.txt", target2 = "E:/target2.txt"; FileCopy.bioCopy(source, target1); FileCopy.nioCopy(source, target2); } }
原文:https://www.cnblogs.com/zhi-leaf/p/12860192.html