当前位置:网站首页>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 原理
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的设置见:
添加链接描述
文章中图片引用赵珊珊老师讲课视频,仅作学习使用,感谢<^_^>
边栏推荐
- stack stack
- 【Meetup预告】OpenMLDB+OneFlow:链接特征工程到模型训练,加速机器学习模型开发
- Day 87
- Day 73
- 无效的修订:3.18.1-g262b901-dirty
- Day 70
- Real-time Feature Computing Platform Architecture Methodology and Practice Based on OpenMLDB
- JS case exercise (classic case of teacher pink)
- Simple mine sweeping in C language (with source code)
- 杀死进程-查看防火墙状态
猜你喜欢
随机推荐
js 学习进阶(事件高级 pink老师教学笔记)
Invalid revision: 3.18.1-g262b901-dirty
C语言实现猜数字(附带源码,可直接运行)
Day 78
stack stack
8-byte standard request parsing during USB enumeration
Byte (byte) and bit (bit)
Dark Horse Event Project
黑马大事件项目
PyQt5中调用.ui转换的.py文件代码解释
场景驱动的特征计算方式OpenMLDB,高效实现“现算先用”
Wonderful linkage | OpenMLDB Pulsar Connector principle and practical operation
The Summer of Open Source 2022 is coming | Welcome to sign up for the OpenMLDB community project~
Day 67
127.0.0.1 已拒绝连接
Pinyougou project combat notes
Getting Started with JNI
微信小程序_开发工具的安装
js 学习进阶(Dom部分 pink老师教学笔记)
自己动手写RISC-V的C编译器-02语法描述方法和递归下降解析