当前位置: 首页 > news >正文

IO缓冲流

目录

字节缓冲流 

字符缓冲流 

构造方法

字符缓冲流特有方法

BufferedReader(Reader r) 

Bufferedwriter(Writer r)

 综合练习

 1、四种方式拷贝文件,并统计各自用时字节流的基本流:一次读写一个字节

 2、恢复出师表的顺序

3、当程序运行超过3次时给出提示:本软件只能免费使用3次,欢迎您注册会员后继续使用



字节缓冲流 

public BufferedInputStream(InputStreamis)        把基本流包装成高级流,提高读取数据的性能
public BufferedOutputStream(OutputStream os)        把基本流包装成高级流,提高写出数据的性能

底层自带了长度为8192的缓冲区提高性能 

利用字节缓冲流拷贝文件

package BufferedStream;import java.io.*;public class BufferedStreamDemo1 {public static void main(String[] args) throws IOException {BufferedInputStream bis=new BufferedInputStream(new FileInputStream("..\\Myio\\a.txt"));BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("..\\Myio\\b.txt"));int len=0;while ((len=bis.read())!=-1){bos.write(len);}bis.close();bos.close();}
}

 

一次读多个字节👇

package BufferedStream;import java.io.*;public class BufferedStreamDemo2 {public static void main(String[] args) throws IOException {//创建缓冲流对象BufferedInputStream bis=new BufferedInputStream(new FileInputStream("..\\Myio\\a.txt"));BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("..\\Myio\\b.txt"));//拷贝,一次读多个字节byte[] bytes=new byte[1024];int len=0;while ((len=bis.read(bytes))!=-1){bos.write(bytes,0,len);}//释放资源bis.close();bos.close();}
}

字符缓冲流 

构造方法

public BufferedReader(Reader r)        把基本流变成高级流
public Bufferedwriter(Writer r)        把基本流变成高级流

字符缓冲流特有方法

字符缓冲输入流特有方法

public string readLine()               读取一行数据,!如果没有数据可读了,会返回null

字符缓冲输出流特有方法

public void newLine()                跨平台的换行 

Mac:\r   Linux: \n   Windows:\r\n 

BufferedReader(Reader r) 

package BufferedStream;import java.io.*;public class BufferedStreamDemo3 {public static void main(String[] args) throws IOException {//创建字符缓冲输入流对象BufferedReader br=new BufferedReader(new FileReader("..\\Myio\\a.txt"));//读取数据String s1 = br.readLine();System.out.println(s1);String s2 = br.readLine();System.out.println(s2);//释放资源br.close();}
}

 

readLine方法在读取的时候,一次读一整行,遇到回车换行结束,但是他不会把回车换行谈到内存当中

这里我把输出语句的ln删了

他就打印到一整行了

循环读取👇

package BufferedStream;import java.io.*;public class BufferedStreamDemo3 {public static void main(String[] args) throws IOException {//创建字符缓冲输入流对象BufferedReader br=new BufferedReader(new FileReader("..\\Myio\\a.txt"));//循环读取数据String s;while ((s=br.readLine())!=null){System.out.println(s);}//释放资源br.close();}
}

Bufferedwriter(Writer r)

package BufferedStream;import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;public class BufferedStreamDemo4 {public static void main(String[] args) throws IOException {BufferedWriter bw=new BufferedWriter(new FileWriter("c.txt"));bw.write("努力遇见更好的自己");bw.newLine();bw.write("加油哦");bw.close();}
}

 

 如果要打开续写功能的话,要像下面这样 

在new FileWriter中加入true 

 综合练习

 1、四种方式拷贝文件,并统计各自用时
字节流的基本流:一次读写一个字节

package IOTest;import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;public class Test3 {public static void main(String[] args) throws IOException {long start=System.currentTimeMillis();method1();long end=System.currentTimeMillis();System.out.println((end-start)/1000.0+"秒");}public static void method1() throws IOException {FileInputStream fis=new FileInputStream("E:\\111\\c.jpg");FileOutputStream fos=new FileOutputStream("..\\Myio\\d.jpg");int b=0;while((b=fis.read())!=-1){fos.write(b);}fos.close();fis.close();}}

字节流的基本流:一次读写一个字节数组

public class Test3 {public static void main(String[] args) throws IOException {long start=System.currentTimeMillis();method3();long end=System.currentTimeMillis();System.out.println((end-start)/1000.0+"秒");}
public static void method2() throws IOException {FileInputStream fis=new FileInputStream("E:\\111\\c.jpg");FileOutputStream fos=new FileOutputStream("..\\Myio\\d.jpg");byte[] bytes=new byte[8192];int len=0;while ((len=fis.read(bytes))!=-1){fos.write(bytes,0,len);}fos.close();fis.close();}
}


字节缓冲流:一次读写一个字节

public class Test3 {public static void main(String[] args) throws IOException {long start=System.currentTimeMillis();method3();long end=System.currentTimeMillis();System.out.println((end-start)/1000.0+"秒");}
public static void method3() throws IOException {BufferedInputStream bis=new BufferedInputStream(new FileInputStream("E:\\111\\c.jpg"));BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("..\\Myio\\d.jpg"));int len=0;while ((len=bis.read())!=-1){bos.write(len);}bis.close();bos.close();}
}


字节缓冲流:一次读写一个字节数组

public class Test3 {public static void main(String[] args) throws IOException {long start=System.currentTimeMillis();method4();long end=System.currentTimeMillis();System.out.println((end-start)/1000.0+"秒");}
public static void method4() throws IOException {BufferedInputStream bis=new BufferedInputStream(new FileInputStream("E:\\111\\c.jpg"));BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("..\\Myio\\d.jpg"));byte[] bytes=new byte[8192];int len=0;while ((len=bis.read(bytes))!=-1){bos.write(bytes,0,len);}bis.close();bos.close();}}

 2、恢复出师表的顺序

可以拿去试一试哦👇

4.将军向宠,性行淑均,晓畅军事,试用于昔日,先帝称之曰“能”,是以众议举宠为督:愚以为营中之事,悉以咨之,必能使行阵和睦,优劣得所。
2.宫中府中,俱为一体;陟罚臧否,不宜异同:若有作奸犯科及为忠善者,宜付有司论其刑赏,以昭陛下平明之理;不宜偏私,使内外异法也。
9.今当远离,临表涕零,不知所言。
5.亲贤臣,远小人,此先汉所以兴隆也;亲小人,远贤臣,此后汉所以倾颓也。先帝在时,每与臣论此事,未尝不叹息痛恨于桓、灵也。侍中、尚书、长史、参军,此悉贞良死节之臣,愿陛下亲之、信之,则汉室之隆,可计日而待也。
1.先帝创业未半而中道崩殂,今天下三分,益州疲弊,此诚危急存亡之秋也。然侍卫之臣不懈于内,忠志之士忘身于外者,盖追先帝之殊遇,欲报之于陛下也。诚宜开张圣听,以光先帝遗德,恢弘志士之气,不宜妄自菲薄,引喻失义,以塞忠谏之路也。
3.侍中、侍郎郭攸之、费祎、董允等,此皆良实,志虑忠纯,是以先帝简拔以遗陛下:愚以为宫中之事,事无大小,悉以咨之,然后施行,必能裨补阙漏,有所广益。
7.先帝知臣谨慎,故临崩寄臣以大事也。受命以来,夙夜忧叹,恐托付不效,以伤先帝之明;故五月渡泸,深入不毛。今南方已定,兵甲已足,当奖率三军,北定中原,庶竭驽钝,攘除奸凶,兴复汉室,还于旧都。此臣所以报先帝而忠陛下之职分也。至于斟酌损益,进尽忠言,则攸之、祎、允之任也。
8.愿陛下托臣以讨贼兴复之效,不效,则治臣之罪,以告先帝之灵。若无兴德之言,则责攸之、祎、允等之慢,以彰其咎;陛下亦宜自谋,以咨诹善道,察纳雅言,深追先帝遗诏。臣不胜受恩感激。
6.臣本布衣,躬耕于南阳,苟全性命于乱世,不求闻达于诸侯。先帝不以臣卑鄙,猥自枉屈,三顾臣于草庐之中,咨臣以当世之事,由是感激,遂许先帝以驱驰。后值倾覆,受任于败军之际,奉命于危难之间:尔来二十有一年矣。
package IOTest;import java.io.*;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;public class Test4 {public static void main(String[] args) throws IOException {//读取数据BufferedReader br=new BufferedReader(new FileReader("..\\Myio\\a.txt"));String str;TreeMap<Integer,String>tm=new TreeMap<>();while ((str=br.readLine())!=null){String[] arr = str.split("、");//把数据先放入TreeMap中//数组下标0为序号,1为内容tm.put(Integer.parseInt(arr[0]),arr[1]);}br.close();//写出数据BufferedWriter bw=new BufferedWriter(new FileWriter("..\\Myio\\b.txt"));Set<Map.Entry<Integer, String>> entries = tm.entrySet();for (Map.Entry<Integer, String> entry : entries) {String value = entry.getValue();bw.write(value);bw.newLine();}bw.close();}
}

3、当程序运行超过3次时给出提示:本软件只能免费使用3次,欢迎您注册会员后继续使用

程序运行演示如下:
第一次运行控制台输出:欢迎使用本软件,第1次使用免费~
第二次运行控制台输出:欢迎使用本软件,第2次使用免费~
第三次运行控制台输出:欢迎使用本软件,第3次使用免费~
第四次及之后运行控制台输出:本软件只能免费使用3次,欢迎您注册会员后继续使用~ 

 

把计数器的次数存在本地文件里

package IOTest;import java.io.*;public class Test5 {public static void main(String[] args) throws IOException {BufferedReader br=new BufferedReader(new FileReader("..\\Myio\\c.txt"));String line = br.readLine();int count=Integer.parseInt(line);//表示当前程序运行了一次count++;//判断if(count<=3){System.out.println("欢迎使用本软件,第"+count+"次使用免费~");}else{System.out.println("本软件只能免费使用3次,欢迎您注册会员后继续使用~");}//把当前自增之后的count写出到文件中BufferedWriter bw=new BufferedWriter(new FileWriter("..\\Myio\\c.txt"));bw.write(count+"");br.close();bw.close();}
}

 


缓冲流就说到这里啦!

努力遇见更好的自己!!!

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • react基础学习 JSX
  • 2002NOIP普及组真题 4. 过河卒
  • kafka-消费者-消费异常处理(SpringBoot整合Kafka)
  • 短信测压APP
  • 保姆级讲解 Linux下FTP服务器的搭建、配置与管理
  • 【Vue】获取模块内的mutations方法
  • 上位机图像处理和嵌入式模块部署(f407 mcu和其他mcu品类的选择)
  • 计算机网络 | 第二章 物理层 | 王道考研自用笔记
  • 社交创新:Facebook的技术与产品发展
  • window.open(“.html“,“_blank“) 执行是下载,并没有打开新窗口显示html
  • 机器学习-- 如何清洗数据集
  • 利用CSS隐藏HTML元素并插入替代内容
  • 超详解——python数字和运算——小白篇
  • 【数据结构】【版本1.0】【线性时代】——顺序表
  • 网络安全技术实验一 信息收集和漏洞扫描
  • 2017届校招提前批面试回顾
  • android高仿小视频、应用锁、3种存储库、QQ小红点动画、仿支付宝图表等源码...
  • Git同步原始仓库到Fork仓库中
  • Java 最常见的 200+ 面试题:面试必备
  • javascript面向对象之创建对象
  • PHP 小技巧
  • vue中实现单选
  • webgl (原生)基础入门指南【一】
  • 测试如何在敏捷团队中工作?
  • 关于Android中设置闹钟的相对比较完善的解决方案
  • 日剧·日综资源集合(建议收藏)
  • 小程序测试方案初探
  • 云大使推广中的常见热门问题
  • # Swust 12th acm 邀请赛# [ K ] 三角形判定 [题解]
  • #数学建模# 线性规划问题的Matlab求解
  • (一)80c52学习之旅-起始篇
  • (转)socket Aio demo
  • *++p:p先自+,然后*p,最终为3 ++*p:先*p,即arr[0]=1,然后再++,最终为2 *p++:值为arr[0],即1,该语句执行完毕后,p指向arr[1]
  • .NET Core MongoDB数据仓储和工作单元模式封装
  • .net framework4与其client profile版本的区别
  • .net framwork4.6操作MySQL报错Character set ‘utf8mb3‘ is not supported 解决方法
  • .NET gRPC 和RESTful简单对比
  • .Net 代码性能 - (1)
  • /etc/skel 目录作用
  • @NestedConfigurationProperty 注解用法
  • @vue/cli脚手架
  • @zabbix数据库历史与趋势数据占用优化(mysql存储查询)
  • @拔赤:Web前端开发十日谈
  • [ C++ ] STL_stack(栈)queue(队列)使用及其重要接口模拟实现
  • [2019红帽杯]Snake
  • [30期] 我的学习方法
  • [Android]Tool-Systrace
  • [Android实例] 保持屏幕长亮的两种方法 [转]
  • [BZOJ2281][SDOI2011]黑白棋(K-Nim博弈)
  • [C++初阶]string类的详解
  • [CF543A]/[CF544C]Writing Code
  • [HEOI2013]ALO
  • [HOW TO]如何在iPhone应用程序中发送邮件
  • [IE编程] WebBrowser控件的多页面浏览(Tabbed Browsing)开发接口
  • [Interview]Java 面试宝典系列之 Java 多线程