国产成人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》完整版| T大校花沉浮录续集TXT| 向日葵在线观看免费高清电视剧| 欧洲尺码少女16集全电视剧剧情.. | 《冲动的惩罚》免费全集| 《妻子为丈夫升职》| 快乐8| 坤坤寒进桃子里在线看歌词 | 自慰被发现然后被C了H漫画| 《厨房韵母2》动漫下线了吗| 五姑娘影院在线观看免费版电视剧 | 三陪| 《无能的丈夫》完整版| 欧洲最强女RAPPER免费| 父母儿女一家狂第八集| 双飞儿媳妇和她闺蜜免费阅读 | 免费观看60分钟高清电视剧 | 小莹的性荡生活第15章| 《高压监狱2》完整版| 护士二姐妹精神诊所| 鹿鞭丸的功效和作用| 继夫不戴套挺进我的身体H漫画 | 春闺艳妇(古言)作者:年富一年| 麻花传剧原创MV免费播放小米车 | 妈妈装睡配合孩子阴阳调和| 特殊按摩治疗师| 胸吻| 有夫之妇精油按摩的功效与作用| 女性扒开大腿内侧小肚子痒| 金枝玉叶大结局| 人野兽马狗猪大全| 国精产品一品二品国精HTC| 下面的嘴可比上面的诚实| 儿媳婉柔和父亲王大川的背景故事| 《慈母夜吟》完整版| 美女和帅哥一起努力生产豆浆| 正常硬度是多硬| 公天天吃我奶躁我的在| 《伦敦空姐美版2023》| 性感内衣女装| 魅惑摄影