此笔记根据老师上课内容整理,并非本人100%原创
IO概述
什么是IO?
i: input
o: output
为什么有IO?
学习了File,可以对文件进行操作
对文件的内容进行操作 使用IO
java中如何实现IO功能
通过流模型实现IO功能
IO的分类
按流向分(以内存为参照物)
-
输入流: 外部设备 --> 内存 read
-
输出流: 内存 ---> 外部设备 write
按照数据类型
-
字节流 : 逻辑单位是字节 , 1B = 0000 0000
-
字符流: 逻辑单位是字符(把它理解为一种文化符号,abc, 你好, の). 本质传输的还是字节
4个抽象基类
字节输出流: OutputStream
字节输入流: InputStream
字符输出流: Writer
字符输入流:Reader
由这4个抽象基类派生的子类都是以其父类名作为后缀的
举例:
FileOutputStream
FileInputStream
FileWriter
FileReader
什么时候用什么流?
对于文本文件,推荐使用字符流 .txt .java .cpp
对于非文本文件,使用字节流 .mp3 .mp4 .jpg .png .avi .pdf .word .ppt .exe
字节流是万能的
字节流
字节输出流
抽象基类OutputStream
此抽象类是表示输出字节流的所有类的超类
继承关系
成员方法
void | close() 关闭此输出流并释放与此流有关的所有系统资源。 |
---|---|
void | flush() 刷新此输出流并强制写出所有缓冲的输出字节。 |
void | write(byte[] b) 将 b.length 个字节从指定的 byte 数组写入此输出流。 |
void | write(byte[] b, int off, int len) 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流。 |
abstract void | write(int b) 将指定的字节写入此输出流。write 的常规协定是:向输出流写入一个字节。要写入的字节是参数 b 的八个低位。b 的 24 个高位将被忽略。 |
具体子类
FileOutputStream文件字节输出流
文件输出流是用于将数据写入 File
继承关系
构造方法
FileOutputStream(File file) 创建一个向指定 File 对象表示的文件中写入数据的文件输出流。 |
---|
FileOutputStream(File file, boolean append) 创建一个向指定 File 对象表示的文件中写入数据的文件输出流。 |
FileOutputStream(String fileName) 创建一个向具有指定名称的文件中写入数据的输出文件流。 |
FileOutputStream(String name, boolean append) 创建一个向具有指定 name 的文件中写入数据的输出文件流。 |
成员方法
void | close() 关闭此输出流并释放与此流有关的所有系统资源。 |
---|---|
void | flush() 刷新此输出流并强制写出所有缓冲的输出字节。 |
void | write(byte[] b) 将 b.length 个字节从指定的 byte 数组写入此输出流。 |
void | write(byte[] b, int off, int len) 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流。 |
void | write(int b) 将指定的字节写入此输出流。write 的常规协定是:向输出流写入一个字节。要写入的字节是参数 b 的八个低位。b 的 24 个高位将被忽略。 |
package _17io01.com.cskaoyan.bytestream._01fileoutputstream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @description:
* @author: 景天
* @date: 2022/6/23 15:55
**/
/*
写数据的步骤
1.创建输出流对象
2.write
3.close 释放资源
*/
public class Demo {
public static void main(String[] args) throws IOException {
//1.创建输出流对象
//File file = new File("a.txt");
//FileOutputStream out = new FileOutputStream(new File("a.txt"));
FileOutputStream out = new FileOutputStream("b.txt");
//2.write
// write(int b) 写单个字节
//out.write(97);
// write(byte[])
String s = "abc";
byte[] bytes = s.getBytes();
//out.write(bytes);
// 是abc? 还是aabc?
// write(byte[], int off, int len)
out.write(bytes, 1, 2);
//3.close 释放资源
out.close();
}
}
注意事项
-
当创建输出流对象的时候发什么了什么事?
-
创建之前,jvm会到操作系统看一下这个文件是否存在
-
如果不存在,帮我们创建
-
如果存在,覆盖内容,重新写
-
-
如何实现文件追加功能?
-
利用带append的构造方法
-
package _17io.com.cskaoyan.bytestream._01fileoutputstream; import java.io.FileOutputStream; import java.io.IOException; /** * @description: 文件追加 * @author: 景天 * @date: 2022/6/23 16:42 **/ public class Demo2 { public static void main(String[] args) throws IOException { // 创建输出流对象 FileOutputStream out = new FileOutputStream("b.txt", true); // write out.write("哈哈哈哈".getBytes()); // close out.close(); } }
-
-
如何实现换行功能?
-
通过换行符
-
package _17io.com.cskaoyan.bytestream._01fileoutputstream; import java.io.FileOutputStream; import java.io.IOException; /** * @description: 换行功能 * @author: 景天 * @date: 2022/6/23 16:42 **/ public class Demo3 { public static void main(String[] args) throws IOException { // 创建输出流对象 FileOutputStream out = new FileOutputStream("a.txt"); // write out.write("abc".getBytes()); // 写入换行符 // "\r\n" out.write("\r\n".getBytes()); out.write("abc".getBytes()); // "\r" out.write("\r".getBytes()); out.write("abc".getBytes()); // "\n" out.write("\n".getBytes()); out.write("abc".getBytes()); //System.lineSeparator() 系统默认换行符 out.write(System.lineSeparator().getBytes()); out.write("abc".getBytes()); // close out.close(); } }
-
-
异常处理
-
传统try-catch
-
package _17io.com.cskaoyan.bytestream._01fileoutputstream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /** * @description: try-catch * @author: 景天 * @date: 2022/6/23 17:13 **/ public class Demo4 { public static void main(String[] args) { // 创建输出流对象 FileOutputStream out = null; try { // 假设有异常 out = new FileOutputStream("a.txt"); // write out.write("abc".getBytes()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { // close // 做一下判断 if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
-
-
try-with-resources
-
语法 try(资源,实现了AutoCloseable接口的类都可以被视为资源){ // 可能出现异常的代码 // 出了try 资源被自动释放 }catch(){ }
-
package _17io.com.cskaoyan.bytestream._01fileoutputstream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /** * @description: try-with-resources * @author: 景天 * @date: 2022/6/23 17:13 **/ public class Demo5 { public static void main(String[] args) { // 创建输出流对象 try(FileOutputStream out = new FileOutputStream("a.txt")) { // write out.write("abc".getBytes()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
-
验证一下自动释放
-
package _17io.com.cskaoyan.bytestream._01fileoutputstream; /** * @description: * @author: 景天 * @date: 2022/6/23 17:22 **/ /* 验证自动释放操作 */ public class Demo6 { public static void main(String[] args) { // try-with-resources try(A a = new A()) { // 调用func a.func(); } catch (Exception e) { e.printStackTrace(); } } } //定义一个类实现AutoCloseable接口 class A implements AutoCloseable{ @Override public void close() throws Exception { System.out.println("close执行了"); } public void func() { System.out.println("func执行了"); } }
-
-
-
为什么要close?
-
io资源属于操作系统资源,并不属于jvm资源,并不会被回收,只能通过close方法显式的释放资源.只用用到了不属于jvm的资源, 一般都需要释放资源.
-
BufferedOutputStream缓冲字节输出流
该类实现缓冲的输出流。通过设置这种输出流,应用程序就可以将各个字节写入底层输出流中,而不必针对每次字节写入调用底层系统
继承关系
构造方法
BufferedOutputStream(OutputStream out) 创建一个新的缓冲输出流,以将数据写入指定的底层输出流。 |
---|
BufferedOutputStream(OutputStream out, int size) 创建一个新的缓冲输出流,以将具有指定缓冲区大小的数据写入指定的底层输出流。 |
成员方法
3个write
Demo
package _17io.com.cskaoyan.bytestream._05bufferedoutputstream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @description: write数据
* @author: 景天
* @date: 2022/6/24 11:00
**/
public class Demo2 {
public static void main(String[] args) throws IOException {
// 创建缓冲输出流对象
BufferedOutputStream out =
new BufferedOutputStream(new FileOutputStream("a.txt"));
// write
// 单字节
out.write(97);
// 多字节
out.write("宝,我今天虽然很累,但是还是有想你哦".getBytes());
// flush
//out.flush();
// close
out.close();
}
}
注意:
-
使用的是带缓冲区的流,记得flush操作
-
close方法 会执行flush
-
如果缓冲区满了,自动刷新