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

java基础I/O

1,I/O流的概念:
IO流代表的是一个数据输入的源或者输出的目标地址,可以是硬盘,内存,网络或者什么其他的电子设备,而IO流的类型也很多比如最简单的字节或者字符,或者其他更高级的对象。
不管它有多少特性,本质上 流就是一组字节序列,或者直接可以说01字节序列吧?

2,字节流
首先是一个官网例子:CopyBytes.java。


import java.io.*;public class CopyBytes {public static void main(String[] args) throws IOException {read(); //420856988read2(); //1100868readBuffer(); //5977046readBuffer2(); //964845}public static void read() throws IOException {final long l = System.nanoTime();FileInputStream in = null;FileOutputStream out = null;try {String properties = System.getProperty("java.class.path");in = new FileInputStream(properties+"/xanadu.txt"); //想要在IDEA里面跑起来 就把文件放在resources里面//in = new FileInputStream("xanadu.txt"); //否则把文件放在classpath目录里面,使用java命令启动程序out = new FileOutputStream(properties+"/outagain.txt");//out = new FileOutputStream("outagain.txt");int c;while ((c = in.read()) != -1) { //这里每次读取一个字节out.write(c);}} finally {if (in != null) {in.close();//必须得关闭}if (out != null) {out.close();}}System.out.println(System.nanoTime() - l);}public static void read2() throws IOException {final long l = System.nanoTime();FileInputStream in = null;FileOutputStream out = null;try {String properties = System.getProperty("java.class.path");in = new FileInputStream(properties+"/xanadu.txt"); //想要在IDEA里面跑起来 就把文件放在resources里面//in = new FileInputStream("xanadu.txt"); //否则把文件放在classpath目录里面,使用java命令启动程序out = new FileOutputStream(properties+"/outagain.txt");//out = new FileOutputStream("outagain.txt");int c;byte[] bufferBytes = new byte[8192];while ((c = in.read(bufferBytes)) != -1) { //这里每次读取一个字节out.write(bufferBytes,0,c);}} finally {if (in != null) {in.close();//必须得关闭}if (out != null) {out.close();}}System.out.println(System.nanoTime() - l);}public static void readBuffer() throws IOException {final long l = System.nanoTime();InputStream in = null;OutputStream out = null;try {String properties = System.getProperty("java.class.path");in = new BufferedInputStream(new FileInputStream(properties+"/xanadu.txt")); //想要在IDEA里面跑起来 就把文件放在resources里面out = new BufferedOutputStream(new FileOutputStream(properties+"/outagain.txt"));int c;while ((c = in.read()) != -1) { //这里每次读取一个字节out.write(c);}} finally {if (in != null) {in.close();//必须得关闭}if (out != null) {out.close();}}System.out.println(System.nanoTime() - l);}public static void readBuffer2() throws IOException {final long l = System.nanoTime();InputStream in = null;OutputStream out = null;try {String properties = System.getProperty("java.class.path");in = new BufferedInputStream(new FileInputStream(properties+"/xanadu.txt")); //把数据批量读进内存out = new BufferedOutputStream(new FileOutputStream(properties+"/outagain.txt"));int c;byte[] bufferBytes = new byte[8192];while ((c = in.read(bufferBytes)) != -1) { //这个批量读进bufferBytes数组。out.write(bufferBytes,0,c);}out.flush(); //这里可以手动刷新一下,防止数据丢失。} finally {if (in != null) {in.close();//必须得关闭}if (out != null) {out.close();}}System.out.println(System.nanoTime() - l);}
}


2.1 字节流默认每次输入或者输出一个字节(8-bit),类很多,但是所有的都是InputStream/OutputStream的子类,CopyBytes.java使用了FileInputStream和FileOutputStream,但是其他的类的使用方法差不多,除了构造方法不一样。
2.2 整个复制的过程比较简单,就是在输入流里面读取一个字节,然后往输出流里面写一个字节,直到读完。
2.3 输入输出流使用完毕后必须关闭,而且是在finally代码块里面确保一定关闭,防止资源泄漏。
2.4 这个CopyBytes看起来挺好用,但这只是一个低级别的I/O操作,平时应该避免使用,它只是基础,如果复制可以使用Character Stream。

3,字符流
首先是官网的两个例子:CopyCharacters.java和CopyLine.java


import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException;public class CopyLines {public static void main(String[] args) throws IOException {BufferedReader inputStream = null;PrintWriter outputStream = null;try {String properties = System.getProperty("java.class.path");System.out.println(properties);inputStream = new BufferedReader(new FileReader(properties+"/xanadu.txt"));outputStream = new PrintWriter(new FileWriter(properties+"/characteroutput1.txt"));String l;while ((l = inputStream.readLine()) != null) {outputStream.println(l);}} finally {if (inputStream != null) {inputStream.close();}if (outputStream != null) {outputStream.close();}}}
}


import java.io.*;public class CopyCharacters {public static void main(String[] args) throws IOException {readByReader();       //27623168readByBufferReader(); //2098684 //2Kb的文件就相差10倍}public static void readByReader() throws IOException{final long l = System.nanoTime();Reader inputStream = null;Writer outputStream = null;try {String properties = System.getProperty("java.class.path");inputStream = new FileReader(properties+"/xanadu.txt");outputStream = new FileWriter(properties+"/characteroutput.txt");int c;while ((c = inputStream.read()) != -1) {outputStream.write(c);}} finally {if (inputStream != null) {inputStream.close();}if (outputStream != null) {outputStream.close();}}System.out.println(System.nanoTime() - l);}public static void readByBufferReader() throws IOException{final long l = System.nanoTime();Reader inputStream = null;Writer outputStream = null;try {String properties = System.getProperty("java.class.path");inputStream = new BufferedReader(new FileReader(properties+"/xanadu.txt"),8192); //这个size大小对性能的影响很大outputStream = new BufferedWriter(new FileWriter(properties+"/characteroutput.txt"),8192);int c;while ((c = inputStream.read()) != -1) {outputStream.write(c);}} finally {if (inputStream != null) {inputStream.close();}if (outputStream != null) {outputStream.close();}}System.out.println(System.nanoTime() - l);}}


3.1 Java默认使用Unicode存储字符值,而字符流会自动处理本地字符集与内部之间的转换。使用字符流替代字节流的程序会自动回应本地字符集而无需程序员操心。
3.2 所有的字符流都继承自Reader和Writer第一个例子CopyCharacters.java使用了FileRead和FileWriter
3.3 字符流通常是字节流的包装器,字节流执行物理I/O,而字符流处理字节与字符之间的转换,有两个通用的字节到字符“桥接”流: InputStreamReader 和 OutputStreamWriter,自定义是可以用这两个类。
3.4 字符流通常一次会读取多个字符,比如一行字符,CopyLine.java 就是按行读取写入的例子,一串带有行终止符的字符称为一行。行终止符有 回车符/换行符序列("/r/n") 单个回车符("/r") 单个换行符("/n")。
3.5 调用 readLine 将返回一行带有该行的文本,CopyLines 输出每一行 (println) ,这将附加当前操作系统的行终止符。这可能与输入文件中使用的行终止符不同

4,缓冲流
前面介绍的字节流和字符流没有缓冲,每次读取都是由底层操作系统直接操作,效率较低,因为每次操作都可能触发磁盘IO或者网络活动,为了避免这种情况设计了缓冲流。
4.1 缓冲输入流从缓冲区(内存)读取数据,当缓冲区为空是才调用底层操作系统,同样的输出流也是先输出到缓冲区,要满时才才调用本地的API。
4.2 有四个缓冲流类用于包装未缓冲的流: BufferedInputStream 和 BufferedOutputStream 创建缓冲字节流,而和 BufferedReader BufferedWriter 创建缓冲字符流。
4.3 用法:inputStream = new BufferedReader(new FileReader("xanadu.txt"));
outputStream = new BufferedWriter(new FileWriter("characteroutput.txt"));
4.4 靠,这个性能差别还是很大的,尤其是对于字节流来说可以提升近百倍。
4.5 每次创建时会创建一个固定大小的缓冲区数组(字符流是char[8192],字节流是byte[8192]),根据文档描述 默认的大小就可以满足绝大多数情况,当然我想应该是文件越大越要提高数值吧,资源允许的话。8192->8M大小
4.6 缓冲区是内存里面存储该文件的地方,在copy时还可以创建缓冲数组提高效率,最终版应该是 CopyBytes.readBuffer2()方法。
这个地方打比方的话就是 如果使用单纯的字节流,不使用缓冲流,就相当于 我们在桌面查看一个D盘的文件,每次都得打开D盘然后读取一个字节,然后返回桌面复制到另一文件李敏啊,然后在回到D盘再读取一个字节...这样子。
用了缓冲流呢,每次读取8192个字节,回来复制一下,write(int i)复制时呢就是一个字节一个字节复制的,write(byte[8192],0,len)就是每次复制8192个字节,当然最快。
4.7 缓冲输出流提供方法 flush() 手动刷新数据到目标文件,每次结束之后可以手动调用,方式数据丢失,在关闭流时也会自动调用刷新方法。总结要么关闭也么刷新最好两个都做了。

 

http://www.lryc.cn/news/417431.html

相关文章:

  • 关于“八股文”在程序员面试中的角色及其对工作实际效用的讨论
  • 【算法设计题】基于front、rear和count的循环队列初始化、入队和出队操作,第6题(C/C++)
  • 端点区间影响
  • Leetcode3224. 使差值相等的最少数组改动次数
  • thinkphp之命令执行漏洞复现
  • 算法板子:匈牙利算法——二分图的最大匹配
  • 轻松拯救数据危机!四大必备的数据恢复软件免费版推荐!
  • windbg常用命令
  • Ubuntu(20.04 LTS)更换镜像源
  • golang使用 copier对象复制时进行类型转化
  • 英特尔18A制程技术分析解读
  • 【百度面试算法题】2024-08-02
  • OSPF基础
  • leetcode 958.二叉树的完全性检验
  • Spring 中请求作用域的数据存储在 ThreadLocal 中还是 Spring 容器中?
  • 基础岛 - 8G显存验证书生·浦语大模型的Demo
  • Jangow靶机攻略
  • Vue项目通过宝塔部署之后,页面刷新后浏览器404页面
  • Java一一一简易图书管理系统
  • Ubuntu配置carla docker环境
  • 超越sd3!比肩Midjourney-v6?AI绘画大模型FLUX1.0详细评测与本地部署方法(附安装文件)
  • 帆软填报报表单元格根据其它单元格内容决定另外的单元格可筛选什么值
  • 一键浪漫的回忆:微软开源的修复工具!!【送源码】
  • 力扣-240.搜索二维矩阵(2)
  • Python推导式和生成器表达式
  • 比较支持向量机、AdaBoost、逻辑斯谛回归模型的学习策略与算法
  • Android顶部标题栏自定义,添加按钮
  • Spring Boot 整合 Dubbo3 + Nacos 2.4.0【进阶】+ 踩坑记录
  • 浙江省食品安全管理员题库及答案
  • C++ 几何算法 - 求两条直线交点