Java:缓冲流、转换流、序列化流
1、缓冲流-概述
读、写都可以使用一种“数组”的方式,这种方式会大大提高读、写的效率,基于这种原因,Java内部提供一种“缓冲流”,其内部自带一个缓冲区(数组),目的也是为了提高程序运行速率。
2、缓冲流-字节缓冲流
1)、输出流:BufferedOutputStream
2)、输入流:BuferedInputStream
注意:都没有特有的方法,都是继承于父类的方法
public static void main(String[] args) {try (//1.源文件到程序的输入流:BufferedInputStream bufIn = new BufferedInputStream(new FileInputStream("d:\\douyu.exe"));//2.程序到目标文件的:输出流BufferedOutputStream bufOut = new BufferedOutputStream(new FileOutputStream("e:\\douyu_copy.exe"))) {//一次读写一个字节/*int b = 0;while ((b = bufIn.read()) != -1) {//从"缓存区读",一次读一个字节bufOut.write(b);//输出到"缓存区",一次输出一个字节}*///一次读写一个字节数组byte[] byteArray = new byte[1024];int len = 0;while ((len = bufIn.read(byteArray)) != -1) {//从"缓存区读",一次读1KbufOut.write(byteArray,0,len);//输出到”缓存区",一次输出1k}System.out.println("复制完毕!");} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}
}
3、缓冲流-字符缓冲流
1)、字符缓冲流同“字节缓冲流”作用一样,内部提供了8K的缓冲区;
2)、字符缓冲流的俩个类各自提供了一个特有的方法:
输出流:newLine();//换行
输入流:readLine();//忽略换行
配套使用
public static void main(String[] args) {try (BufferedReader bufIn = new BufferedReader(new FileReader("demo03.txt"));BufferedWriter bufOut = new BufferedWriter(new FileWriter(("demo3_copy2.txt")))) {//一次读写一个字符数组/*char[] chArray = new char[1024];int len = 0;while ((len = bufIn.read(chArray)) != -1) {bufOut.write(chArray,0,len);}*///使用特有方法赋值String row = null;while ((row = bufIn.readLine()) != null) {//一次读取一行bufOut.write(row);bufOut.newLine();//输出一个换行}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}
}
4、缓冲流-练习-文本排序-分析
5、缓冲流-练习-文本排序-实现
public static void main(String[] args) throws IOException {//1.源文件的输入流BufferedReader bufIn = new BufferedReader(new FileReader("demo05.txt"));//2.目标文件的输出流BufferedWriter bufOut = new BufferedWriter(new FileWriter("demo05_copy.txt"));//3.一次读一行,存储到Map中Map<Integer, String> map = new HashMap<>();String row = null;while ((row = bufIn.readLine()) != null) {//row = "3.侍中、 侍郎郭攸之、 费祎..."String id = row.substring(0, row.indexOf("."));//String id = "3"String msg = row.substring(row.indexOf(".") + 1);//String msg = "侍中、 侍郎郭攸之、...."map.put(Integer.valueOf(id), msg);//map.put(3,"侍中、 侍郎郭攸之、....")}//4.向目标文件写入for (int i = 1; i <= map.size(); i++) {bufOut.write(i + "." + map.get(i));bufOut.newLine();}//5.关闭文件bufOut.close();bufIn.close();System.out.println("处理完毕!");
}
6、转换流-为甚要有字符流
1)、字符流:一次按一个字符读取,每个国家的字符占用的字节数是不同的,(UTF-8,汉字占用3个字节)
2)、各个国家使用的编码表不同,字符占用的字节数也就不同,如果都使用“字节流”读写就会很不方便,所以Java提供了一种专门操作纯文本文件的字符流
3)、可以根据各国的语言编码方式,来自动对字符进行编码、解码
7、转换流-字符编码和字符集
常见字符集(编码表):记录了字符与数字的映射关系。计算机村存储字符时就是存储的对应是数字的二进制数
1)、ASCII码表:最早的码表,记录了128个字符,每个字符使用1个字节表示。
2)、中文码表(GBxxxx)
GB2312:最早的中文码表,记录7000多字符,现在不在使用了
GBK:目前使用的码表,记录2w多汉字,每个汉字使用2个字节表示
UTF-8码表:国际码表。有中文;字符采用1,2,3,4字节表示。每个中文使用:3个字节表示;
…
8、转换流-编码引出的问题
乱码–>解码与编码不一致:
例:文本中是GBK编码,而读取时IDEA使用的是UTF-8编码
public static void main(String[] args) throws IOException {FileReader in = new FileReader("demo06.txt");//使用UTF-8解码int c = 0;while ((c = in.read()) != -1) {System.out.println("读取的字符:" + (char)c);//乱码}in.close();
}
//原因:文本中是:GBK编码
//读取时,使用UTF-8解码;
9、转换流-转换输出流OutputStreamWriter的使用
1)、FileWriter和FileReader都是使用默认的编码方式
2)、使用OutputSreamWriter可以指定编码方式
public static void main(String[] args) throws IOException {OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("demo07.txt"), "GBK");osw.write("你好");//使用GBK编码:两个汉字:4个字节osw.close();
}
10、转换流-转换输入流InputStreamReader的使用
使用InputStreamReader可以指定解码方式
public static void main(String[] args) throws IOException {InputStreamReader isr = new InputStreamReader(new FileInputStream("demo07.txt"), "GBK");int c = isr.read();//按照GBK解码:一次读取2个字节System.out.println("读取的字符:" + (char)c);isr.close();
}
11、转换流-转换流工作方式图解
12、转换流-练习-转换文件编码
将一个使用GBK编码的文档复制为一个UTF-8编码的文档
public static void main(String[] args) throws IOException {//1.将demo09.txt(GBK-8字节)复制为:demo09_UTF8.txt(12个字节)//2.使用GBK读取InputStreamReader isr = new InputStreamReader(new FileInputStream("demo09.txt"), "GBK");//使用UTF-8输出OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("demo09_UTF8.txt"),"UTF-8");//3.循环读取int c = 0;while ((c = isr.read()) != -1) {osw.write(c);}//4.关闭流osw.close();isr.close();
}
13、序列化-概述
1)、序列化:指将一个对象连同属性值一起存储在一个文件中,或者通过网络传输,这个过程叫做序列化
2)、反序列化:将之前已经序列化的对象,再次读取到程序中,并在内存中创建对象
14、序列化-序列化流ObjectOutputStream类
public class Student implements Serializable{//要想序列化,必须实现Serializable接口,进而获得一些功能(可以被序列化的功能)String name;int age;public Student(String name, int age) {this.name = name;this.age = age;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +'}';}
}
测试类:
public static void main(String[] args) throws IOException {ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("demo10.txt"));Student stu = new Student("张三", 18); oos.writeObject(stu);oos.close();
}
注意:被序列化的对象必须实现:Serializable(接口),
此接口无任何抽象方法,这种接口叫:标记接口,表示为实现它的类开启某些功能
15、序列化-反序列化ObjectInputStream类
public static void main(String[] args) throws IOException, ClassNotFoundException {//反序列化ObjectInputStream ois = new ObjectInputStream(new FileInputStream("demo10.txt"));Object obj = ois.readObject();//在内存产生的是Student对象Student stu = (Student)obj;//向下转型System.out.println(stu);
}
注意:反序列化时,接收的对象必须要是在序列化时的包下的那个对象类,否则运行异常
16、序列化-关于版本号
public class Student implements Serializable{String name;int age;String sex;private static final long serialVersionUID = 2;....
}
注意:反序列化之前会检查现有类的序列号和文件中对象的序列号是否相同,不同将产生异常,建议在开发中自己对序列号进行维护
17、序列化-禁止属性被序列化transient关键字
public class Student implements Serializable{String name;int age;transient String sex;...
}
在属性前加transient,该属性将不会被序列化
18、序列化-练习-序列化集合
public static void main(String[] args) throws IOException, ClassNotFoundException {/*//1.先存储一些Student对象ArrayList<Student> stuList = new ArrayList<>();stuList.add(new Student(1, "特朗普", 16));stuList.add(new Student(2, "金三胖", 18));stuList.add(new Student(3, "普京", 19));//将集合中数据全部序列化到文件中ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("demo12.txt"));oos.writeObject(stuList);oos.close();System.out.println("序列化结束");*/ObjectInputStream ois = new ObjectInputStream(new FileInputStream("demo12.txt"));Object obj = ois.readObject();ois.close();ArrayList<Student> stuList = (ArrayList<Student>)obj;for (Student stu : stuList) {System.out.println(stu);}
}
19、打印流-概述
java中有两种打印流:
1)、字节打印流:PrinteStream
2)、字符打印流:printWrite
打印流的特点:
1)、只有输出流,没有输入流;
2)、可以操作控制台,也可以操作文件;
20、打印流-字节打印流PrintStream类
1)、System.out.println():向控制台输出数据
2)、System.out就是PrintStream类型:默认被指向:控制台
3)、可以将out的流向进行更改,使其流向文件:
public static void main(String[] args) throws FileNotFoundException {PrintStream ps = new PrintStream("demo13.txt");System.setOut(ps);System.out.println("Hello");System.out.println("呵呵");System.out.println("嘻嘻");
}
复习
能够使用字节缓冲流读取数据到程序
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("文件位置")); Byte[] byt = new Byte[1024]; int len = 0; while((len = bis.read(byt)) != -1){System.out.println(byt); }
能够使用字节缓冲流写出数据到文件
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("文件位置")); bos.write("文件内容".getByte());
能够明确字符缓冲流的作用和基本用法
BufferedReader reader = new BufferedReader(new FileInputStream("文件位置")); BufferedWrite write = new BufferWrite(new FileOutputStream("文件位置")); byte[] byt = new byte[1024]; int len = 0; while((len = reader.read(byte))!=-1){writer.write(byte,0,len); } reader.close(); write.close();
能够使用缓冲流的特殊功能
readLine();//读取一行字符 newLine();//换行 //一般配套使用
能够阐述编码表的意义
编码表就是记录有字符与其相对应的数字,计算机存储这些字符时都会将这些字符转换为与其对应的二进制进行存储
能够使用转换流读取指定编码的文本文件
InputStreamReader in = new InputStreamReader(new FileInputStream("文件位置"),"GBK"); OutputStreamWriter out = new OutputStreamWrite(new FileOutputStream("文件位置"),"UTF-8"); int c = 0; while((c = in.read())!=-1){out.write(c); } in.close(); out.close();
能够使用转换流写入指定编码的文本文件
能够说出打印流的特点
只有输出流,没有输入流,可以输出到控制台,也可以输出到文件中;默认为控制台
PrintStream ps = new PrintStream("文件位置"); System.setOut(ps); System.out.println("要写入的内容");
能够使用序列化流写出对象到文件
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("复制目标文件")); Student stu = new Student(1,"张珊",23); oos.writeObject(stu); oos.close();
能够使用反序列化流读取文件到程序中
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("目标文件")); Object obj = ois.readObject(); Student student = (Student)obj; System.out.println(student); ois.close();
Java:缓冲流、转换流、序列化流相关推荐
- 集合到文件 文件到集合 点名器 集合到文件数据排序版 标准输入流 打印流 对象序列化流 对象返序列化流 Properties 游戏次数 进程和线程 线程 多线程的实现方式 设置和获取线程名称
文章目录 集合到文件 文件到集合 点名器 集合到文件数据排序版 标准输入流 打印流 对象序列化流 对象返序列化流 Properties 游戏次数 进程和线程 线程 多线程的实现方式 设置和获取线程名称 ...
- Java基础-22总结登录注册IO版,数据操作流,内存操作流,打印流,标准输入输出流,转换流,随机访问流,合并流,序列化流,Properties...
你需要的是什么,直接评论留言. 获取更多资源加微信公众号"Java帮帮" (是公众号,不是微信好友哦) 还有"Java帮帮"今日头条号,技术文章与新闻,每日更新 ...
- Java基础-22总结登录注册IO版,数据操作流,内存操作流,打印流,标准输入输出流,转换流,随机访问流,合并流,序列化流,Properties
你需要的是什么,直接评论留言. 获取更多资源加微信公众号"Java帮帮" (是公众号,不是微信好友哦) 还有"Java帮帮"今日头条号,技术文章与新闻,每日更新 ...
- File类,字节字符输入输出流,缓冲流,标准流,对象序列化流
一,File文件类 1 File类创建功能: public boolean createNewFiLe():当具有该名称的文件不存在时,创建一个由该抽象路径名命名的新空文件 如果文件 ...
- IO流案例以及特殊操作流/对象对象序列化流/Properties集合
文章目录 IO流案例 复制单级文件夹 复制多级文件夹 IO特殊操作流 标准输入流 字节打印流 字符打印流 对象序列化流 serialVersionUID&transient Propertie ...
- day11-字符流、属性集、缓冲流、转换流、序列化流笔记
学习目标 - 能够使用FileWriter写数据的5个方法- public void write(int c) :写出一个字符.- public void write(char[] cbuf) 写字符 ...
- 【小白学Java】D32》》》IO流 之 序列化流 打印流
[友情链接]➡➡▶IO流 之 File类 & 递归 [友情链接]➡➡▶IO流 之 过滤器 & 字节流 [友情链接]➡➡▶IO流 之字符流 & 属性集(Properties集合) ...
- java io文件流序列化_Java——Properties集合,Object序列化流与反序列化流,打印流,commons-IO文件工具类...
一.properties集合 集合对象Properties类,继承Hashtable,实现Map接口,可以和IO对象结合使用,实现数据的持久存储. 特点: Hashtable的子类,map集合中的方法 ...
- java序列化流_java 序列化流与反序列化流
一 对象序列化流ObjectOutputStream ObjectOutputStream 将Java对象的基本数据类型和图形写入OutputStream.可以使用ObjectInputStream ...
- Java19-day10【标准输入输出流、字节字符打印流、对象序列化-反序列化流、serialVersionUIDtransient、Properties】
视频+资料[链接:https://pan.baidu.com/s/1MdFNUADVSFf-lVw3SJRvtg 提取码:zjxs] Java基础--学习笔记(零起点打开java世界的大门)--博 ...
最新文章
- centos7 python3安装numpy_Centos7安装python3、numpy、scipy、matplotlib、pandas等
- 记一次愚蠢的操作--线程安全问题
- 2015年的计算机二级考试题,2015年计算机二级考试《VFP》精选上机练习题
- 分治法的关键特征_经典算法思想2——分治(Divide-and-Conquer)
- 一个成功的BI项目实施需要注意哪些?
- Swift3.0 在其它类获取Appdelegate单例的属性或则对象的值
- pytorch张量_PyTorch张量-详细概述
- 如何利用vmware workstation安装linux
- Process Explorer 诊断和排错实例(下)
- Protobuf, understand the hood
- for in和for of的区别(转)
- mysql数据库下载及安装教程
- 未来教育全国计算机考试,未来教育.全国计算机等级考试
- 如何在生化实验中选择合适的牛血清白蛋白(BSA)?
- 电商订单批量导入API接口功能
- [SpringBoot](一)基本概念
- 和绅的女人们——汉人、黑人、白人、还有他的“干”女儿
- SSO单点登录-分布式系统实战
- 维沃丫3手机微信无法连接服务器1.104,手机微信网络连接不可用?教你4个方法,轻松解决网络问题!...
- Qt on Mac 开发之 读取苹果系统OSX上的plist资源文件