java基础I/O

news/2024/10/20 17:30:32/

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.ppmy.cn/news/1506115.html

相关文章

AI10-PaddleDetection2.7安装测试

1、下载PaddleDetect-release-2.7开源项目 https://github.com/PaddlePaddle/PaddleDetection https://github.com/PaddlePaddle/PaddleDetection/releases PaddleDetection: PaddleDetection的目的是为工业界和学术界提供丰富、易用的目标检测模型 2、安装 环境要求 * Paddl…

学习笔记 韩顺平 零基础30天学会Java(2024.8.7)

P481 Math方法 利用random返回一个[2,7]之间的随机数: 因为random只能返回[0,1)之间的随机数,因此做一下处理:[(int)(a), (int) (aMath.random()*(b-a1))],对于Math.random()*(b-a1),其中b-a1,它乘上[0,1)相…

vue实现PC端图片放大缩小可鼠标拖动,鼠标滚轮控制放大缩小完整代码付效果图

vue实现图片放大缩小可鼠标拖动&#xff0c;鼠标滚轮控制放大缩小完整代码付效果图 效果图&#xff1a; 创建一个ImageViewer 组件&#xff0c;并且在当前页面引用完整代码如下&#xff1a; 代码引用&#xff1a; <template><view><image-viewer :imageUrl&q…

Flink 开发语言选择 —— Java vs Scala

引言 Apache Flink 是一个用于处理无界和有界数据流的开源分布式计算框架。随着 Flink 的日益流行&#xff0c;越来越多的开发者开始考虑使用哪种编程语言来进行 Flink 应用程序的开发。本文将探讨在 Flink 中使用 Java 和 Scala 的优缺点&#xff0c;并帮助你做出更明智的选择…

Linux Vim教程

Linux Vim 教程 Vim&#xff08;Vi IMproved&#xff09;是一个强大的文本编辑器&#xff0c;广泛用于编程和系统管理。本文将带你全面了解 Vim 的基础使用、常用命令、高级功能等。 1. 安装 Vim 在大多数 Linux 发行版中&#xff0c;Vim 已经预装。如果没有&#xff0c;可以…

Linux系统使用Docker安装RStudio服务并实现任意浏览器远程访问

文章目录 前言1. 安装RStudio Server2. 本地访问3. Linux 安装cpolar4. 配置RStudio server公网访问地址5. 公网远程访问RStudio6. 固定RStudio公网地址 前言 RStudio Server 使你能够在 Linux 服务器上运行你所熟悉和喜爱的 RStudio IDE&#xff0c;并通过 Web 浏览器进行访问…

Javaweb项目|ssm基于java的健身房管理系统的设计与实现vue

收藏点赞不迷路 关注作者有好处 文末获取源码 一、系统展示 二、万字文档展示 基于ssm基于java的健身房管理系统的设计与实现vue 开发语言&#xff1a;Java 数据库&#xff1a;MySQL 技术&#xff1a;SpringSpringMVCMyBatisVue 工具&#xff1a;IDEA/Ecilpse、Navicat、Mav…

谷粒商城实战笔记-125-全文检索-ElasticSearch-整合-SpringBoot整合high-level-client

文章目录 一&#xff0c;技术选型1. 通过 TCP 连接&#xff08;9300 端口&#xff09;2. 通过 HTTP 连接&#xff08;9200 端口&#xff09;3.最终选择 二&#xff0c;SpringBoot整合Elasticsearch-Rest-High-Level-Client1&#xff0c;新增模块gulimall-search1&#xff0c;添…