1. 缓冲流
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XrTPpD56-1628948845358)(C:\Users\14324\AppData\Roaming\Typora\typora-user-images\image-20210814184050529.png)]
1.1 缓冲流概述
缓冲流自带缓冲区,可以提高原始字节流、字符流读写数据的性能
1.2 字节缓冲流
BufferedInputStream
BufferedOutputStream
| 构造器 | 说明 |
|---|---|
| public BufferedInputStream(InputStream is) | 可以把低级的字节输入流包装成一个高级的缓冲字节输入流管道 |
| public BufferedOutputStream(OutputStream os) | 可以把低级的字节输出流包装成一个高级的缓冲字节输出流管道 |
1.2.1 字节缓冲流的性能分析
字节缓冲输入流和输出流自带了8kb的缓冲池
建议使用字节缓冲输入流、字节缓冲输出流,结合字节数组的方式
1.2.2 用缓冲流字节数组复制
public static void main(String[] args) throws IOException {
//创建
File file = new File("Test08_14\\src\\data2.txt");
//创建低级的字节输入流
FileInputStream fis = new FileInputStream(file);
//创建字节输入缓冲流
BufferedInputStream bis = new BufferedInputStream(fis);
//创建
File file1 =new File("Test08_14\\src\\data1.txt");
//创建低级的字节输出流
FileOutputStream fos = new FileOutputStream(file1);
//创建字节输出缓冲流
BufferedOutputStream bos = new BufferedOutputStream(fos);
//定义数组
byte[] buffer = new byte[1024];
int len;
//循环输出
while ((len = bis.read(buffer))!=-1 ) {
bos.write(buffer,0,len);
}
//关闭流
bis.close();
bos.close();
}
1.3 字符缓冲流
字符缓冲流自带8K缓冲区
1.3.1 字符缓冲流的构造器
BufferedReader
| 构造器 | 说明 |
|---|---|
| public BufferedReader(Reader r) | 可以把低级的字符输入流包装成一个高级的缓冲字符输入流管道 |
BufferedWriter
| 构造器 | 说明 |
|---|---|
| public BufferedWriter(Writer w) | 可以把低级的字符输出流包装成一个高级的缓冲字符输出流管道 |
1.3.2 字符缓冲流的特有方法
**输入流: **
| 方法 | 说明 |
|---|---|
| public String readLine() | 读取一行数据返回,如果读取没有完毕,无行可读返回null |
**输出流: **
| 方法 | 说明 |
|---|---|
| public void newLine() | 换行操作 |
1.3.3 用缓冲流字符复制
public static void main(String[] args) throws IOException {
//创建
File file = new File("Test08_14\\src\\data2.txt");
//创建低级的字符输入流
FileReader fr = new FileReader(file);
//创建字符输入缓存流
BufferedReader br = new BufferedReader(fr);
//创建
File file1 =new File("Test08_14\\src\\data1.txt");
//创建低级的字符输出流
FileWriter fw = new FileWriter(file1);
//创建字符输出缓存流
BufferedWriter bw = new BufferedWriter(fw);
String s;
while ((s = br.readLine()) != null) {
//写入数据
bw.write(s);
//换行
bw.newLine();
}
//释放流
bw.close();
br.close();
}
2. 转换流__字符流
先通过字节流得到字节数据,通过转换流转换为指定的字符流
字符流再通过指定的编码模式转为字节流进行写入
2.1 字符输入转换流
**字符输入转换流:**InputStreamReader,可以把原始的字节流按照指定编码转换成字符输入流。
| 构造器 | 说明 |
|---|---|
| public InputStreamReader(InputStream is) | 可以把原始的字节流按照代码默认编码转换成字符输入流 |
| public InputStreamReader(InputStream is ,String charset) | 可以把原始的字节流按照指定编码转换成字符输入流 |
2.2 字符输出转换流
**字符输入转换流:**OutputStreamWriter,可以把字节输出流按照指定编码转换成字符输出流。
| 构造器 | 说明 |
|---|---|
| public OutputStreamWriter(OutputStream os) | 可以把原始的字节输出流按照代码默认编码转换成字符输出流。 |
| public OutputStreamWriter(OutputStream os,String charset) | 可以把原始的字节输出流按照指定编码转换成字符输出流(重点) |
2.3 用字符转换流进行复制
public static void main(String[] args) throws IOException {
//创建
File file = new File("Test08_14\\src\\data2.txt");
//创建低级字节输入流,获取文件的字节流
FileInputStream fis = new FileInputStream(file);
//创建字符转换流,可以将字节以指定的编码模式存储为字节流
InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
//创建字节输入缓冲流
BufferedReader br = new BufferedReader(isr);
//创建
File file1 =new File("Test08_14\\src\\data1.txt");
//创建低级字节输出流
FileOutputStream fos = new FileOutputStream(file1);
//创建字符转换流,可以将字节流以指定的编码模式转为字符流
OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
//创建字符输出缓存流
BufferedWriter bw = new BufferedWriter(osw);
String s;
while ((s = br.readLine()) != null) {
bw.write(s);
bw.newLine();
}
bw.close();
br.close();
}
3. 序列化
对象序列化必须实现序列化接口 Serializable
3.1 对象序列化
**作用:**以内存为基准,把内存中的对象存储到磁盘文件中去,称为对象序列化。
**对象字节输出流:**ObjectOutputStream
| 构造器 | 说明 |
|---|---|
| public ObjectOutputStream(OutputStream out) | 把低级字节输出流包装成高级的对象字节输出流 |
序列化方法:
| 方法名称 | 说明 |
|---|---|
| public final void writeObject(Object obj) | 把对象写出去到对象序列化流的文件中去 |
public static void main(String[] args) throws IOException {
//创建字节输出流
FileOutputStream fos = new FileOutputStream("Test08_14\\src\\data3.txt");
//创建对象字节输出流
ObjectOutputStream objectout = new ObjectOutputStream(fos);
//创建对象
Student s = new Student("name",18);
//调用特有方法writeObject 将对象写入文件
objectout.writeObject(s);
//关闭流
objectout.close();
}
3.2 对象反序列化
作用:以内存为基准,把存储到磁盘文件中去的对象数据恢复成内存中的对象
对象字节输入流:ObjectInputStream
| 构造器 | 说明 |
|---|---|
| public ObjectInputStream(InputStream out) | 把低级字节输如流包装成高级的对象字节输入流 |
反序列化方法:
| 方法名称 | 说明 |
|---|---|
| public Object readObject() | 把存储到磁盘文件中去的对象数据恢复成内存中的对象返回 |
public static void main(String[] args) throws Exception {
//创建字节输入流
FileInputStream fis = new FileInputStream("Test08_14\\src\\data3.txt");
//创建对象字节输入流
ObjectInputStream ois = new ObjectInputStream(fis);
//创建学生对象,用来接调用readObject()方法返回的student对象
Student s =(Student) ois.readObject();
//输出查看student对象
System.out.println(s);
}
4.打印流
作用:打印流可以实现方便、高效的打印数据到文件中去。
PrintStream,PrintWriter两个类
4.1 构造器及方法
PrintStream构造器
| 构造器 | 说明 |
|---|---|
| public PrintStream(OutputStream os) | 打印流直接通向字节输出流管道 |
| public PrintStream(File f) | 打印流直接通向文件对象 |
| public PrintStream(String filepath) | 打印流直接通向文件路径 |
方法
| 方法 | 说明 |
|---|---|
| public void print(Xxx xx) | 打印任意类型的数据出去 |
PrintWrite构造器
| 构造器 | 说明 |
|---|---|
| public PrintWriter(OutputStream os) | 打印流直接通向字节输出流管道 |
| public PrintWriter (Writer w) | 打印流直接通向字符输出流管道 |
| public PrintWriter (File f) | 打印流直接通向文件对象 |
| public PrintWriter (String filepath) | 打印流直接通向文件路径 |
方法
| 方法 | 说明 |
|---|---|
| public void print(Xxx xx) | 打印任意类型的数据出去 |
4.2 PrintStream和PrintWriter的区别
打印数据功能上是一模一样的,都是使用方便,性能高效(核心优势)
PrintStream继承自字节输出流OutputStream,支持写字节数据的方法。
PrintWriter继承自字符输出流Writer,支持写字符数据出去。
public static void main(String[] args) throws Exception {
// 1、创建一个打印流对象
//PrintStream ps = new PrintStream(new FileOutputStream("io-app2/src/ps.txt" , true));
// 追加数据,在低级管道后面加True
//PrintStream ps = new PrintStream("io-app2/src/ps.txt" );
PrintWriter ps = new PrintWriter("io-app2/src/ps.txt");
// 打印功能上与PrintStream的使用没有区别
ps.println(97);
ps.println('a');
ps.println(23.3);
ps.println(true);
ps.println("我是打印流输出的,我是啥就打印啥");
ps.close();
}
4.3 输出语句的重定向
作用:了解改变输出语句的位置到文件
public static void main(String[] args) throws Exception {
System.out.println("锦瑟无端五十弦");
System.out.println("一弦一柱思华年");
// 改变输出语句的位置(重定向)
PrintStream ps = new PrintStream("io-app2/src/log.txt");
System.setOut(ps); // 把系统打印流改成我们自己的打印流
System.out.println("庄生晓梦迷蝴蝶");
System.out.println("望帝春心托杜鹃");
}
5. Properties属性集对象
Properties代表的是一个属性文件,可以把自己对象中的键值对信息存入到一个属性文件中去。
属性文件:后缀是.properties结尾的文件,里面的内容都是 key=value,后续做系统配置信息的。
Properties和IO流结合的方法:
主要是用store() 方法和 load()方法
| 构造器 | 说明 |
|---|---|
| void load(InputStream inStream) | 从输入字节流读取属性列表(键和元素对) |
| void load(Reader reader) | 从输入字符流读取属性列表(键和元素对) |
| void store(OutputStream out, String comments) | 将此属性列表(键和元素对)写入此 Properties表中,以适合于使用 load(InputStream)方法的格式写入输出字节流 |
| void store(Writer writer, String comments) | 将此属性列表(键和元素对)写入此 Properties表中,以适合使用 load(Reader)方法的格式写入输出字符流 |
| public Object setProperty(String key, String value) | 保存键值对(put) |
| public String getProperty(String key) | 使用此属性列表中指定的键搜索属性值 (get) |
| public Set stringPropertyNames() | 所有键的名称的集合 (keySet()) |
Properties的作用?
可以存储Properties属性集的键值对数据到属性文件中去:void store(Writer writer, String comments)
可以加载属性文件中的数据到Properties对象中来:void load(Reader reader)
写数据
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
properties.setProperty("admin", "123456");
properties.setProperty("dlei", "003197");
properties.setProperty("heima", "itcast");
System.out.println(properties);
/*
参数一:保存管道 字符输出流管道
参数二:保存心得
*/
properties.store(new FileWriter("Test08_14/src/data444.txt")
, "this is 1!");
}
读数据
public static void main(String[] args) throws IOException {
// 需求:Properties读取属性文件中的键值对信息。(读取)
Properties properties = new Properties();
// 加载属性文件中的键值对数据到属性对象properties中去
properties.load(new FileReader("Test08_14/src/data444.txt"));
System.out.println(properties);
String rs = properties.getProperty("dlei");
System.out.println(rs);
properties.setProperty("admin","123wwwwwwwww");
System.out.println(properties);
}
6 IO框架
FileUtils主要有如下方法:
| 方法名 | 说明 |
|---|---|
| String readFileToString(File file, String encoding) | 读取文件中的数据, 返回字符串 |
| void copyFile(File srcFile, File destFile) | 复制文件。 |
| void copyDirectoryToDirectory(File srcDir, File destDir) | 复制文件夹。 |
步骤
- 在项目中创建一个文件夹:lib
- 将commons-io-2.6.jar文件复制到lib文件夹
- 在jar文件上点右键,选择 Add as Library -> 点击OK
- 在类中导包使用
public static void main(String[] args) throws Exception {
// 1.完成文件复制!
IOUtils.copy(new FileInputStream("D:\\resources\\hushui.jpeg"),
new FileOutputStream("D:\\resources\\hushui2.jpeg"));
// 2.完成文件复制到某个文件夹下!
FileUtils.copyFileToDirectory(new File("D:\\resources\\hushui.jpeg"), new File("D:/"));
// 3.完成文件夹复制到某个文件夹下!
FileUtils.copyDirectoryToDirectory(new File("D:\\resources") , new File("D:\\new"));
FileUtils.deleteDirectory(new File("D:\\new"));
FileUtils.deleteDirectory(new File("D:\\new"));
}

被折叠的 条评论
为什么被折叠?



