系统学习JAVA第十七天(字节流、字符流、缓冲的字节流、缓冲的字符流、将字节流转换为缓冲的字符流、面向对象——>字节流转成对象)
系统学习JAVA第十七天
第一阶段在2021.2.1结束了!
一、数据传输
IO输入和输出,硬盘之间的数据交换
1、文件读写流程
①创建文件的容器
②判断方向 合适的类 创建和文件之间的通道
③ 调用对应的方法
④ 查看
⑤关闭
2、字节流
优点:相比于字符流来说传输快
缺点:因为大多数文件都是字符所以解析麻烦
应用场景:图片、视频、音频、压缩包,使用字节流的文档英文
InputStream、FileInputStream
read方法是读取当前文件,一般是用byte数组存储数据,它有返回值,返回的是当前文件的字节数,当返回值为-1时文档就到
了末尾OuputStream、FileOutputStream
write方法是将字节写入当前文件,默认是覆盖原来文件中的内容,将append设置为true就不会覆盖原来的内容了
//字节输入流
InputStream is = new FileInputStream(file);
//字节输出流
OutputStream os = new FileOutputStream(file);
3、字符流
优点:解析方便
缺点:相比于字节流来说传输慢
应用场景:中文
Reader、FileReader
read方法是读取当前文件,一般是用char数组存储数据,它有返回值,返回的是当前文件的字符数,当返回值为-1时文档就到
了末尾Writer、FileWriter
write方法是将字符写入当前文件,默认是覆盖原来文件中的内容,将append设置为true就不会覆盖原来的内容了
//字符输入流
Reader r = new FileReader(file);
//字符输出流
Writer w = new FileWriter(file);
4、缓冲的字节流
缓冲的字节流速度比普通的字节流快,但是没有其他的特殊方法
BufferedInputStream
read方法是读取当前文件,一般是用byte数组存储数据,它有返回值,返回的是当前文件的字节数,当返回值为-1时文档就到
了末尾BufferedOutputStream
write方法是将字节写入当前文件,默认是覆盖原来文件中的内容,将append设置为true就不会覆盖原来的内容了
//缓冲字节输入流
// InputStream iss = new FileInputStream(file);
// BufferedInputStream bis = new BufferedInputStream(is);BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));//缓冲字节输出流
// OutputStream oss = new FileOutputStream(file);
// BufferedOutputStream bos = new BufferedOutputStream(oss);BufferedOutputStream bos =new BufferedOutputStream(new FileOutputStream(file));
5、缓冲的字符流
读取文档按行读取,写出文档按行书写,可以使用缓冲字符流
BufferedReader
read方法是读取当前文件,一般是用char数组存储数据,它有返回值,返回的是当前文件的字符数,当返回值为-1时文档就到
了末尾
readLine方法可以实现文件的按行读的效果,它有返回值,返回的是当前文件的字符串, 当返回值为null时文档就到了末尾BufferedWriter
write方法是将字节写入当前文件,默认是覆盖原来文件中的内容,将append设置为true就不会覆盖原来的内容了
newLine方法可以实现文件的按行写的效果, 搭配write实习按行写
//缓冲字符输入流
// Reader rr = new FileReader(file);
// BufferedReader br = new BufferedReader(rr);BufferedReader br = new BufferedReader(new FileReader(file));//缓冲字符输出流
// Writer ww = new FileWriter(file);
// BufferedWriter wr = new BufferedWriter(ww);BufferedWriter bw = new BufferedWriter(new FileWriter(file));
6、将字节流转换为缓冲的字符流
如果现在我们有字节流,实现按行读写?将字节流转字符流
输入流:InputStreamReader输出流:OutputStreamWriter
//因为只有缓冲字符流能一行一行的读取和写出数据//所以如果想将字节文件也一行一行的读或者写,就需要将缓冲字节流转换成缓冲字符流//字节流————>字符流————>缓冲字符流//读取
// InputStream is = new FileInputStream(file);
// InputStreamReader isr = new InputStreamReader(is);
// BufferedReader bf = new BufferedReader(isr);BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));//写出
// OutputStream os = new FileOutputStream(file);
// OutputStreamWriter osw = new OutputStreamWriter(os);
// BufferedWriter bw = new BufferedWriter(osw);BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
7、面向对象——>字节流转成对象
存储对象,对象需要具有转换成字节流的能力,所以要实现序列化的接口(implements Serializable)
序列化:对象转成字节流的功能
反序列化:读取对象就是把字节流转成对象
输入流:ObjectInputStream 输出流:ObjectOutputStream
//对象流,需要通过字节流来实现对象的读取和写入//字节流————>对象字节流//输入
// InputStream is = new FileInputStream(file);
// ObjectInputStream ois = new ObjectInputStream(is);ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));//输出
// OutputStream os = new FileOutputStream(file);
// ObjectOutputStream oos = new ObjectOutputStream(os);ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
二、案例
我将所有要使用到的输入输出流都封装在了IO这个类中,一个是父类Animal实现了Serializable接口,一个是子类Cat继承了Animal父类,最后还有一个是测试类Test(Test当中的大多数代码都被我注释了,要接口注释只需要删掉前面的//即可,或者选中要解开注释的代码按下Ctrl+?)
1、IO类
package com.tangxin16.zuoye;import java.io.*;public class IO {//字节输入流方法 输入一个字节文件就可以读取它里面的全部内容public void Input(File file){//如果没有就新建一个文件if(file.exists()==false){try {file.createNewFile();} catch (IOException e) {e.printStackTrace();}}InputStream is = null;try {is = new FileInputStream(file);//定义byte数组最大容量为1024byte[] bytes = new byte[1024];//每次最多只读1024个字节int a = is.read(bytes);//如果a的值不等于-1说明文件还没有读到最后一个字节,所以就继续读if(a != -1){a=is.read(bytes);}System.out.println(new String(bytes));} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {try {is.close();} catch (IOException e) {e.printStackTrace();}}}//字节输出流 输入一个文件,要输入的字符串,和是否要拼接在原来字符串后面的参数true就是要拼接,false就是不需要//这样就可以将你要输入的字符串输入到文本当中public void Output(File file,String s,Boolean add){OutputStream os = null;try {if(add == true){//如果add传进来的是true那么就在字符后面继续添加你输入的字符os = new FileOutputStream(file,true);}else {//否则就替换掉原来的字符os = new FileOutputStream(file);}os.write(s.getBytes());} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {try {os.close();} catch (IOException e) {e.printStackTrace();}}}//将一个文件中的字符复制到另一个文件当中 输入2个文本文件和一个是否需要拼接的boolean类型public void CopyFile(File ifile,File ofile,boolean add){if(ifile.exists()==false){try {ifile.createNewFile();} catch (IOException e) {e.printStackTrace();}}InputStream is = null;OutputStream os = null;try {is = new FileInputStream(ifile);if(add == true){os = new FileOutputStream(ofile,true);}else {os = new FileOutputStream(ofile);}//先读再取每次最多读和存1024个字节byte[] bytes = new byte[1024];int a = is.read(bytes);while (a!=-1){os.write(bytes,0,a);a = is.read(bytes);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {try {is.close();os.close();} catch (IOException e) {e.printStackTrace();}}}//使用字符流读取文件public void Reader(File file){Reader r = null;try {r = new FileReader(file);char[] chars = new char[512];int a = r.read(chars);while (a != -1){a = r.read(chars);}System.out.println(chars);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {try {r.close();} catch (IOException e) {e.printStackTrace();}}}//使用字符流写入文件public void Writer(File file,String s,boolean add){Writer w = null;try {if(add == true){w = new FileWriter(file,true);}else {w = new FileWriter(file);}w.write(s);} catch (IOException e) {e.printStackTrace();}finally {try {w.close();} catch (IOException e) {e.printStackTrace();}}}//将对象输出到文本文件 输入一个文本文件,一个动物对象,一个是否拼接的boolean类型public void ObjectOutFile(File file,Animal[] animal,boolean add){ObjectOutputStream oos = null;try {if(add==true){//因为对象输出流不能直接输出需要借助字节输出流才行oos = new ObjectOutputStream(new FileOutputStream(file,true));}else {oos = new ObjectOutputStream(new FileOutputStream(file));}oos.writeObject(animal);} catch (IOException e) {e.printStackTrace();}finally {try {oos.close();} catch (IOException e) {e.printStackTrace();}}}//对象输入流,用来读取文本中的对象 需要输入一个文本和一个对象public Object ObjectInFile(File file,Animal[] animal){ObjectInputStream ois = null;try {ois = new ObjectInputStream(new FileInputStream(file));animal = (Animal[]) ois.readObject();for (Animal a:animal) {System.out.println(a);}} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();}finally {try {ois.close();return animal;} catch (IOException e) {e.printStackTrace();return animal;}}}//使用缓冲字节流读取文件中的字节public void BffInput(File file){if(file.exists()==false){try {file.createNewFile();} catch (IOException e) {e.printStackTrace();}}BufferedInputStream bis = null;try {bis = new BufferedInputStream(new FileInputStream(file));byte[] bytes = new byte[1024];int a = bis.read(bytes);while (a!=-1){a = bis.read(bytes);}System.out.println(new String(bytes));} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {try {bis.close();} catch (IOException e) {e.printStackTrace();}}}//使用缓冲字节流将字节写入文件中public void BffOutput(File file,String s,Boolean add){BufferedOutputStream bos = null;try {if(add == true){bos = new BufferedOutputStream(new FileOutputStream(file,true));}else {bos = new BufferedOutputStream(new FileOutputStream(file));}bos.write(s.getBytes());} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {try {bos.close();} catch (IOException e) {e.printStackTrace();}}}//使用缓冲字符流读取文件中的字符public void BffReader(File file){BufferedReader br = null;try {br = new BufferedReader(new FileReader(file));String s = br.readLine();while (s != null){System.out.println(s);s = br.readLine();}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {try {br.close();} catch (IOException e) {e.printStackTrace();}}}//使用缓冲字符流写入文件public void BffWriter(File file,String s,boolean add){BufferedWriter br = null;try {if(add == true){br = new BufferedWriter(new FileWriter(file,true));}else {br = new BufferedWriter(new FileWriter(file));}//一次写入10行for (int i = 0; i < 10; i++) {br.write(s+i);br.newLine();}} catch (IOException e) {e.printStackTrace();}finally {try {br.close();} catch (IOException e) {e.printStackTrace();}}}//用缓冲字符流一行一行的将一个文件中的字符复制到另一个文件当中public void BffCopyFileString(File ifile,File ofile,boolean add){BufferedReader br = null;BufferedWriter bw = null;try {br = new BufferedReader(new FileReader(ifile));if(add == true){bw = new BufferedWriter(new FileWriter(ofile,true));}else {bw = new BufferedWriter(new FileWriter(ofile));}String str = br.readLine();while (str!=null){bw.write(str);bw.newLine();str = br.readLine();}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {try {br.close();bw.close();} catch (IOException e) {e.printStackTrace();}}}//用缓冲字节流转换为缓冲字符流后一行一行的将一个文件中的字符复制到另一个文件当中public void BffCopyFileByte(File ifile,File ofile,boolean add){if(ifile.exists()==false){try {ifile.createNewFile();} catch (IOException e) {e.printStackTrace();}}BufferedReader br = null;BufferedWriter bw = null;try {br = new BufferedReader(new InputStreamReader(new FileInputStream(ifile)));if(add == true){bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(ofile)));}else {bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(ofile)));}String str = br.readLine();while (str!=null){bw.write(str);bw.newLine();str = br.readLine();}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {try {br.close();bw.close();} catch (IOException e) {e.printStackTrace();}}}
}
2、Animal类
package com.tangxin16.zuoye;import java.io.Serializable;public abstract class Animal implements Serializable {public void run(){}
}
3、Cat类
package com.tangxin16.zuoye;public class Cat extends Animal{private String name;private int age;public Cat(int age,String name) {this.age = age;this.name = name;}public Cat(){}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;}@Overridepublic void run(){System.out.println("猫在跑");}@Overridepublic String toString() {return "Cat{" +"name='" + name + '\'' +", age=" + age +'}';}
}
4、Test类
package com.tangxin16.zuoye;import java.io.File;
import java.util.Scanner;public class Test {public static void main(String[] args) {//读的文件File ifile = new File("C:\\Users\\tangxin\\Desktop\\java\\Iabc.txt");//写的文件File ofile = new File("C:\\Users\\tangxin\\Desktop\\java\\Oabc.txt");//创建IO类的对象IO io = new IO();// //读取ifile文件中的字节
// io.Input(ifile);// //Scanner输入语句
// Scanner scanner = new Scanner(System.in);
// //将输入的语句赋值给s
// String s = scanner.nextLine();
// //将字符串s的值写人ofile文件,传入ture的意思是s添加在上一次字节的后面
// io.Output(ofile,s,true);
// //读取ofile文件中的字节
// io.Input(ofile);// //将ifile文件中的字节复制到ofile文件中,因为传入的是true所以这次的传入是添加在上一次字节的后面
// io.CopyFile(ifile,ofile,true);
// //读取ofile文件中的字节
// io.Input(ofile);// //Scanner输入语句
// Scanner scanner = new Scanner(System.in);
// //将输入的语句赋值给s
// String s = scanner.nextLine();
// //使用字符流写出
// io.Writer(ofile,s,true);
// //使用字符流读取
// io.Reader(ofile);// //新建一个animal类的数组
// Animal[] animal = new Cat[10];
// //为animal下的子类Cat赋值,每循环一次赋一次值
// for (int i = 0; i < animal.length; i++) {// animal[i] = new Cat(i,"小憨憨"+i);
// }
// //写入animal的对象到ofile文件中,且覆盖原来的内容
// io.ObjectOutFile(ofile,animal,false);
// //从ofile文件中读取animal的对象
// io.ObjectInFile(ofile,animal);// //Scanner输入语句
// Scanner scanner = new Scanner(System.in);
// //将输入的语句赋值给s
// String s = scanner.nextLine();
// //使用缓冲字节流写入
// io.BffOutput(ofile,s,false);
// //使用缓冲字节流读取
// io.BffInput(ofile);// //Scanner输入语句
// Scanner scanner = new Scanner(System.in);
// //将输入的语句赋值给s
// String s = scanner.nextLine();
// //使用缓冲字符流写入
// io.BffWriter(ifile,s,false);
// //使用缓冲字符流读取
// io.BffReader(ofile);// //用缓冲字符流将ifile中的字符数据,复制到ofile文件中,按行进行读取和输入,ture表示拼接
// io.BffCopyFileString(ifile,ofile,true);
// //使用缓冲字符流读取
// io.BffReader(ofile);// //用缓冲字节流转换为缓冲字符流后将ifile中的字符数据,复制到ofile文件中,按行进行读取和输入,ture表示拼接
// io.BffCopyFileByte(ifile,ofile,true);
// //使用缓冲字符流读取
// io.BffReader(ofile);}
}
系统学习JAVA第十七天(字节流、字符流、缓冲的字节流、缓冲的字符流、将字节流转换为缓冲的字符流、面向对象——>字节流转成对象)相关推荐
- 系统学习 Java IO (六)----管道流 PipedInputStream/PipedOutputStream
目录:系统学习 Java IO---- 目录,概览 PipedInputStream 类使得可以作为字节流读取管道的内容. 管道是同一 JVM 内的线程之间的通信通道. 使用两个已连接的管道流时,要为 ...
- 作为初学者,应该如何系统学习Java呢?
Java编程语言发展的迅猛,每年的Java人才缺口人数都高达百万以上.作为IT小白应该如何系统学习Java呢?作为过来人,小千在这里给大家一些中肯的意见,希望对大家的工作和学习Java有一定的借鉴作用 ...
- 你为什么学不好Java?系统学习Java的七大因素
随着传统行业的没落,越来越多的人准备进军互联网行业,如何学好Java成功就业的问题在网上越来越热,在知乎上面看了好多介绍学习方法的文章和一些优秀答主的高赞回答.多数创作都是围绕着学习什么技术比较专业性 ...
- 如何系统的学习java_如何系统学习java
如何系统学习java Java作为一门名副其实的工业级语言,语法友好,学习简单,大规模的应用给代码质量的管控带来了困难,特别是团队开发中,开发过程中的规范会直接影响最终项目的稳定性.下面小编收集了一些 ...
- 零基础如何系统学习Java Web?
自学 java 的时候,不仅需要关注 java 入门的相关知识,还需要关注自己的自学方法.掌握好自学的方法,对构筑知识框架有很大帮助. 怎么样在没人指导的情况下自己去练习.自学和在学校上课的时候不一样 ...
- 字节流转化为文件流_字节流转成字符串之后,在通过字符串转成字节流后的文件为什么会不一样?...
public static void main(String[] args) throws Exception { File sourceFile = new File("/home/joy ...
- 【JAVA基础】重新系统学习Java(七)常用API(String、ArrayList)
目录 常用API(String.ArrayList) String String类概述 创建字符串对象的2种方式 String类常见面试题 String类常用API-字符串内容比较 String类常用 ...
- 除了《深入理解 Java 虚拟机》,还可以看怎么系统学习 Java 虚拟机?
作为一名优秀的 Java 开发程序员,以及想那些想要学习 Java 更深层一点的知识的同学,对 JVM 的熟悉与熟练使用是必不可缺的核心技能了,也是每个 Java 程序员应该要做到的. 深入学习 JV ...
- java压缩文件_Linux 系统学习--Java学习第118天
第141次(Linux) 学习主题:Linux 学习目标: 1 掌握Linux常用命令 对应作业 Linux常用命令1 如何使用cd命令切换固定位置? cd 目录名 中间必须要有空格隔开 如何使用cd ...
最新文章
- 汪潮涌:AI创业落地为王,技术和算法难以成为核心壁垒
- C++中public、protected、private的差别
- Windows下载Android源代码
- 手把手教你在Windows10环境下安装深度学习框架(pytorch or tensorflow)
- web性能測試工具-沒還有實驗-URL收集_无需整理
- 关掉微软Word的一些不必要的插件,提高启动速度
- html控制图的宽,用JointJS做一个简单的功能控制图
- golang atomic load 性能_设计模式之Golang单例模式
- 测试页打印失败.是否参阅打印疑难解答以获得帮助_使用DeepState对API进行模糊测试(上)...
- Spring Cloud 微服务实战系列-Ribbon入门RestTemplate 介绍
- 在应用中集成科大讯飞的语音识别技术
- CMU 15-213 Introduction to Computer Systems学习笔记(7) Machine-Level Programming-Data
- android 生成 kml代码,android 导入KML文件
- 一步一步带你实现自定义圆形进度条(详解)
- php生成盖章图片,印章图案生成器
- Windows10下下载安装ideaIU
- 计算机图形学的网络课程
- 【测绘程序设计】坐标反算神器V1.0(附C/C#/VB源程序)
- 复旦计算机夏令营英语口语,保研夏令营时间|复旦大学计算机学院2020保研夏令营...
- 计算机科学出国,假如你是李华,打算出国学习计算机科学.现在给在美国工作的David Zhang写信,了解以下情况.1.需参加的英语考试;...
热门文章
- 数组去重方法集锦(一)
- 网络编程基础 - m
- matlab中的下标都是从1开始
- .CS文件编译生成.DLL文件 .EXE文件
- python re re.compile search groupdict 正则多取值
- Linux系统 安装飞桨PaddleHub+LAC实现词法分析 实现加载自定义词典分词 (解决Lac服务启动报错问题、解决自定义词典空格无法分词问题)
- 时空穿梭 探寻高端存储架构的前世今生
- 2019互联网年会图鉴:去年煮酒论英雄,今年却似新亭会
- C程序设计语言读书笔记:入门C语言
- webpack打包之后的文件过大的解决方法