当前位置:网站首页>2022-08-09 Group 4 Self-cultivation class study notes (every day)
2022-08-09 Group 4 Self-cultivation class study notes (every day)
2022-08-11 07:29:00 【bodybuilding class】
IO流(输入输出流)
1、按照流向分
输入流:从硬盘上读取数据到内存.(读)
输出流:Write data from memory to hard disk.(写)
A file goes through multiple copies during the transfer,IOPerformance itself is low.
2、按照操作单元分:
字节流:is a byte-by-byte operation.二进制操作.Manipulate files of any type.
字符流:is a character-by-character operation.一个字符两个字节,Mainly used to process text files.
.txt,.java,.py,.xml,.properties,.html,.css,.js,
3、按照角色划分:
节点流:Manipulate a specific one directlyIO设备.
处理流:在节点流的基础上,做进一步的处理.
Java中输入/输出流常用的流:
字节输入流 字节输出流 字符输入流 字符输出流
抽象基类 InputStream OutputStream Reader Writer
访问文件 FileInputStream FileOutputStream FileReader FileWriter
(节点流)
缓冲流 BufferedInputStream BufferedOutputStream BufferedReader BufferedWriter
(处理流)
操作对象 ObjectInputStream ObjectOutputStream
public class Ch01 {
public static void main(String[] args) {
File file = new File("e:/aaa.txt");
try {
file.createNewFile();
System.out.println("文件创建完成!");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
how to use flow?
输入流:It is to read data from memory bit by bit!
字节输入流:
1.创建一个FileInputStream对象
2.定义一个标记,用来控制输入流的读取
3.循环读取,如果读取到了-1,说明读取到了文件的末尾,循环结束
4.关闭资源.*****
注意:我们发现一个流读完了就没有了,不能再读了.
当一个流读完之后会默认调用mark和reset方法来进行记录和重置,
这个流就已经重置到了上次读完的位置,
所以就无法再次读取内容.并不是读完一次之后就关闭了流.
public class Ch02 {
@Test
public void test02() {
InputStream inputStream = null;
try {
inputStream = new FileInputStream("e:/aaa.txt");
int read;
byte [] buf = new byte[1024];
while((read = inputStream.read(buf)) != -1) {
// System.out.print(Arrays.toString(buf) + " ");
System.out.println(new String(buf));
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
IOUtil.closeIO(inputStream,null);
// try {
// if(Objects.nonNull(inputStream)){
// // 关闭流
// inputStream.close();
// }
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
}
}
@Test
public void test01() {
InputStream inputStream = null;
try {
inputStream = new FileInputStream("e:/aaa.txt");
// 开始读的操作,read方法,返回值是int,当返回值为-1时,说明文件读取到了末尾
// 读取文件是否结束的标记
int read;
// 字节流读数据的时候一个字节一个字节去读
// 循环读取
while((read = inputStream.read()) != -1) {
System.out.print(read + " ");
}
System.out.println();
System.out.println("读取完毕,再读一次....");
// 字节流读数据的时候一个字节一个字节去读
// 循环读取
while((read = inputStream.read()) != -1) {
System.out.print(read + " ");
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
// 关闭流
inputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
字节输出流
FileOutputStream构造器:
boolean append参数:如果传入true,则代表在原有基础上追加,不覆盖
如果传入false,或者不传,覆盖原有内容
写的操作,目标文件如果不存在,会自动新建.
文件复制
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/*
使用FileInputStream+FileOutputStream完成文件的拷贝
拷贝的过程为一边读一边写
使用以上字节流拷贝文件的时后,文件类型随意,万能的
*/
public class Demo05 {
public static void main(String[] args) {
FileInputStream fis=null;//创建文件字节输入流对象
FileOutputStream fos=null;//创建文件字节输出流对象
try {
fis=new FileInputStream("C:\\Users\\ASUS\\temp");
fos=new FileOutputStream("C:\\dd.txt");
//最核心的:一边读一边写
byte[] bytes=new byte[1024*1024];//1MB
int a=0;
while ((a=fis.read(bytes))!=-1){
fos.write(bytes,0,a);
}
fos.flush();//刷新,输出流最后要刷新
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {//分开关闭
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
在jdk1.7之后,Many resource classes are implementedAutoCloseable接口
Including our common streams,FileInputStream,FileOutputStream....
可以在try中定义资源,并会主动释放资源
了解即可!!
字符流:纯文本文件
缓冲流:
处理流,
Reader reader = new FileReader("e:/aaa.txt")
bufferedReader = new BufferedReader(reader);
外层流,内层流,关闭了外层的流,内层的流会随之关闭.
public class Ch01 {
public static void main(String[] args) {
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try {
bufferedReader = new BufferedReader(new FileReader("e:/aaa.txt"));
bufferedWriter = new BufferedWriter(new FileWriter("e:/bbb.txt"));
String str;
while((str = bufferedReader.readLine()) != null){
bufferedWriter.write(str);
bufferedWriter.newLine();
}
System.out.println("文件复制成功!!!");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtil.closeIO(bufferedReader,bufferedWriter);
}
}
}
序列化与反序列化:操作对象
序列化:将对象写入到IO流中,将内存模型的对象变成字节数字,
可以进行存储和传输.
反序列化:从IO流中恢复对象,将存储在硬盘上或者从网络中接收的数据
恢复成对象模型
使用场景:所有可在网络上传输的对象都必须是可序列化的,
否则会报错,所有保存在硬盘上的对象也必须要可序列化.
序列化版本号:
反序列化必须拥有class文件,但随着项目的升级,class文件也会升级
序列化保证升级前后的兼容性.
java序列化提供了一个版本号
版本号是可以自由指定,如果不指定,JVM会根据类信息自己计算一个版本号,
所以无法匹配,则报错!!!
不指定版本号,还有一个隐患,不利于JVM的移植,可能class文件没有改,
但是不同的jvm计算规则不一样,导致无法反序列化
如果只修改了方法,反序列化是不受影响,无需修改版本号
修改了静态变量static,瞬态变量transient,反序列化也不受影响,无需修改版本号
总结:
1.所有需要网络传输的对象都需要实现序列化接口
2.对象的类名、实例变量都会被序列化;方法、类变量、transient变量不会被序列化
3.如果想让某个变量不被序列化,可以用transient修饰
4.序列化对象的引用类型成员变量,也必须是可序列化的,否则会报错
5.反序列化时必须有序列化对象的class文件
6.同一个对象被序列化多次,只有第一次序列化为二进制流,以后都只是保存序列化的版本号
7.建议所有可序列化的类加上版本号,方便项目升级.
我们最终会把所有的.class文件打包,把这个包部署到服务器上
从始至终,.java仅仅是我们程序员写的,给程序员看的.
.java甚至不会参与到打包中,不会出现在服务器上.
运维人员去服务器部署项目,部署的就是一堆的.class.
我们的.properties属性文件是不参与编译的.
public class Ch03 {
public static void main(String[] args) throws IOException {
File file = new File("db.properties");
// file.createNewFile();
Properties properties = new Properties();
properties.load(new FileInputStream(file));
System.out.println(properties.getProperty("username"));
}
}
边栏推荐
猜你喜欢
导航定位中的坐标系
unable to extend table xxx by 1024 in tablespace xxxx
Douyin API interface
淘宝sku API 接口(PHP示例)
unable to extend table xxx by 1024 in tablespace xxxx
ROS 服务通信理论模型
快速了解集成学习
Daily sql-statistics of the number of professionals (including the number of professionals is 0)
radix-4 FFT principle and C language code implementation
图的拉普拉斯矩阵
随机推荐
unable to extend table xxx by 1024 in tablespace xxxx
Discourse's Close Topic and Reopen Topic
sql--7天内(含当天)购买次数超过3次(含),且近7天的购买金额超过1000的用户
博途PLC 1200/1500PLC ModbusTcp通信梯形图优化汇总(多服务器多从站轮询)
图文带你理解什么是Few-shot Learning
Unity3D 学习路线?
如何选择专业、安全、高性能的远程控制软件
Pinduoduo API interface
Daily sql: request for friend application pass rate
redis + lua实现分布式接口限流实现方案
姿态解算-陀螺仪+欧拉法
Tidb二进制集群搭建
【深度学习】什么是互信息最大化?
OA project meeting notice (query & whether attending & feedback for details)
抖音API接口
亚马逊获得AMAZON商品详情 API 返回值说明
每日sql-求2016年成功的投资总和
unable to extend table xxx by 1024 in tablespace xxxx
矩阵分析——矩阵分解
淘宝商品详情API接口