上次简单介绍了一下关于文件读写的操作,今天把I/O流补全。
**
缓冲流
**
与文件流不同的是,文件流的读入读出是一个字节或字符来进行的,效率低且速度慢。
缓冲流提供了一个缓冲区(数组),一次性搬运数据,效率高速度快
下面是缓冲流的继承关系:
BufferedOutputStream继承了OutputStream
BufferedInputStream继承了InputStream
BufferedWriter继承了Writer
BufferedReader继承了Reader
关于缓冲流的使用与文件流非常相似,我把步骤写在了下面的代码区:
BufferedOutputStream与BufferedInputStream
File file=new File("/Users/ahlahmlahlah/Desktop/test5.txt");
//使用BufferedOutputStream步骤
//第一步,创建一个FileOutputStream对象,参数是数据的目的地
FileOutputStream fos=new FileOutputStream(file);
//第二步,创建一个BufferedOutputStream对象
BufferedOutputStream bos=new BufferedOutputStream (fos);//也可以指定缓冲区大小,再加上一个参数
//第三步,调用BufferedOutputStream的write方法
bos.write("把数据写进缓冲区中".getBytes());
//第四步,使用flush方法,把缓冲区数据刷新入文件
bos.flush();
//第五步,关闭流
bos.close();
//使用BufferedOutputStream
FileInputStream fis=new FileInputStream(file);
BufferedInputStream bis=new BufferedInputStream(fis);
byte[] bytes=new byte[1024];
int len=0;
while((len=bis.read(bytes))!=-1)
{
System.out.println(new String(bytes));
}
bis.close();
结果
控制台:
把数据写进缓冲区中
BufferdWriter与BufferdReader
//BufferedWriter
File file2=new File("/Users/ahlahmlahlah/Desktop/test6.txt");
FileWriter fw=new FileWriter(file2);
BufferedWriter bw=new BufferedWriter(fw);
bw.write("BufferedWriter的一个对象写进来的数据");
bw.flush();
bw.close();
//BufferedReader
FileReader fr=new FileReader(file2);
BufferedReader br=new BufferedReader(fr);
System.out.println(br.readLine());
System.out.println(br.readLine());//读到空的行就返回一个null,,并且readLine()不会把换行符读进来,需要newLine()方法换行。
br.close();
结果:
控制台
BufferedWriter的一个对象写进来的数据
null
使用缓冲流要注意的就是构造方法中要传递的参数是文件流的对象,而不是文件对象
**
对象的序列化与反序列化
**
简单来说序列化就是:
把对象转换为字节序列的过程
反序列化是:
把字节对象恢复为对象的过程
被序列化的类必须继承java.io.Serializable接口
下面我们看一下序列化的实例(把Person类写道硬盘中再读取出来)
/*
ObjectOutputStream是OutputStream的一个子类,作用是把对象以流的方式写入到文件来保存,称为对象的序列化
ObjectInputStream是InputStream的一个子类,作用是把文件中对象以流的方式读入,称为对象的反序列化
*/
public class Object_Stream {
public static void main(String[] args) throws Exception {
File file=new File("/Users/ahlahmlahlah/Desktop/test9.txt");
//ObjectOutputStream
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(new Person("靓仔",20));
oos.close();
//ObjectInputStream
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
Object o=ois.readObject();
ois.close();
System.out.println(o);
}
}
Person类
public class Person implements Serializable {
private final long serialVersionUID=1L;//自己添加的序列号
private String name;
private int age;
Person()
{
}
Person(String name,int age)
{
this.name=name;
this.age=age;
}
@Override
public String toString()
{
return "姓名:"+name+",年龄:"+age;
}
}
在看结果之前,解释一下serialVersionUID=1L;的意义:
编译器会根据Person.java生成一个Person.class。因为Person实现了Serializable接口,所以会给Person.class生成一个序列号,但若果类发生了修改那么我们反序列化的时候,会产生一个异常,为了解决我们自己加加上一个序列号,这样无论怎么修改序列号都不会改变。
结果
姓名:靓仔,年龄:20
最后,关于序列化介绍一个关键字的使用:transient
transient关键字修饰的成员变量无法被序列化
我们把成员变量age加上transient关键字:
private transient int age;
看一下输出结果
姓名:靓仔,年龄:0
age变量此时不可以被序列化(我也没有赋值)所以只可以读出0。
**
转换流
**
我们在使用流的时候,常常会产生乱码问题,这是因为Java默认UTF-8编码表,若我们使用其他的编码表,比如GBK编码表,就会产生乱码(解码失败)现象。而OutputStreamWriter与InputStreamReader分别是FileWriter与FileReader的父类,可以自定义使用哪一个编码表编码表,解决乱码问题。
简单来说,OutputStreamWriter作用是编码,把可以看不懂的编成看懂的;InputStreamReader作用是解码,把看懂的编成看不懂的
下面是转换流的使用:
public static void main(String[] args) throws IOException {
File file=new File("/Users/ahlahmlahlah/Desktop/test7.txt");
//InputStreamWriter使用步骤
OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream(file),"utf-8");//第二个参数就是指定的编码表,默认就是utf-8格式
osw.write("你好");
osw.flush();
osw.close();
File file2=new File("/Users/ahlahmlahlah/Desktop/test8.txt");
OutputStreamWriter osw2=new OutputStreamWriter(new FileOutputStream(file2),"GBK");//指定GBK编码表,一个汉字用两个字节表示
osw2.write("你好");
osw2.flush();
osw2.close();
//InputStreamReader的使用步骤
InputStreamReader isr=new InputStreamReader(new FileInputStream(file),"utf-8");//file路径下编码格式是utf-8,所以解码也是utf-8
int len=0;
while((len=isr.read())!=-1)
{
System.out.print((char)len);
}
isr.close();
System.out.println();
InputStreamReader isr2=new InputStreamReader(new FileInputStream(file2),"GBK");//file2路径下编码格式是GBK,所以解码也是GBK
int len2=0;
while((len2=isr2.read())!=-1)
{
System.out.print((char)len2);
}
isr2.close();
}
结果:
注意看两个文件的大小,test7使用了utf-8编码,一个字符是三个字节,test8使用了GBK编码,一个字符是两个字节。所以test7是6字节,test8是4字节。
控制台结果
你好
你好
**
Properties
**
Properties继承了Hashtable,是一个双列集合,Properties特殊之处在于它可以保存在流中加载。
下面是 Properties的使用:
public static void main(String[] args) throws IOException {
Properties prop=new Properties();
prop.setProperty("迪丽热巴", "18");
prop.setProperty("杨幂", "19");
prop.setProperty("苗苗", "20");//对标put方法,但是两个必须都是字符串
File file =new File("/Users/ahlahmlahlah/Desktop/test4.txt");
FileWriter fw=new FileWriter(file);
FileReader fr=new FileReader(file);
prop.store(fw, "");//把集合中的数据写入文件,第一个参数传递FileOutputStream也可以
fw.close();
Properties prop2=new Properties();
prop2.load(fr);//把文件中的数据读到集合中
Set<String> set=prop2.stringPropertyNames();//对标setKey方法
for(String key:set)
{
String value=prop2.getProperty(key);//对标get
System.out.println(key+"="+value);
}
fr.close();
}
结果:
控制台输出:
苗苗=20
迪丽热巴=18
杨幂=19
这次就先写这么多,博主来广东玩了,最近几天晚上应该都会更新,88