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

Java IO流(一)IO基础

概述

IO流本质
  • I/O表示Input/Output,即数据传输过程中的输入/输出,并且输入和输出都是相对于内存来讲
  • Java IO(输入/输出)流是Java用于处理数据读取和写入的关键组件
  • 常见的I|O介质包括
    • 文件(输入|输出)
    • 网络(输入|输出)
    • 键盘(输出)
    • 显示器(输出)
  • 使用场景
    • 文件拷贝(File)
    • 文件上传下载
    • Excel导入导出
    • 网络程序中数据传输(聊天工具)

分类

概述

Java中几乎所有的IO操作都需要使用java.io包;流可以通过如下方式进行分类

  • 按流向分(输入输出过程通常都是站在程序角度考虑)
    • 输入流(Input)
    • 输出流(Output)
  • 按流的处理类型分
    • 字节流(byte): 字节是计算机存储容量的基本单位(Byte),1B=8b,二进制中占8位
    • 字符流(char): 字符是文字或符号的统称

        注意:字节流对于什么类型的文件都可以读取,如二进制类型的文件(图片,视频,音频,压缩                    文件等),而字符流用于读取文本类型文件

  • 按流的功能来分
    • 节点流(直接跟输入输出源交互)
    • 处理流(对其他流包装的流:包装流)

字节流(InputStream && OutputStream)

InputStream 类图
OutputStream类图

日常开发过程中常用的字节流:

FileInputStream && FileOutputStream: 常用来实现文件复制/拷贝

BufferedInputStream && BufferedOutputStream: 为了减少IO次数,提高读取效率

PrintStream:源自OutputStream,标准字节的打印输出流(日志框架的实现原理)

ZipOutputStream && ZipInputStream:用来进行文件压缩/文件解压

字符流(Reader && Writer)

Reader类图
Writer类图

日常开发过程中常用的字符流:

FileReader&&FileWriter:作用同FileInputStream && FileOutputStream

BufferedReader&&BufferedWriter:作用同BufferedInputStream && BufferedOutputStream,同时BufferedReader提供了按行读取文本的方法,方便文本处理

扩展: 我们知道字节流可以读取任意文件,为什么还要设计出字符流呢?

  • 对于字符文件,先作为字节传输->再转成字符,比较耗时
  • 对于字符文件,如果其中为中文,则容易乱码

设计模式

在IO流中使用了多种设计模式,包括如下:

适配器模式

适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作

Java IO中为了实现字符流和字节流之间的相互转换,设计了两个适配器的类,

InputStreamReader和OutputStreamWriter

InputStreamReader isr = new InputStreamReader(new FileInputStream(fileName), "UTF-8");
BufferedReader bufferedReader = new BufferedReader(isr);

装饰器模式

装饰器模式可以将新功能动态地附加于现有对象而不改变现有对象的功能。InputStream的子类FilterInputStream,OutputStream 的子类 FilterOutputStream,Reader 的子类 BufferedReader 以及 FilterReader,还有Writer的子类BufferedWriter、FilterWriter 以及 PrintWriter等,它们都是抽象装饰类。增强了子类对象的功能。

实践

ZipOutputStream&&FileOutputStream&&FileInputStream实现文件压缩

