Java学习笔记之文件处理下
Java文件处理上
2.5 缓冲流
缓冲流,也叫高效流,是对4个基本流的增强。
缓冲流的原理,是在创建流对象时,会创建一个内置的默认大小的缓冲区数组,通过缓冲区读写,减少系统IO次数,从而提高读写的效率
2.5.1 字节缓冲流 BufferdInputStream BufferdOutputStream
方法:
- public BufferedInputStream(InputStream in):构造方法,创建一个新的缓冲输入流
- public BufferedOutputStream(OutputStream out):构造方法,创建一个新的缓冲输出流
- 可以使用基本流的方法
案例
//缓冲输入流:BufferedInputStream(InputStream in):构造方法,创建一个新的缓冲输入流
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("a.txt"));
//缓冲输出流:BufferedOutputStream(OutputStream out):构造方法,创建一个新的缓冲输出流
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(".txt"));
2.5.2 字符缓冲流 BufferdReader BufferdWriter
方法
- public BufferedReader(Reader in):创建一个新的缓冲输入流
- public BufferedWriter(Writer out):创建一个新的缓冲输出流
- 可以使用基本流的方法
特有方法
- BufferedReader:public String readLine():读一行文字
- BufferedWriter:public void newLine():写一行行分隔符,由系统属性定义符号
案例
//1.创建字符缓冲输入流 BufferedReader(Reader in)
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
//2.创建字符缓冲输出流 BufferedWriter(Writer out)
BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
//3.读写数据
bw.write("缓冲输出");
//3.1 写出换行 newLine()
bw.newLine();
bw.write("流");
//3.2 读取数据
String line = null;
//循环读取,读取到最后返回null
while ((line=br.readLine())!=null){
System.out.println(line);
System.out.println("=============");
/**
* 输出结果:
* 缓冲输出
* 流
*/
}
//释放资源:先开后关,后开先关
bw.close();
br.close();
2.6 转换流
2.6.1 InputStreamReader类
转换流java.io.InputStreamReader,是Reader的子类,是从字节流到字符流的桥梁。它读取字节,并使用指定的字符集将其解码为字符。它的字符可以由名称指定,也可以接受平台的默认字符集
##方法:
- InputStreamReader(InputStream in):创建一个使用默认字符集的字符流
- InputStreamReader(InputStream in,String charsetName):创建一个指定字符集的字符流
案例
//创建流对象,默认UTF-8编码
InputStreamReader is = new InputStreamReader(new FileInputStream("a.txt"));
//创建流对象,指定GBK编码
InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"),"GBK");
//定义变量,保存字符
int read;
//使用默认编码字符流读取,输出乱码
while ((read=is.read())!=-1){
System.out.println((char)read);//��Һ�
}
is.close();
//使用指定编码字符流读取,输出正常
while ((read=isr.read())!=-1){
System.out.println((char)read);//你好
}
isr.close();
2.6.2 OutputStreamWriter类
转换流java.io.OutputStreamWriter,是Writer的子类,是从字符流到字节流的桥梁。使用指定的字符集将字符编码为字节。它的字符集可以有名称指定,也可以接受平台指定的默认字符集
方法
- OutputStreamWriter(OutputStream out):创建一个使用默认字符集的字符流
- OutputStreamWriter(OutputStream out,String charsetName):创建一个指定字符集的字符流
案例
//创建流对象,默认UTF-8编码
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("a.txt"));
//写出数据
out.write("你好");//保存为6个字节
out.close();
//创建流对象,指定GBK编码
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("a.txt"),"GBK");
//写出数据
osw.write("你好");//保存为4个字节
osw.close();
2.6.3 解决文件编码问题
将GBK编码的文本文件,转换为UTF-8编码的文本文件
1.指定GBK编码的转换流,读取文本文件
2.指定UTF-8编码的转换流,写出文本文件
案例实现
//1.创建流对象
//1.1 转换输入流,指定GBK编码
InputStreamReader isr = new InputStreamReader(new FileInputStream("b.txt"),"GBk");
//1.2 转换输出流,默认UTF-8编码
OutputStreamWriter osr = new OutputStreamWriter(new FileOutputStream("c.txt"));
//2.读写数据
//2.1 定义数组
char[] cbuf = new char[1024];
//2.2 定义长度
int len;
//2.3 循环读取
while ((len = isr.read(cbuf))!=-1){
//2.4循环写出
osr.write(cbuf,0,len);
}
//3.释放资源(先开后放,后开先放)
osr.close();
isr.close();
2.7 属性集
2.7.1概述
java.util.Properties继承于Hashtable,来表示一个持久的属性集。它使用键值结构存储数据,每个键及其对应值都是一个字符串。该类也被许多类使用,比如获取系统属性时,System.getProperties方法就是返回一个Properties对象
2.7.2 Properties类
方法:
- public Properties():构造方法,创建一个空的属性列表
- public Object setProperty(String key,String value):保存一对属性
- public String getProperty(String key):使用此属性列表中指定的键搜索属性值
- public Set stringPropertyNames():所有键的名称的集合
案例
//1.Properties():构造方法,创建一个空的属性列表
Properties properties = new Properties();
//2.Object setProperty(String key,String value):保存一对属性
properties.setProperty("username","admin");
properties.setProperty("password","123");
//3.String getProperty(String key):使用此属性列表中指定的键搜索属性值
System.out.println(properties.getProperty("username"));//输出admin
System.out.println(properties.getProperty("password"));//输出123
//4.Set<String> stringPropertyNames():所有键的名称的集合
Set<String> set = properties.stringPropertyNames();
//5.遍历集合,并获取键值
for (String name:set){
System.out.println(name);//获取键的名称
System.out.println(properties.getProperty(name));//获取键对应的值的名称
}
与流相关的方法
- public void load(InputStream inStream):从字节流中读取键值对。参数中使用了字节输入流,通过流对象,可以关联到某文件上,这样就能够加赞文本中的数据了
案例
//1.创建一个空的属性列表
Properties properties = new Properties();
//2.加载文本信息到属性集
properties.load(new FileInputStream("a.txt"));
//3.获取所有键的名称的集合
Set<String> set = properties.stringPropertyNames();
//4.遍历集合,并获取键值
for (String name:set){
System.out.println(properties.getProperty(name));//获取键对应的值的名称
}
2.8 序列化
Java提供了一种对象序列化的机制。用一个字节序列可以表示一个对象,该字节序列包含该对象的数据、对象的类型和对象中存储的属性等信息。字节序列谢楚道文件之后,相当于文件中持久保存了一个对象的信息
反之,该字节序列还可以从文件中读取回来,重构对象,对它进行反序列化。对象的数据、对象的类型和对象中存储的数据信息,都可以用来在内存中创建对象
2.8.1 ObjectOutputStream类
java.io.ObjectOutputStream类,将java对象的原始数据写出到文件,实现对象的持久存储
方法
- public ObjectOutputStream(OutputStream out):创建一个指定OutputStream的ObjectOutputStream
案例
ObjectOutputStram out = new ObjectOutputStrem(new FileOutputStream("a.txt"));
序列化操作
1.一个对象要实现序列化,必须满足两个条件
- 该类必须实现 java.io.Serializable接口,SeriaLizable是一个标记接口,不实现此接口的类将不会是使任何状态序列化或反序列化,会抛出NotSerializableException;
- 该类的所有属性必须是可序列化的,如果一个属性不需要可序列化的,则该属性必须注明是瞬态的,使用transient关键字修饰
class Student implements Serializable {
private String name;
private Integer age;
private transient int nomber;//transient瞬态修饰成员,不会被序列化
}
2.写出对象方法
- public final void writeObject(Object obj):将制定的对象写出
Student student = new Student();
student.setName("小明");
student.setAge(19);
try {
//创建序列化流对象
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("student.txt"));
//写出对象
out.writeObject(student);
//释放资源
out.close();
}catch (Exception e){
e.printStackTrace();
}
2.8.2 ObjectInputStream类
ObjectInputStream反序列化流,将之前使用ObjectOutputStream序列化的原始数据恢复为对象
方法
- public ObjectInputStream(InputStream in):创建一个指定的InputStream的ObjectInputStream
反序列化操作
如果能找到一个对象的class文件,我们可以进行反序列化操作,调用ObjectInputStream读取对象的方法
*public final Object readObject():读取一个对象
Student student = null;
try {
//创建反序列化流对象
ObjectInputStream in = new ObjectInputStream(new FileInputStream("student.txt"));
//读取对象
student = (Student) in.readObject();
//释放资源
in.close();
}catch (Exception e){
e.printStackTrace();
}
//输出
System.out.println("Name:"+student.getName());
System.out.println("Age:"+student.getAge());
今日小结
今天结束了文件处理的相关部分。文件处理还是蛮重要的,我觉着我可能还得继续回顾,不过没关系,现在我掌握了一部分,基础是没问题的,接下来,碰到了再深入深入吧。
此。