电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

Java使用BIO和NIO進行文件操作對比代碼示例

瀏覽:4日期:2022-09-01 17:20:28

什么是Java NIO?

同步非阻塞io模式,拿燒開水來說,NIO的做法是叫一個線程不斷的輪詢每個水壺的狀態,看看是否有水壺的狀態發生了改變,從而進行下一步的操作。Java NIO有三大組成部分:Buffer,Channel,Selector,通過事件驅動模式實現了什么時候有數據可讀的問題。

什么是Java BIO?

同步阻塞IO模式,數據的讀取寫入必須阻塞在一個線程內等待其完成。這里使用那個經典的燒開水例子,這里假設一個燒開水的場景,有一排水壺在燒開水,BIO的工作模式就是, 叫一個線程停留在一個水壺那,直到這個水壺燒開,才去處理下一個水壺。但是實際上線程在等待水壺燒開的時間段什么都沒有做。不知道io操作中什么時候有數據可讀,所以一直是阻塞的模式。

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;import java.nio.file.Files;import java.nio.file.Paths;/** * 文件讀取,緩沖區大小(BF_SIZE)對NIO的性能影響特別大,對BIO無影響<br> * 10M的文件,BIO耗時87毫秒,NIO耗時68毫秒,Files.read耗時62毫秒 * * @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 nioRead1(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) + '毫秒'); } } /** * 使用Files.read讀取文件 * * @param fileName 待讀文件名 * @return * @throws IOException */ public static String nioRead2(String fileName) throws IOException { long startTime = System.currentTimeMillis(); try { byte[] byt = Files.readAllBytes(Paths.get(fileName)); return new String(byt); } finally { System.out.println('使用Files.read讀取文件耗時:' + (System.currentTimeMillis() - startTime) + '毫秒'); } } public static void main(String[] args) throws IOException { String fileName = 'E:/source.txt'; FileRead.bioRead(fileName); FileRead.nioRead1(fileName); FileRead.nioRead2(fileName); }}

2、寫文件

package com.zhi.test;import java.io.File;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;import java.nio.ByteBuffer;import java.nio.channels.FileChannel;import java.nio.file.Files;import java.nio.file.StandardOpenOption;/** * 文件寫<br> * 10M的數據,BIO耗時45毫秒,NIO耗時42毫秒,Files.write耗時24毫秒 * * @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 nioWrite1(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) + '毫秒'); } } /** * 使用Files.write進行文件寫 * * @param fileName 文件名稱 * @param content 待寫內存 * @throws IOException */ public static void nioWrite2(String fileName, String content) throws IOException { long startTime = System.currentTimeMillis(); try { File file = new File(fileName); if (!file.exists()) {file.createNewFile(); } Files.write(file.toPath(), content.getBytes(), StandardOpenOption.WRITE); } finally { System.out.println('使用Files.write寫文件耗時:' + (System.currentTimeMillis() - startTime) + '毫秒'); } } public static void main(String[] args) throws IOException { String content = FileRead.nioRead2('E:/source.txt'); String target1 = 'E:/target1.txt', target2 = 'E:/target2.txt', target3 = 'E:/target3.txt'; FileWrite.bioWrite(target1, content); FileWrite.nioWrite1(target2, content); FileWrite.nioWrite2(target3, content); }}

3、復制文件

