25IO第4天Properties序列化流打印流CommonsIO讲义.docx
《25IO第4天Properties序列化流打印流CommonsIO讲义.docx》由会员分享,可在线阅读,更多相关《25IO第4天Properties序列化流打印流CommonsIO讲义.docx(18页珍藏版)》请在冰豆网上搜索。
25IO第4天Properties序列化流打印流CommonsIO讲义
第17天IO流
今日内容介绍
◆Properties集合
◆序列化流与反序列化流
◆打印流
◆commons-IO
第1章Properties类
1.1Properties类介绍
Properties类表示了一个持久的属性集。
Properties可保存在流中或从流中加载。
属性列表中每个键及其对应值都是一个字符串。
特点:
1、Hashtable的子类,map集合中的方法都可以用。
2、该集合没有泛型。
键值都是字符串。
3、它是一个可以持久化的属性集。
键值可以存储到集合中,也可以存储到持久化的设备(硬盘、U盘、光盘)上。
键值的来源也可以是持久化的设备。
4、有和流技术相结合的方法。
●load(InputStream)把指定流所对应的文件中的数据,读取出来,保存到Propertie集合中
●load(Reader)
●store(OutputStream,commonts)把集合中的数据,保存到指定的流所对应的文件中,参数commonts代表对描述信息
●stroe(Writer,comments);
代码演示:
/*
*
*Properties集合,它是唯一一个能与IO流交互的集合
*
*需求:
向Properties集合中添加元素,并遍历
*
*方法:
*publicObjectsetProperty(Stringkey,Stringvalue)调用Hashtable的方法put。
*publicSetstringPropertyNames()返回此属性列表中的键集,
*publicStringgetProperty(Stringkey)用指定的键在此属性列表中搜索属性
*/
publicclassPropertiesDemo01{
publicstaticvoidmain(String[]args){
//创建集合对象
Propertiesprop=newProperties();
//添加元素到集合
//prop.put(key,value);
prop.setProperty("周迅","张学友");
prop.setProperty("李小璐","贾乃亮");
prop.setProperty("杨幂","刘恺威");
//System.out.println(prop);//测试的使用
//遍历集合
Setkeys=prop.stringPropertyNames();
for(Stringkey:
keys){
//通过键找值
//prop.get(key)
Stringvalue=prop.getProperty(key);
System.out.println(key+"=="+value);
}
}
}
1.2将集合中内容存储到文件
需求:
使用Properties集合,完成把集合内容存储到IO流所对应文件中的操作
分析:
1,创建Properties集合
2,添加元素到集合
3,创建流
4,把集合中的数据存储到流所对应的文件中
stroe(Writer,comments)
store(OutputStream,commonts)
把集合中的数据,保存到指定的流所对应的文件中,参数commonts代表对描述信息
5,关闭流
代码演示:
publicclassPropertiesDemo02{
publicstaticvoidmain(String[]args)throwsIOException{
//1,创建Properties集合
Propertiesprop=newProperties();
//2,添加元素到集合
prop.setProperty("周迅","张学友");
prop.setProperty("李小璐","贾乃亮");
prop.setProperty("杨幂","刘恺威");
//3,创建流
FileWriterout=newFileWriter("prop.properties");
//4,把集合中的数据存储到流所对应的文件中
prop.store(out,"savedata");
//5,关闭流
out.close();
}
}
1.3读取文件中的数据,并保存到集合
需求:
从属性集文件prop.properties中取出数据,保存到集合中
分析:
1,创建集合
2,创建流对象
3,把流所对应文件中的数据读取到集合中
load(InputStream)把指定流所对应的文件中的数据,读取出来,保存到Propertie集合中
load(Reader)
4,关闭流
5,显示集合中的数据
代码演示:
publicclassPropertiesDemo03{
publicstaticvoidmain(String[]args)throwsIOException{
//1,创建集合
Propertiesprop=newProperties();
//2,创建流对象
FileInputStreamin=newFileInputStream("prop.properties");
//FileReaderin=newFileReader("prop.properties");
//3,把流所对应文件中的数据读取到集合中
prop.load(in);
//4,关闭流
in.close();
//5,显示集合中的数据
System.out.println(prop);
}
}
注意:
使用字符流FileReader就可以完成文件中的中文读取操作了
第2章序列化流与反序列化流
用于从流中读取对象的
操作流ObjectInputStream称为反序列化流
用于向流中写入对象的操作流ObjectOutputStream称为序列化流
●特点:
用于操作对象。
可以将对象写入到文件中,也可以从文件中读取对象。
2.1对象序列化流ObjectOutputStream
ObjectOutputStream将Java对象的基本数据类型和图形写入OutputStream。
可以使用ObjectInputStream读取(重构)对象。
通过在流中使用文件可以实现对象的持久存储。
注意:
只能将支持java.io.Serializable接口的对象写入流中
●代码演示:
publicclassObjectStreamDemo{
publicstaticvoidmain(String[]args)throwsIOException,ClassNotFoundException{
/*
*将一个对象存储到持久化(硬盘)的设备上。
*/
writeObj();//对象的序列化。
}
publicstaticvoidwriteObj()throwsIOException{
//1,明确存储对象的文件。
FileOutputStreamfos=newFileOutputStream("tempfile\\obj.object");
//2,给操作文件对象加入写入对象功能。
ObjectOutputStreamoos=newObjectOutputStream(fos);
//3,调用了写入对象的方法。
oos.writeObject(newPerson("wangcai",20));
//关闭资源。
oos.close();
}
}
●Person类
publicclassPersonimplementsSerializable{
privateStringname;
privateintage;
publicPerson(){
super();
}
publicPerson(Stringname,intage){
super();
this.name=name;
this.age=age;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicintgetAge(){
returnage;
}
publicvoidsetAge(intage){
this.age=age;
}
@Override
publicStringtoString(){
return"Person[name="+name+",age="+age+"]";
}
}
2.2对象反序列化流ObjectInputStream
ObjectInputStream对以前使用ObjectOutputStream写入的基本数据和对象进行反序列化。
支持java.io.Serializable接口的对象才能从流读取。
●代码演示
publicclassObjectStreamDemo{
publicstaticvoidmain(String[]args)throwsIOException,ClassNotFoundException{
readObj();//对象的反序列化。
}
publicstaticvoidreadObj()throwsIOException,ClassNotFoundException{
//1,定义流对象关联存储了对象文件。
FileInputStreamfis=newFileInputStream("tempfile\\obj.object");
//2,建立用于读取对象的功能对象。
ObjectInputStreamois=newObjectInputStream(fis);
Personobj=(Person)ois.readObject();
System.out.println(obj.toString());
}
}
2.3序列化接口
当一个对象要能被序列化,这个对象所属的类必须实现Serializable接口。
否则会发生异常NotSerializableException异常。
同时当反序列化对象时,如果对象所属的class文件在序列化之后进行的修改,那么进行反序列化也会发生异常InvalidClassException。
发生这个异常的原因如下:
●该类的序列版本号与从流中读取的类描述符的版本号不匹配
●该类包含未知数据类型
●该类没有可访问的无参数构造方法
Serializable标记接口。
该接口给需要序列化的类,提供了一个序列版本号。
serialVersionUID.该版本号的目的在于验证序列化的对象和对应类是否版本匹配。
●代码修改如下,修改后再次写入对象,读取对象测试
publicclassPersonimplementsSerializable{
//给类显示声明一个序列版本号。
privatestaticfinallongserialVersionUID=1L;
privateStringname;
privateintage;
publicPerson(){
super();
}
publicPerson(Stringname,intage){
super();
this.name=name;
this.age=age;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicintgetAge(){
returnage;
}
publicvoidsetAge(intage){
this.age=age;
}
@Override
publicStringtoString(){
return"Person[name="+name+",age="+age+"]";
}
}
2.4瞬态关键字transient
当一个类的对象需要被序列化时,某些属性不需要被序列化,这时不需要序列化的属性可以使用关键字transient修饰。
只要被transient修饰了,序列化时这个属性就不会琲序列化了。
同时静态修饰也不会被序列化,因为序列化是把对象数据进行持久化存储,而静态的属于类加载时的数据,不会被序列化。
●代码修改如下,修改后再次写入对象,读取对象测试
publicclassPersonimplementsSerializable{
/*
*给类显示声明一个序列版本号。
*/
privatestaticfinallongserialVersionUID=1L;
privatestaticStringname;
privatetransient/*瞬态*/intage;
publicPerson(){
super();
}
publicPerson(Stringname,intage){
super();
this.name=name;
this.age=age;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicintgetAge(){
returnage;
}
publicvoidsetAge(intage){
this.age=age;
}
@Override
publicStringtoString(){
return"Person[name="+name+",age="+age+"]";
}
}
第3章打印流
3.1打印流的概述
打印流添加输出数据的功能,使它们能够方便地打印各种数据值表示形式.
打印流根据流的分类:
●字节打印流PrintStream
●字符打印流PrintWriter
●方法:
voidprint(Stringstr):
输出任意类型的数据,
voidprintln(Stringstr):
输出任意类型的数据,自动写入换行操作
●代码演示:
/*
*需求:
把指定的数据,写入到printFile.txt文件中
*
*分析:
*1,创建流
*2,写数据
*3,关闭流
*/
publicclassPrintWriterDemo{
publicstaticvoidmain(String[]args)throwsIOException{
//创建流
//PrintWriterout=newPrintWriter(newFileWriter("printFile.txt"));
PrintWriterout=newPrintWriter("printFile.txt");
//2,写数据
for(inti=0;i<5;i++){
out.println("helloWorld");
}
//3,关闭流
out.close();
}
}
3.2打印流完成数据自动刷新
可以通过构造方法,完成文件数据的自动刷新功能
●构造方法:
●开启文件自动刷新写入功能
publicPrintWriter(OutputStreamout,booleanautoFlush)
publicPrintWriter(Writerout,booleanautoFlush)
●代码演示:
/*
*分析:
*1,创建流
*2,写数据
*/
publicclassPrintWriterDemo2{
publicstaticvoidmain(String[]args)throwsIOException{
//创建流
PrintWriterout=newPrintWriter(newFileWriter("printFile.txt"),true);
//2,写数据
for(inti=0;i<5;i++){
out.println("helloWorld");
}
//3,关闭流
out.close();
}
}
第4章commons-IO
4.1导入classpath
加入classpath的第三方jar包内的class文件才能在项目中使用
创建lib文件夹
将commons-io.jar拷贝到lib文件夹
右键点击commons-io.jar,BuildPath→AddtoBuildPath
4.2FilenameUtils
这个工具类是用来处理文件名(译者注:
包含文件路径)的,他可以轻松解决不同操作系统文件名称规范不同的问题
●常用方法:
getExtension(Stringpath):
获取文件的扩展名;
getName():
获取文件名;
isExtension(StringfileName,Stringext):
判断fileName是否是ext后缀名;
4.3FileUtils
提供文件操作(移动文件,读取文件,检查文件是否存在等等)的方法。
●常用方法:
readFileToString(Filefile):
读取文件内容,并返回一个String;
writeStringToFile(Filefile,Stringcontent):
将内容content写入到file中;
copyDirectoryToDirectory(FilesrcDir,FiledestDir);文件夹复制
copyFile(FilesrcFile,FiledestFile);文件夹复制
●代码演示:
/*
*完成文件的复制
*/
publicclassCommonsIODemo01{
publicstaticvoidmain(String[]args)throwsIOException{
//method1("D:
\\test.avi","D:
\\copy.avi");
//通过Commons-IO完成了文件复制的功能
FileUtils.copyFile(newFile("D:
\\test.avi"),newFile("D:
\\copy.avi"));
}
//文件的复制
privatestaticvoidmethod1(Stringsrc,Stringdest)throwsIOException{
//1,指定数据源
BufferedInputStreamin=newBufferedInputStream(newFileInputStream(src));
//2,指定目的地
BufferedOutputStreamout=newBufferedOutputStream(newFileOutputStream(dest));
//3,读
byte[]buffer=newbyte[1024];
intlen=-1;
while((len=in.read(buffer))!
=-1){
//4,写
out.write(buffer,0,len);
}
//5,关闭流
in.close();
out.close();
}
}
/*
*完成文件、文件夹的复制
*/
publicclassCommonsIODemo02{
publicstaticvoidmain(String[]args)throwsIOException{
//通过Commons-IO完成了文件复制的功能
FileUtils.copyFile(newFile("D:
\\test.avi"),newFile("D:
\\copy.avi"));
//通过Commons-IO完成了文件夹复制的功能
//D:
\基础班复制到C:
\\abc文件夹下
FileUtils.copyDirectoryToDirectory(newFile("D:
\\基础班"),newFile("C:
\\abc"));
}
}
第5章总结
5.1IO流总结
●字节流
⏹字节输入流InputStream
◆FileInputStream操作文件的字节输入流
◆BufferedInputStream高效的字节输入流
◆ObjectInputStream反序列化流
⏹字节输出流OutputStram
◆FileOutputStream操作文件的字节输出流
◆BufferedOutputStream高效的字节输出流
◆ObjectOuputStream序列化流
◆PrintStream字节打印流
●字符流
⏹字符输入流Reader
◆FileReader操作文件的字符输入流
◆BufferedReader高效的字符输入流
◆InputStreamReader输入操作的转换流(把字节流封装成字符流)
⏹字符输出流Writer
◆FileWriter操作文件的字符输出流
◆BufferedWriter高效的字符输出流
◆OutputStreamWriter输出操作的转换流(把字节流封装成字符流)
◆PrintWriter字符打印流
●方法:
⏹读数据方法:
◆read()一次读一个字节或字符的方法
◆read(byte[]char[])一次读一个数组数据的方法