当前位置:网站首页>IO流和序列化与反序列化
IO流和序列化与反序列化
2022-08-11 05:30:00 【zhangkai__】
java中I/O流关键点总结
1.File类
1.1 文件封装File对象
//将文件封装为一个File类的对象(3种引入写法)
File f = new File("d:\\test001.txt");
File f2 = new File("d:/test001.txt");
File f3 = new File("d:"+File.separator+"test001.txt");
File f4 =new File("D:\\a");
File f5 =new File("D:\\a\\b\\c");
//创建目录
f4.mkdir();//创建单层目录
f5.mkdirs();//创建多层目录
//删除 只能是空目录,有内容则不删除
f4.delete();
2.I/O流
2.1 IO流理解

IO类可大致分为如下多种:其中黄色为常用
2.2 文件读入
public static void main(String[] args) throws IOException {
//创建一个File类的对象
File f =new File("d:/test001.txt");
//利用FileReader类 将程序和源文件进行管道连接-->创建FileReader流对象
FileReader fr = new FileReader(f);
//进行程序读取(单个字符)
fr.read();
int n;
n=fr.read();
System.out.println(n);
//引入多个字符
char [] ch =new char[5];
int len = fr.read(ch);
while (len!=-1){
//len 用来一次装填定量的元素
for (int i =0;i<len;i++){
System.out.println(ch[i]);
}
//len重新放入后面的字符
len=fr.read(ch);
}
//连接管道不用就关闭
fr.close();
}
2.3 文件写出
public static void main(String[] args) throws IOException {
//引入一个文件
File f=new File("d:/test002.txt");
//FileWriter连接源文件和程序管道
FileWriter fw =new FileWriter(f);
//通过程序给源文件写入信息
String str="232哈哈";
for (int i=0;i<str.length();i++){
fw.write(str.charAt(i));
}
//追加信息
FileWriter w =new FileWriter(f);
String sr="zhongsg";
char [] da =sr.toCharArray();
fw.write(da);
//关闭流
fw.close();
}
注:文件不存在,那么自动创建文件
文件存在,FileWriter(f) 相当于源文件进行覆盖操作;
FileWriter(f,false) 相当于源文件进行覆盖操作,不是追加;
FileWriter(f,true) 对来原来的文件进行追加,而不是覆盖。
2.4 文件复制
public static void main(String[] args) throws IOException {
//一个源文件,一个目标
File f=new File("d:/test001.txt");
File f2=new File("d:/test003.txt");
//输入通道
FileReader fr=new FileReader(f);
//输出通道
FileWriter fw=new FileWriter(f2);
//进行复制
fw.write(fr.read());
char[] ch=new char[5];
int len =fr.read(ch);
while(len!=-1){
String s=new String(ch,0,len);
fw.write(s);
len=fr.read(ch);
}
//关闭通道
fw.close();
fr.close();
注:这里有可能出现复制中文出现异常,原因是在于创建文件时的类型默认ANSI类型,应该改成UTF-8类型。
打开文件另存为—>最下方有选择编码—>UTF-8
3.FileInputStream,FileOutputStream类
- 当文件是UTF-8进行存储时,其中英文字符占用1个字节;中文字符占用3个字节;
- 所以当文件是文本文件时,不建议使用字节流;因为会将字符拆开,失去意义;
- 字节流对象.read()方法读取字符返回int类型而不是byte类型,是因为底层做了处理,让返回数据都是“正数”,避免出现-1的情况,与结束条件(fr.read()!=-1)重合。
4.处理流
4.1BufferedIuputStream,BufferedOutputStream类 字节流
4.1.1 原理
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-irMZjrym-1655126465738)(C:\Users\Administrator.DESKTOP-G8VK4LM\Desktop\markdown\图片资源\处理流BufferedReader.jpg)]](/img/e1/f733d18cba9c123362307c4db8562d.jpg)
4.1.2 注意点
- 相当于建立缓冲区进行存储,在FileInputStream, FileOutputStream外,再套一层流(多重管道)。
- 关闭流操作时,只需要关闭高级流,则内部流也会随之关闭。高级流在外部。
- 若存储数据量大于初始设定缓冲区8192时,底层自动将全部内部推送至目标文件,缓冲区清空
4.2 BufferedReader,BufferedWriter类 字符流
4.2.1 读取操作
方式1:一个字符一个字符读
方式2:利用缓冲数组
方式3:读取字符串String
利用readLine()方法一行一行读
public static void main(String[] args) throws IOException {
//1.源文件
File f=new File("test001.txt");
//2.目标文件
File f2=new File("Tesst004.txt");
//3.流管道
FileReader fr=new FileReader(f);
FileWriter fw=new FileWriter(f2);
//4.处理缓存流套管道
BufferedReader br=new BufferedReader(fr);
BufferedWriter bw=new BufferedWriter(fw);
//5.读取写出
//5.1一个一个读
int n=br.read();
while (n!=-1){
bw.write(n);
n=br.read();
}
//5.2缓存数组
char[] ch=new char[30];
int len=br.read(ch);
while (len!=-1){
bw.write(ch,0,len);
len=br.read(ch);
}
//5.3 读取String:
String str=br.readLine();//每次读取一行数据
while(str!=null){
bw.write(str);
bw.newLine();//补充换行
str=br.readLine();
}
//6.关闭流
bw.close();
br.close();
5.转换流
5.1 定义
InputStreamReader ,OutputStreamWriter
将字节流和字符流进行转换,属于字符流。
5.2 转换过程
将字节转换为字符时,需要指定一个编码格式,如:UTF-8,GBK这个编码要和文件本身的编码格式相同。
文件(字节)---->(字节流)---->(转换流)---->(字符)程序
程序(字符)---->(转换流)---->(字节流)---->(字节)文件
6.System类对IO流的支持

7.序列化与反序列化
实在写不动了,直接上笔记吧。。。

- 接上,static,transient修饰的属性不可以被序列化,但是其所在类不受影响,只是这些修饰属性反序列化不出来。
最后
Idea中的序列化标识码serialVersionUID的设置见:
添加链接描述
文章中图片引用赵珊珊老师讲课视频,仅作学习使用,感谢<^_^>
边栏推荐
- 本地服务配置内网穿透实现微信公众号整合
- [Meetup]OpenMLDBxDolphinScheduler 链接特征工程与调度环节,打造端到端MLOps工作流
- Jetpack use exception problem collection
- 虚拟机更改IP地址
- 2021-09-11 C语言 变量与内存分配
- Event Preview | On April 23, a number of wonderful sharing sessions of OpenMLDB will come, which will live up to the good time of the weekend
- OpenMLDB Meetup No.2 会议纪要
- 无效的修订:3.18.1-g262b901-dirty
- 谨此留个纪念
- The Summer of Open Source 2022 is coming | Welcome to sign up for the OpenMLDB community project~
猜你喜欢

Vscode remote connection server terminal zsh+Oh-my-zsh + Powerlevel10 + Autosuggestions + Autojump + Syntax-highlighting

JS进阶网页特效(pink老师笔记)

Day 86

Open Source Machine Learning Database OpenMLDB Contributor Program Fully Launched

JNI入门

开源之夏 2022 火热来袭 | 欢迎报名 OpenMLDB 社区项目~

Visual studio2019 配置使用pthread

JVM tuning and finishing

星盟-pwn-babyheap

Tinker接入全流程---编译篇
随机推荐
2021-09-11 C language variables and memory allocation
Day 87
USB in NRZI to encode the data
OpenMLDB + Jupyter Notebook:快速搭建机器学习应用
Day 81
Intelligent risk control China design and fall to the ground
mysql basic summary
Goldbach's conjecture and the ring of integers
编译异常解决
Jetpack使用异常问题集锦
Vscode远程连接服务器终端zsh+Oh-my-zsh + Powerlevel10 + Autosuggestions + Autojump + Syntax-highlighting
Wonderful linkage | OpenMLDB Pulsar Connector principle and practical operation
【无标题】
SearchGuard configuration
JS this关键字
The whole process of Tinker access --- configuration
mysql基础总结
PAT乙级刷题之路
OpenMLDB Pulsar Connector: Efficiently connect real-time data to feature engineering
Day 86