国产成人AV无码一二三区,少女1到100集,国产精品久久久久精品综合紧,巜公妇之诱感肉欲HD在线播放

文章 > java基础 > java IO流效率对比

java IO流效率对比

头像

小妮浅浅

2021-02-09 13:44:073857浏览 · 0收藏 · 0评论

本教程操作环境:windows7系统、java10版,DELL G3电脑。

1.缓冲流说明

缓冲流,也叫高效流,是对4个基本的FileXxx 流的增强,所以也是4个流,按照数据类型分类:

  • 字节缓冲流:BufferedInputStream,BufferedOutputStream

  • 字符缓冲流:BufferedReader,BufferedWriter

缓冲流的基本原理,是在创建流对象时,会创建一个内置的默认大小的缓冲区数组,通过缓冲区读写,减少系统IO次数,从而提高读写的效率。

2.字符流和缓冲字符流的对比

public class IOTest {
public static void main(String[] args) throws IOException {
// 数据准备
dataReady();
 
File data = new File("C:/Mu/data.txt");
File a = new File("C:/Mu/a.txt");
File b = new File("C:/Mu/b.txt");
File c = new File("C:/Mu/c.txt");
 
long start = System.currentTimeMillis();
copy(data, a);
long end = System.currentTimeMillis();
 
long start2 = System.currentTimeMillis();
copyChars(data, b);
long end2 = System.currentTimeMillis();
 
long start3 = System.currentTimeMillis();
bufferedCopy(data, c);
long end3 = System.currentTimeMillis();
 
System.out.println("普通字节流1耗时:" + (end - start) + " ms,文件大小:" + a.length() / 1024 + " kb");
System.out.println("普通字节流2耗时:" + (end2 - start2) + " ms,文件大小:" + b.length() / 1024 + " kb");
System.out.println("缓冲字节流耗时:" + (end3 - start3) + " ms,文件大小:" + c.length() / 1024 + " kb");
}
 
// 普通字符流不使用数组
public static void copy(File in, File out) throws IOException {
Reader reader = new FileReader(in);
Writer writer = new FileWriter(out);
 
int ch = 0;
while ((ch = reader.read()) != -1) {
writer.write((char) ch);
}
reader.close();
writer.close();
}
 
// 普通字符流使用字符流
public static void copyChars(File in, File out) throws IOException {
Reader reader = new FileReader(in);
Writer writer = new FileWriter(out);
 
char[] chs = new char[1024];
while ((reader.read(chs)) != -1) {
writer.write(chs);
}
reader.close();
writer.close();
}
 
// 缓冲字符流
public static void bufferedCopy(File in, File out) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(in));
BufferedWriter bw = new BufferedWriter(new FileWriter(out));
 
String line = null;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
 
// 释放资源
bw.close();
br.close();
}
 
// 数据准备
public static void dataReady() throws IOException {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 600000; i++) {
sb.append("abcdefghijklmnopqrstuvwxyz");
}
OutputStream os = new FileOutputStream(new File("C:/Mu/data.txt"));
os.write(sb.toString().getBytes());
 
os.close();
System.out.println("完毕");
}
}

运行结果:

普通字符流1耗时:1337 ms,文件大小:15234 kb
普通字符流2耗时:82 ms,文件大小:15235 kb
缓冲字符流耗时:205 ms,文件大小:15234 kb

以上是java IO流效率的对比实例,缓冲字符流相对而言的优势并不是那么大,这是因为在使用上我们以缓冲流的方法用的居多,想要学习的可以在课外找寻资料。

关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

《厨房激战5》完整版在线观看| 免费观看已满十八岁在线播放电视. | 瑜伽馆里的私密按摩效果怎么样| 《法国空乘8》电影| AAAAABBBBBB级是什么.| 欢愉骑行1-27章内容概括| 77777免费观看电视剧推荐适.| 男生把小困困放到女生困里视频| 《一滴都不许漏》何泽城林荫| 女性抹胸开襟哺乳衣的注意事项 | 金银瓶4葡萄熟了| 女版壮志凌云满天星版| 姐姐片多多免费高清电视剧叶玉卿| 驾校情缘(老树开花)孙潇笔趣.| 五姑娘影院在线观看免费版电视剧| 国产在线观看免费全集电视剧网站| 免费网站在线观看人数在哪省| EXO妈妈MV高清视频播放| 麻花传MV在线观看第15集预告| 男女裸体做受XXXX性视频直播| 亚洲日韩AV精品一区二区| -区二三国产好的精华波品牌介绍 精品卡一卡二卡3卡高清乱码 | 公交车掀开奶罩边躁狠狠躁漫画| 俩个人做AJ的视频教程大全| 男生把小困因放到女生困里视频| 放荡的女兵伦理2| 男男| 内衣秀| 三个领导换着躁我几小时在线观看| 《家访的老师》动漫在线观看| 电影《偷吃》在线观看| 美女和帅哥一起努力生产豆浆| 明星记忆修改系统| 来吧儿子妈妈是纯还是混| 美女国模大尺度辨阴| 豪门贵妇猎美录TXT| 成色18K1.8.35MB| 女生穿露胸开又裙穿搭大忌| 人犬兽人在线观看免费完整| 《太太你想丈夫被开除吧》| 法国《监狱伦理3》