package com.zhi.test;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.nio.channels.FileChannel;import java.nio.file.Files;import java.nio.file.Paths;/** * 文件復制<br> * 10M的文件,bio耗時56毫秒,nio耗時12毫秒,Files.copy耗時10毫秒 * * @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(); try { 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(); } finally { System.out.println('使用BIO復制文件耗時:' + (System.currentTimeMillis() - startTime) + '毫秒'); } } /** * 使用NIO復制一個文件 * * @param target 源文件 * @param source 目標文件 * * @throws IOException */ public static void nioCopy1(String source, String target) throws IOException { long startTime = System.currentTimeMillis(); try { 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(); } finally { System.out.println('使用NIO復制文件耗時:' + (System.currentTimeMillis() - startTime) + '毫秒'); } } /** * 使用Files.copy復制一個文件 * * @param target 源文件 * @param source 目標文件 * * @throws IOException */ public static void nioCopy2(String source, String target) throws IOException { long startTime = System.currentTimeMillis(); try { File file = new File(target); if (file.exists()) {file.delete(); } Files.copy(Paths.get(source), file.toPath()); } finally { System.out.println('使用Files.copy復制文件耗時:' + (System.currentTimeMillis() - startTime) + '毫秒'); } } public static void main(String[] args) throws IOException { String source = 'E:/source.txt'; String target1 = 'E:/target1.txt', target2 = 'E:/target2.txt', target3 = 'E:/target3.txt'; FileCopy.bioCopy(source, target1); FileCopy.nioCopy1(source, target2); FileCopy.nioCopy2(source, target3); }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 无机纤维喷涂棉-喷涂棉施工工程-山东华泉建筑工程有限公司▲ | 滑板场地施工_极限运动场地设计_滑板公园建造_盐城天人极限运动场地建设有限公司 | 立式硫化罐-劳保用品硫化罐-厂家直销-山东鑫泰鑫硫化罐厂家 | 电机铸铝配件_汽车压铸铝合金件_发动机压铸件_青岛颖圣赫机械有限公司 | 盐城网络公司_盐城网站优化_盐城网站建设_盐城市启晨网络科技有限公司 | 挤塑板-XPS挤塑板-挤塑板设备厂家[襄阳欧格] | 精密线材测试仪-电线电缆检测仪-苏州欣硕电子科技有限公司 | 【365公司转让网】公司求购|转让|资质买卖_股权转让交易平台 | 周易算网-八字测算网 - 周易算网-宝宝起名取名测名字周易八字测算网 | 飞行者联盟-飞机模拟机_无人机_低空经济_航空技术交流平台 | 气动隔膜泵厂家-温州永嘉定远泵阀有限公司 | 海鲜池-专注海鲜鱼缸、移动海鲜缸、饭店鱼缸设计定做-日晟水族厂家 | ◆大型吹塑加工|吹塑加工|吹塑代加工|吹塑加工厂|吹塑设备|滚塑加工|滚塑代加工-莱力奇塑业有限公司 | 电脑刺绣_绣花厂家_绣花章仔_织唛厂家-[源欣刺绣]潮牌刺绣打版定制绣花加工厂家 | 深圳快餐店设计-餐饮设计公司-餐饮空间品牌全案设计-深圳市勤蜂装饰工程 | 电缆桥架生产厂家_槽式/梯式_热镀锌线槽_广东东莞雷正电气 | 扬州汇丰仪表有限公司 | 磨煤机配件-高铬辊套-高铬衬板-立磨辊套-盐山县宏润电力设备有限公司 | 彩信群发_群发彩信软件_视频短信营销平台-达信通 | 嘉兴泰东园林景观工程有限公司_花箱护栏 | 软文发布-新闻发布推广平台-代写文章-网络广告营销-自助发稿公司媒介星 | 安徽合肥格力空调专卖店_格力中央空调_格力空调总经销公司代理-皖格制冷设备 | 空气能暖气片,暖气片厂家,山东暖气片,临沂暖气片-临沂永超暖通设备有限公司 | 众品家具网-家具品牌招商_家具代理加盟_家具门户的首选网络媒体。 | 印刷人才网 印刷、包装、造纸,中国80%的印刷企业人才招聘选印刷人才网! | 除尘布袋_液体过滤袋_针刺毡滤料-杭州辉龙过滤技术有限公司 | 打包箱房_集成房屋-山东佳一集成房屋有限公司 | 华禹护栏|锌钢护栏_阳台护栏_护栏厂家-华禹专注阳台护栏、楼梯栏杆、百叶窗、空调架、基坑护栏、道路护栏等锌钢护栏产品的生产销售。 | 郑州墨香品牌设计公司|品牌全案VI设计公司| 实验室pH计|电导率仪|溶解氧测定仪|离子浓度计|多参数水质分析仪|pH电极-上海般特仪器有限公司 | 电位器_轻触开关_USB连接器_广东精密龙电子科技有限公司 | 深圳美安可自动化设备有限公司,喷码机,定制喷码机,二维码喷码机,深圳喷码机,纸箱喷码机,东莞喷码机 UV喷码机,日期喷码机,鸡蛋喷码机,管芯喷码机,管内壁喷码机,喷码机厂家 | 首页|成都尚玖保洁_家政保洁_开荒保洁_成都保洁 | 智能交通网_智能交通系统_ITS_交通监控_卫星导航_智能交通行业 | 钢托盘,钢制托盘,立库钢托盘,金属托盘制造商_南京飞天金属制品实业有限公司 | 智能交通网_智能交通系统_ITS_交通监控_卫星导航_智能交通行业 | 北京网络营销推广_百度SEO搜索引擎优化公司_网站排名优化_谷歌SEO - 北京卓立海创信息技术有限公司 | 汽液过滤网厂家_安平县银锐丝网有限公司 | 深圳网站建设-高端企业网站开发-定制网页设计制作公司 | 家用净水器代理批发加盟_净水机招商代理_全屋净水器定制品牌_【劳伦斯官网】 | 山东彩钢板房,山东彩钢活动房,临沂彩钢房-临沂市贵通钢结构工程有限公司 |