/*** 功能: 通过ZipOutputStream压缩文件,最后返回压缩包* @param files* @param fileName* @return*/
public File zipFiles(File[] files,String fileName) {File zipFile = null;FileOutputStream fosZipFile = null;ZipOutputStream zosZipFile = null; //压缩文件输出流try {zipFile = downloadAttachmentService.createFile("", fileName); //创建一个空的文件目录fosZipFile = new FileOutputStream(zipFile); //以文件流从内存中输出zosZipFile = new ZipOutputStream(fosZipFile); //以压缩流从内存中输出for (File file : files) {FileInputStream fis = new FileInputStream(file); //对每个文件创建输入流,读取文件到内存ZipEntry zipEntry = new ZipEntry(file.getName()); //ZipEntry用来创建压缩文件zosZipFile.putNextEntry(zipEntry); //加入需要压缩的文件byte[] bytes = new byte[1024];int length;while((length = fis.read(bytes)) >= 0) { //读取文件到内存zosZipFile.write(bytes, 0, length); //文件写入压缩流}fis.close();}} catch (IOException e) {e.printStackTrace();} finally { //关闭流try {zosZipFile.close();fosZipFile.close();} catch (IOException e) {e.printStackTrace();}}return zipFile; //返回压缩包
}
/*** @Title: createFile* @Description: 创建下载目录文件* @author Bierce* @param rootPath* @param filename* @return* @throws IOException*/
public File createFile(String rootPath, String filename) throws IOException {// Default root pathif (rootPath.isEmpty()) {rootPath = "download-cache";}File fRoot = new File(rootPath);if (!fRoot.exists() || !fRoot.isDirectory()) {fRoot.mkdirs();}// job sub pathString uuid = UUID.randomUUID().toString();String directoryJob = rootPath + File.separator + getClass().getSimpleName() + File.separator + uuid;//文件名称随机生成保证唯一File dirJob = new File(directoryJob);if (!dirJob.exists() || !dirJob.isDirectory()) {dirJob.mkdirs();}String filePath = directoryJob + File.separator + filename;File file = new File(filePath);if (!file.exists()) {file.createNewFile();}return file;
}
//-----------------扩展方法-文件名去重保证唯一-----------------
/*** @Title: snFileName_noUIID* @Description: 去除sn文件UUID以及解决sn文件名重复问题* @author Bierce* @return file*/
public File snFileName_noUIID(String fileParentPath,String snFileName,File file){//snFileName:完整文件名 sn-xx..UUID..xx.xlsx//snFileName_delUIID: sn.xlsx//snFileName_prefix: sn//suffix:xlsx//文件名:如sn.xlsxString snFileName_delUIID = snFileName.substring(0,snFileName.length() - 42) + ".xlsx";//42是固定长度:UUID+.xlsxString snFileName_prefix = snFileName.substring(0,snFileName.length() - 42);//文件前缀String suffix = snFileName.substring(snFileName.lastIndexOf("."));//文件后缀:.xlsxtry {file = new File(fileParentPath + snFileName_delUIID);//设置sn文件所在目录为计划交接文件目录下int i = 1;//对于同名SN文件情况重新命名while(file.exists()) {//保证文件夹下不存在同名文件String newFileName = snFileName_prefix + "(" + i + ")" + suffix;String parentPath = file.getParent();file = new File(parentPath + File.separator + newFileName);i++;}file.createNewFile();//new File 只是创建了一个File对象,还需要调用createNewFile()方法才能实现文件的成功创建} catch (Exception e) {}return file;
}
http://www.lryc.cn/news/129387.html

相关文章:

  • 区间覆盖 线段覆盖 二分
  • F#奇妙游(20):主动模式
  • OLED透明屏与传统显示屏的区别:探索未来视觉体验的新里程碑
  • 打开软件提示mfc100u.dll缺失是什么意思?要怎么处理?
  • Python 基础 -- Tutorial(二)
  • 11 迭代器|生成器|协程
  • “第三方支付”详解!
  • Rust之泛型、trait与生命周期
  • GPU Microarch 学习笔记 [1]
  • Transformer(一)简述(注意力机制,NLP,CV通用模型)
  • 回归预测 | MATLAB实现BiLSTM双向长短期记忆神经网络多输入多输出预测
  • 使用Dockker创建vwas容器时报错的解决方法
  • 【数据结构OJ题】链表分割
  • 无感知发布
  • C++ 虚继承
  • git commit用法
  • 【LeetCode】543.二叉树的直径
  • TypeScript教程(五)条件语句,循环,函数
  • vue使用jsplumb 流程图
  • 【BASH】回顾与知识点梳理(二十八)
  • LangChain源码逐行解密之系统(二)
  • QT的设计器介绍
  • [LitCTF 2023]Ping
  • Spring Cloud面试突击班1
  • 线上售楼vr全景看房成为企业数字化营销工具
  • “深入探索JVM内部机制:解密Java虚拟机原理“
  • 最长 上升子序列
  • Nginx的介绍
  • [杂项]奥特曼系列影视列表大全
  • java代码日记--java 基础语法