❤️持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第4天,点击查看活动详情
🎨 个人介绍
👉大家好,我是:旺仔不是程序员
👉认真分享技术,记录学习过程的点滴,如果我的分享能为你带来帮助,请支持我奥🍻
👉你的支持,是我每天更新的动力。
👉赞点:👍 留言:✍ 收藏:⭐
👉个人格言:想法一步一步的落实,才是你我前进最佳选择。
1. IO 流案例
1.1 集合到文件数据排序改进版
1 )案例需求
- 键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩)。要求按照成绩总分从高到低写入文本文件
- 格式:姓名,语文成绩,数学成绩,英语成绩 举例:林青霞,98,99,100
2 )分析步骤
- 定义学生类
- 创建TreeSet集合,通过比较器排序进行排序
- 键盘录入学生的数据
- 创建学生对象,把键盘录入的数据对应赋值给学生对象的成员变量
- 把学生对象添加到TreeSet集合
- 创建字符缓冲输出流对象
- 遍历集合,得到每个学生对象
- 把学生对象的数据拼接成指定格式的字符串
- 调用字符串缓冲输出流对象的方法写数据
- 释放资源
3 )代码实现
public static void main(String[] args) throws IOException{
TreeSet<Student> arraySet = new TreeSet<Student>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
// 第二个减去第一个就是 降序
// 第一个减去第二个就是 升序
// 成绩从高往低排
int num = s2.getSum()-s1.getSum();
//次要条件
int num2 = num==0 ? s2.getChineseScore() - s1.getChineseScore() : num;
int num3 = num2 == 0 ? s2.getMathScore()-s1.getMathScore(): num2;
int num4 = num3 == 0 ? s2.getEnglishScore() - s1.getEnglishScore() : num3;
return num4;
}
});
//键盘录入学生数据
for (int i = 0; i < 5 ; i++) {
Scanner input = new Scanner(System.in);
System.out.println("请录入第" + (i + 1) + "个学生信息");
System.out.println("姓名:");
String name = input.next();
System.out.println("语文成绩");
int chinese = input.nextInt();
System.out.println("数学成绩");
int math = input.nextInt();
System.out.println("英语成绩");
int english = input.nextInt();
//创建学生对象,进行赋值
Student s = new Student();
s.setName(name);
s.setChineseScore(chinese);
s.setMathScore(math);
s.setEnglishScore(english);
//把学生添加到集合中
arraySet.add(s);
}
//创建字符缓冲输出流对象
BufferedWriter bwriter = new BufferedWriter(new FileWriter("E:\BackEndLearning\work\Test1\studentList.txt"));
//遍历集合得到每一个学生对象
for(Student s : arraySet) {
// 按照指定格式进行拼接
StringBuilder sb = new StringBuilder();
sb.append(s.getName()).append(",").append(s.getChineseScore()).append(",").append(s .getMathScore()).append(",").append(s.getEnglishScore()).append(",").append(s.getSum());
//调用字符缓冲输出流的方法写数据
bwriter.write(sb.toString());
bwriter.newLine();
bwriter.flush();
}
// 释放资源
bwriter.close();
}
1.2 复制单级文件夹
1 )案例需求
- 把“E:\itcast”这个文件夹复制到 E:\BackEndLearning\work\Test1\itCast
2 )步骤分析
-
创建数据源目录File对象,路径是 E:\itCast、
-
获取数据源目录File对象名称
-
创建目的地目录File 对象, E:\BackEndLearning\work\Test1\itCast
-
判断第三步创建的File 是否存在,如果不存在,就创建
-
获取数据源目录下所有文件的File 数组
-
遍历 File 数组,得到每一个File 对象,该 File 对象,其实就是数据源文件
-
获取数据源文件File对象的名称
-
创建目的地文件File对象的名称,路径由(目的地目录 + 第七步获取的名称)组成
-
复制文件
- 由于不清楚数据源目录下的文件都是什么类型的,所以采用字节流复制文件
- 采用采数为File的构造方法
3 )代码实现
public static void main(String[] args) throws IOException{
//创建数据源目录
File srcFolder = new File("E:\itCast");
// 获取数据源目录File 对象的名称
String srcFolderName = srcFolder.getName();
//创建目的地对应的 File 对象
File destFolder = new File("E:\BackEndLearning\work\Test1",srcFolderName);
//判断目的地对应的 File 是否存在,如果不存在,就创建
if(!destFolder.exists()) {
destFolder.mkdir();
}
// 获取数据源目录下所有文件的File数组
File[] listFiles =srcFolder.listFiles();
//遍历File 数组,得到每一个File对象,该File对象,其实就是数据源文件
for(File srcFile : listFiles) {
if(listFiles != null) {
String fileName = srcFile.getName();
// 获取数据源文件 File 对象
File destFile = new File(destFolder, fileName);
//复制文件
copyFile(srcFile, destFile);
}
}
}
public static void copyFile(File srcFile , File destFile ) throws IOException{
//字节缓冲输入流
BufferedInputStream bReader= new BufferedInputStream(new FileInputStream(srcFile));
// 字节缓冲输出流
BufferedOutputStream bwriter= new BufferedOutputStream(new FileOutputStream(destFile));
byte[] bytes = new byte[1024];
int len;
while ((len = bReader.read(bytes)) != -1) {
bwriter.write(bytes, 0, len);
}
bReader.close();
bwriter.close();
}
注意:
写路径的时候不要有空格,会报错的
1.3 复制多级文件夹
1 )案例需求
- 把“E:\itcast”这个文件夹复制到 D盘目录下
2 )分析步骤
-
创建数据源File 对象,路径是 E:\itcast
-
创建目的地File 对象,路径是D:\
-
写方法实现文件夹复制,参数为数据源File对象和目的地File对象
-
判断数据源File是否为文件
-
是文件:直接复制,用字节流
-
不是文件:
- 在目的地下创建该目录
-
遍历获取该目录下的所有文件的File数组,得到每一个File 对象
-
回到3继续
-
3 )代码实现
public static void main(String[] args) throws IOException {
// 创建数据源File 对象,路径是E:\itCast
File srcFolder = new File("E:\itCast");
// 创建目的地File 对象,路径是D:\
File DestFolder = new File("D:\");
copyFolder(srcFolder,DestFolder);
}
public static void copyFolder(File srcFile, File destFile) throws IOException {
// 判断数据源File s是不是目录
if(srcFile.isDirectory()) {
// 在目的地下创建和数据源File名称一样的目录
String srcFileName = srcFile.getName();
File newFolder = new File(destFile,srcFileName);
if(!newFolder.exists()) {
newFolder.mkdir();
}
// 获取数据源File下所有文件或者目录的File数组
File[] fileArray = srcFile.listFiles();
// 遍历该File数组,得到每一个File
for (File file : fileArray) {
copyFolder(file,newFolder);
}
}else{
// 说明是文件,直接复制,用字节流
File newFile = new File(destFile,srcFile.getName());
copyFile(srcFile,newFile);
}
}
public static void copyFile(File srcFile, File destFile) throws IOException{
//字节缓冲输入流
BufferedInputStream in = new BufferedInputStream(new FileInputStream(srcFile));
//字节缓冲输出流
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(destFile));
byte[] bytes = new byte[1024];
int len;
while ((len = in.read(bytes)) != -1) {
out.write(bytes, 0, len);
}
in.close();
out.close();
}
1.4 复制文件夹的异常处理
1 )基本做法
public static void main(String[] args) {
}
// 使用try...catch...finally
private static void method1() {
FileReader reader = null;
FileWriter writer = null;
try {
reader = new FileReader("E:\BackEndLearning\work\Test1\bw.txt");
writer = new FileWriter("E:\BackEndLearning\work\Test\fox");
char[] chs = new char[1024];
int len;
while ((len = reader.read(chs)) != -1) {
writer.write(chs, 0, len);
}
}catch (IOException e) {
e.printStackTrace();
} finally {
if(writer!= null) {
try{
writer.close();
}catch (IOException e) {
e.printStackTrace();
}
}
if (reader!= null) {
try{
reader.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
//抛出处理
public static void method2() throws IOException {
FileReader reader = new FileReader("E:\BackEndLearning\work\Test1\bw.txt");
FileWriter writer = new FileWriter("E:\BackEndLearning\work\Test\fox");
char[] data = new char[1024];
int len;
while ((len = reader.read(data)) != -1) {
writer.write(data, 0, len);
}
reader.close();
writer.close();
}
2 )JDK7 版本改进
public static void main(String[] args) {
}
public static void method3(){
try ((FileReader fr = new FileReader("fr.txt");
FileWriter fw = new FileWriter("fw.txt");){
char[] date = new char[1024];
int len;
while((len = fr.read(date))!=-1) {
fw.write(date, 0, len);
}
}catch (IOException e) {
e.printStackTrace();
}
}
3 )JDK 9 版本改进
public static void main(String[] args) {
}
public static void method4() throws IOException {
FileReader reader = new FileReader("E:\BackEndLearning\work\Test1\bw.txt");
FileWriter writer = new FileWriter("E:\BackEndLearning\work\Test\fox.txt");
try(reader;writer) {
char[] data = new char[1024];
int len;
while ((len = reader.read(data)) != -1) {
writer.write(data, 0, len);
}
}catch(IOException e) {
e.printStackTrace();
}
}
2. IO 特殊操作流
2.1 标准输入流
1 )System 类中有两个静态的成员变量
- public static final InputStream in:标准输入流。通常该流对应于键盘输入或由主机环境或用户指定的另一个输入流
- public static fianl PrintStream out : 标准输出流。通常该流对应于显示输出或由主机环境或用户指定的另一个输出目标
2 )自己实现键盘录入数据
public static void main(String[] args) throws IOException{
// 标准输入流,以下就可以实现 无限读数据和输出数据
InputStream in = System.in;
int by;
while((by=in.read())!=-1) {
System.out.println((char)by);
}
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入一个字符串");
String line = reader.readLine();
System.out.println("你输入的字符串是:" + line);
System.out.println("请输入一个整数:");
int i = Integer.parseInt(reader.readLine());
System.out.println("你输入的整数是:" + i);
//自己实现键盘录入数据太麻烦了,所以Java就提供了一个类供我们使用
Scanner sc = new Scanner(System.in);
}
2.2 标准输出流
1 )System 类中有两个静态的成员变量
- public static final InputStream in:标准输入流。通常该流对应于键盘输入或由主机环境或用户指定的另一个输入流
- public static fianl PrintStream out : 标准输出流。通常该流对应于显示输出或由主机环境或用户指定的另一个输出目标
2 )输出语句的本质:是一个标准的输出流
- PrintStream ps = System.out;
- PrintStream 类有的方法,System.out都可以使用
3 )实例代码
public static void main(String[] args) {
//public static final PrintStream out:标准输出流
PrintStream out = System.out;
//输出数据
out.println("hello");
out.println(100);
out.println(true);
//System.out的本质是一个字节输出流
out.println("hello");
out.println(100.2);
}
2.3 字节打印流
1 )打印流分类
- 字节打印流:PrintStream
- 字符打印流:PrintWriter
2 )打印流的特点
- 只负责输出数据,不负责读取数据
- 永远不会抛出IOException
- 有自己特有的方法
3 )字节打印流
-
PrintStream(String fileName) : 使用指定文件名创建新的打印流
-
使用继承父类的方法写数据,查看的时候转码;使用自己的特有方法写数据,查看的数据原样输出
-
可以改变输出语句的目的地
- public static void setOut(PrintStream out) : 重新分配“标准”输出流
4 )代码实例
public static void main(String[] args) throws IOException {
// 字节打印流
PrintStream out = new PrintStream("E:\BackEndLearning\work\Test1\fox.txt");
// 写数据
out.write(97);
//特有方法写数据
out.print(97);
out.println();
out.print(98);
//释放资源
out.close();
}
2.4 字符流打印
1 )字符打印流构造方法
| 方法名 | 说明 |
|---|---|
| PrintWriter(String fileName) | 使用指定的文件名创建一个新的PrintWriter, 而不需要自动执行刷新 |
| PrintWriter(Writer out,boolean auto) | 创建一个新的PrintWriter out : 字符输出流autoFlush : 一个布尔值,如果为证,则printIn,printf,或format()方法将刷新输出缓冲区 |
2 )代码实例
public static void main(String[] args) throws IOException {
//创建一个新的PrintWriter,而不需要自动执行 行 刷新
PrintWriter writer = new PrintWriter("E:\BackEndLearning\work\Test1\fox.txt");
// 写数据
writer.write("hello");
writer.write("\r\n");
writer.flush();
writer.write("world");
writer.write("\r\n");
writer.flush();
PrintWriter writer2 = new PrintWriter(writer,true);
writer2.write("hello");
writer2.write("\r\n");
writer2.flush();
writer2.println("hello");
writer2.println("world");
//释放资源
writer.close();
writer2.close();
}
2.5 复制Java 文件打印流改进版
1 )案例需求
- 把E:\BackEndLearning\work\Test1\src\cn\heima\pro\Student.java 复制到 E:\itCast\Student.java
2 )分析步骤
- 根据数据源创建字符输入流对象
- 根据目的地创建字符输出流对象
- 读写数据,复制文件
- 释放资源
3 )代码实现
public static void main(String[] args) throws IOException{
//根据数据源创建字符输入流对象
BufferedReader reader = new BufferedReader(new FileReader("E:\BackEndLearning\work\Test1\src\cn\heima\pro\Student.java"));
//根据目的地创建字符输出流对象
PrintWriter writer = new PrintWriter(new FileWriter("E:\itCast\Student.java "),true);
//读写数据
String line;
while((line= reader.readLine())!=null) {
writer.write(line);
}
// 释放资源
reader.close();
writer.close();
}
2.6 对象序列化流
1 )对象序列化介绍
- 对象序列化:就是将对象保存在磁盘中,或者在网络中传输对象
- 这种机制就是使用一个字节序列表示一个对象,该字节序列包括:对象的类型,对象的数据和对象中存储的属性等信息
- 字节序列写到文件之后,相当于文件中持久保存了一个对象的信息
- 反之,该字节序列还可以从文件中读取回来,重构对象,对它进行反序列化
2 )对象序列化流: ObjectOutputStream
- 将Java对象的原始数据类型和图形写入OutputStream.可以使用ObjectInputStream读取(重构)对象
- 可以通过使用流的文件来实现对象的持久存储。如果流是网络中套接字流,则可以在另一台主机上或者另一个进程中重构对象
3 )构造方法
| 方法名 | 说明 |
|---|---|
| ObjectOutputStream(OutputStream out) | 创建一个写入指定的OutputStream的ObjectOutputStream |
4 )序列化对象的方法
| 方法名 | 说明 |
|---|---|
| void writerObject(Object obj) | 将指定的对象写入ObjectOutputStream |
5 )示例代码
- 学生类
public class Student implements Serializable {
/** 成员变量 */
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + ''' +
", age=" + age +
'}';
}
}
- 测试类
public static void main(String[] args) throws IOException {
//创建序列化流
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("E:\BackEndLearning\work\Test1\oos.txt"));
//创建学生对象
Student s = new Student("林青霞",30);
//将指定的对象写入ObjectOutputStream
oos.writeObject(s);
//释放资源
oos.close();
}
注意:
- 一个对象想要被序列化,该对象所属的类必须实现Serializable接口
- Serializable 是一个标记接口,实现该接口,不需要重写任何方法
2.7 对象的反序列化流
1 )对象反序列化流:ObjectInputStream
- ObjectInputStream 反序列化流使用ObjectOutputStream 编写的原始数据和对象
2 )构造方法
| 方法名 | 说明 |
|---|---|
| ObjectInputStream(InputStream in) | 创建从指定的InputStream读取ObjectInputStream |
3 )反序列化的方法
| 方法名 | 说明 |
|---|---|
| Object readObject() | 从ObjectInputStream 读取一个对象 |
4 )示例代码
public static void main(String[] args) throws IOException,ClassNotFoundException{
// 创建反序列化流
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("E:\BackEndLearning\work\Test1\oos.txt"));
// 读取一个对象
Object obj = ois.readObject();
Student student = (Student) obj;
System.out.println(student.getName()+","+student.getAge());
//释放资源
ois.close();
}
2.8 serialVersionUID & transient
1 )serialVersionUID
-
用对象序列化流 序列化 一个对象后,假如我们修改了对象所属的类文件,读取数据会不会出问题呢?
- 会出问题,会抛出InvalidClassException异常
-
如果出现问题,如何结果呢
-
重新序列化
-
给对象所属的类加一个serialVersionUID
- private static final serialVersionUID = 42L
-
2 )transient
-
如果一个对象中的某个成员变量的值不想被序列化,又该如何实现呢
- 给成员变量加transient关键字修饰,该关键字标记的成员变量不参与序列化过程
3 )示例代码
- 学生类
public class Student implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
/** 成员变量 */
private String name;
private transient int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + ''' +
", age=" + age +
'}';
}
}
- 测试类
public static void main(String[] args) throws IOException, ClassNotFoundException {
writer();
reader();
}
/** 反序列化 */
private static void reader() throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("E:\BackEndLearning\work\Test1\oos.txt"));
Student student = (Student) ois.readObject();
System.out.println(student.getName() + "," + student.getAge());
ois.close();
}
/** 序列化 */
private static void writer() throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("E:\BackEndLearning\work\Test1\oos.txt"));
// 创建一个学生对象
Student s = new Student("林青霞", 30);
// 写数据
oos.writeObject(s);
oos.close();
}
3. Propertise 集合
3.1 Properties 作为Map 集合的使用
1 )Properties 介绍
- 是一个Map体系的集合类
- Properties可以保存到流中或从流中加载
- 属性列表中的每个键及其对应的值都是一个字符串
2 )Proprities基本使用
public static void main(String[] args) {
// 创建集合对象
Properties prop = new Properties();
// 存储元素
prop.put("itheima001", "林青霞");
prop.put("itheima002", "张曼玉");
prop.put("itheima003", "王祖贤");
// 遍历集合
Set<Object> keys = prop.keySet();
for (Object key : keys) {
Object value = prop.get(key);
System.out.println(key + "=" + value);
}
}
注意:
- 它的创建和平常创建类一样,不能使用泛型创建
3.2 ProPerties 作为Map集合的特有方法
1 )特有方法
| 方法名 | 说明 |
|---|---|
| Object setProperty(String key,String value) | 设置集合的键和值,都是String类型,底层调用Hashtable方法put |
| String getProperty(String key) | 使用次属性列表中指定的键搜索属性 |
| Set stringPropertyNames() | 从该属性列表中返回一个不可修改的键集,其中键及其对应的值是字符串 |
2 )示例代码
public static void main(String[] args) {
// 创建集合对象
Properties prop = new Properties();
//设置集合的键和值,都是 String类型,底层调用Hashtable方法put
prop.setProperty("itheima001", "林青霞");
prop.setProperty("itheima002", "张曼玉");
prop.setProperty("itheima003", "王祖贤");
//使用此属性列表中指定的键搜索属性
System.out.println(prop.getProperty("itheima001"));
System.out.println(prop.getProperty("itheima002"));
//:从该属性列表中返回一个不可修改的键集,其中 键及其对应的值是字符串
Set<String> names = prop.stringPropertyNames();
for(String key : names){
String value = prop.getProperty(key);
System.out.println(key + "," +value);
}
}
3.3 Properties 和 IO 流相结合的方法
1 )和IO流结合的方法
| 方法名 | 说明 |
|---|---|
| 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)方法的格式写入输出字符流 |
2 )示例代码
public static void main(String[] args) throws IOException{
// 把集合中的数据保存到文件
myStore();
// 把文件中的数据加载到集合
myLoad();
}
private static void myLoad() throws IOException{
Properties prop = new Properties();
FileReader reader = new FileReader("E:\BackEndLearning\work\Test1\fw.txt");
prop.load(reader);
reader.close();
System.out.println(prop);
}
private static void myStore() throws IOException {
Properties prop = new Properties();
prop.setProperty("itheima001","林青霞");
prop.setProperty("itheima002","张曼玉");
prop.setProperty("itheima003","王祖贤");
FileWriter writer = new FileWriter("E:\BackEndLearning\work\Test1\fw.txt");
prop.store(writer,null);
writer.close();
}
3.4 游戏次数案例
1 )案例需求
- 实现猜数字小游戏,只能玩3次,如果还想玩,提示:游戏试玩已结束,想玩请充值(www.itCast.cn)
2 )步骤分析
-
写一个游戏类,里边有一个猜数字的小游戏
-
写一个测试类,测试类中有main()方法,main()方法中写如下代码:
- 从文件中读取数据到Properties集合中,用load()方法实现
- 文件已经存在:game.txt
- 里面有一个数据值:count=0
- 通过Properties集合获取到玩游戏的次数
- 判断次数是否达到3次
- 如果到了,给出提示:游戏试玩已结束,想玩请充值(www.itCast.cn)
- 如果不到3次
- 次数+1,重新写回文件,用Properties()的store()方法实现玩游戏
3 )代码实现
- 猜数字类
public class GuessNumber {
//创建随机获取类
static Random random = new Random();
//键盘输入类
static Scanner input = new Scanner(System.in);
public static void start() {
//获取1-100的随机数
int guessnumber = random.nextInt(100) + 1;
while(true) {
System.out.println("请输入你猜的数字");
int number = input.nextInt();
if (number < guessnumber) {
System.out.println("你猜小了");
continue;
} else if (number > guessnumber) {
System.out.println("你猜大了");
continue;
} else {
System.out.println("你猜中了");
break;
}
}
}
}
- 使用Properties的类
public static void main(String[] args) throws IOException{
//从文件中读取数据到Properties集合
Properties prop = new Properties();
FileReader reader = new FileReader("E:\BackEndLearning\work\Test1\game.txt");
prop.load(reader);
reader.close();
//通过Properties集合获取到玩游戏的次数
String count = prop.getProperty("count");
int number = Integer.parseInt(count);
//判断次数是否到了三次
if(number >=3) {
//如果到了,给出提示:游戏试玩已结束
System.out.println("游戏试玩已结束,想玩请充值(www.itcast.cn)");
}else {
// 玩游戏
GuessNumber.start();
//次数+1,重新写回文件
number++;
prop.setProperty("count",String.valueOf(number));
FileWriter writer = new FileWriter("E:\BackEndLearning\work\Test1\game.txt");
prop.store(writer,null);
writer.close();
}
}public class GuessNumber {
//创建随机获取类
static Random random = new Random();
//键盘输入类
static Scanner input = new Scanner(System.in);
public static void start() {
//获取1-100的随机数
int guessnumber = random.nextInt(100) + 1;
while(true) {
System.out.println("请输入你猜的数字");
int number = input.nextInt();
if (number < guessnumber) {
System.out.println("你猜小了");
continue;
} else if (number > guessnumber) {
System.out.println("你猜大了");
continue;
} else {
System.out.println("你猜中了");
break;
}
}
}
}
4. 字节流和字符流的使用流程
1. 字节流的使用流程
- 创建File对象
- 创建文件字节流FileInputStream 或者 FileOutputStream 字节流对象
- 使用缓冲字节流BufferedInputStream 或BufferedOutputDtream流
2. 字符流的使用流程
- 创建File对象
- 创建文件字节流FileInputStream 或者 FileOutputStream 字节流对象
- 创建InputStreamWriter 或者 OutputStreamWriter ,注意:这个流的底层必须是字节流,比如FileInputStream 或者 FileOutputStream 。不能是File类
- 创建BufferedReader 或者 BufferedWriter ,注意:只有字符缓冲流有NewLine()方法,字节缓冲流没有
🎈看完了不妨给我点个赞吧,👉你的支持,是我每天更新的动力...