今日学习内容总结
- 缓冲流
- 转换流
- 序列化
- 打印流
缓冲流
缓冲流,也叫高效流,是对4个基本的FileXxx
流的增强,所以也是4个流,按照数据类型分类:
-
字节缓冲流:
BufferedInputStream
,BufferedOutputStream
-
字符缓冲流:
BufferedReader
,BufferedWriter
缓冲流的基本原理,是在创建流对象时,会创建一个内置的默认大小的缓冲区数组,通过缓冲区读写,减少系统IO次数,从而提高读写的效率。
字节缓冲流
构造方法
-
public BufferedInputStream(InputStream in)
:创建一个 新的缓冲输入流。 -
public BufferedOutputStream(OutputStream out)
: 创建一个新的缓冲输出流。
BufferedOutputStream使用步骤
- 创建FileOutputStram对象,构造方法中要绑定要输出的目的地
- 创建BufferedOutputStream对象,构造方法中传输FileOutputStream对象,提高FileOutputStream对象效率
- 使用BufferedOutputStream对象中的方法write,把数据写入内部缓冲区中
- 使用BufferedOutputStream对象中的方法flush,把内部缓冲区的数据刷新到文件中
- 释放资源(会自动调用flush方法)
public static void main(String[] args) throws IOException {
FileOutputStream fos =new FileOutputStream("d:\\z.txt",true);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(97);
bos.close();
}
BufferedInputStream使用步骤
- 创建FileInputStream对象,构造方法中要绑定要读取的文件
- 创建BufferedInputStream对象,构造方法中传输FileInputStream对象,提高FileInputStream对象的读取效率
- 使用BufferedInputStream对象中的方法read,读取文件
- 释放资源(会自动调用flush方法)
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("d:\\z.txt");
BufferedInputStream bis =new BufferedInputStream(fis);
int read=0;
while((read=bis.read())!=-1){
System.out.println(read);
}
bis.close();
}
字符缓冲流
构造方法
-
public BufferedReader(Reader in)
:创建一个 新的缓冲输入流。 -
public BufferedWriter(Writer out)
: 创建一个新的缓冲输出流。
特有方法
字符缓冲流的基本方法与普通字符流调用方式一致,不再阐述,我们来看它们具备的特有方法。
- BufferedReader:
public String readLine()
: 读一行文字。 - BufferedWriter:
public void newLine()
: 写一行行分隔符,由系统属性定义符号。
使用步骤与字节流类似
写入数据:
public static void main(String[] args) throws IOException {
BufferedWriter Bw = new BufferedWriter(new FileWriter("d:\\y.txt"));
for (int i = 0; i <10 ; i++) {
Bw.write("TestBufferedWriter");
Bw.newLine();
}
Bw.close();
}
读取数据:
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("d:\\y.txt"));
String line;
while ((line=br.readLine())!= null){
System.out.println(line);
}
br.close();
}
转换流
字符编码和字符集
字符编码
编码:字符(能看懂的)--字节(看不懂的)
解码:字节(看不懂的)-->字符(能看懂的)
-
字符编码
Character Encoding
: 就是一套自然语言的字符与二进制数之间的对应规则。编码表:生活中文字和计算机中二进制的对应规则
字符集
-
字符集
Charset
:也叫编码表。是一个系统支持的所有字符的集合,包括各国家文字、标点符号、图形符号、数字等。
常见字符集有ASCII字符集、GBK字符集、Unicode字符集等。
OutputStreamWriter类
构造方法
-
(OutputStream in)
: 创建一个使用默认字符集的字符流。 -
OutputStreamWriter(OutputStream in, String charsetName)
: 创建一个指定字符集的字符流。
使用步骤:
- 创建OutputStreamWriter对象,构造方法中传递字节输出流和指定的编码表名称
- 使用OutputStreamWriter对象中的方法write,把字符转换为字节存储在缓冲区
- 使用OutputStreamWriter对象中的方法flush,把内部缓冲区的数据刷新到文件中
- 释放资源(会自动调用flush方法)
public static void main(String[] args) throws IOException {
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\GBK.txt"),"GBK");
osw.write("你好");
osw.close();
}
InputStreamReader类
构造方法
-
InputStreamReader(InputStream in)
: 创建一个使用默认字符集的字符流。 -
InputStreamReader(InputStream in, String charsetName)
: 创建一个指定字符集的字符流。
使用步骤:
- 创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称
- 使用InputStreamReader对象中的方法read读取文件
- 释放资源(会自动调用flush方法)
注意:构造方法中指定的编码表名称要跟文件的编码相同,否则会发生乱码
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("d:\\GBK.txt"),"GBK");
int len=0;
while((len=isr.read())!=-1){
System.out.println((char)len);
}
isr.close();
}
将GBK格式转换为UTF-8格式:
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("d:\\GBK.txt"),"GBK");
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\GBK_u8.txt"),"UTF-8");
int len=0;
while((len=isr.read())!=-1){
osw.write(len);
}
isr.close();
osw.close();
}
序列化
3.1 概述
Java 提供了一种对象序列化的机制。用一个字节序列可以表示一个对象,该字节序列包含该对象的数据
、对象的类型
和对象中存储的属性
等信息。字节序列写出到文件之后,相当于文件中持久保存了一个对象的信息。
反之,该字节序列还可以从文件中读取回来,重构对象,对它进行反序列化。对象的数据
、对象的类型
和对象中存储的数据
信息,都可以用来在内存中创建对象。
3.2 ObjectOutputStream类
ObjectOutputStream类,将Java对象的原始数据类型写出到文件,实现对象的持久存储。
构造方法
-
public ObjectOutputStream(OutputStream out)
: 创建一个指定OutputStream的ObjectOutputStream。
特有方法:
-
public final void writeObject (Object obj)
: 将指定的对象写出。
序列化操作
- 一个对象要想序列化,必须满足两个条件:
- 该类必须实现
java.io.Serializable
接口,Serializable
是一个标记接口,不实现此接口的类将不会使任何状态序列化或反序列化,会抛出NotSerializableException
。 - 该类的所有属性必须是可序列化的。如果有一个属性不需要可序列化的,则该属性必须注明是瞬态的,使用
transient
关键字修饰。
public class Person implements Serializable {
private String name;
private int age;
...
}
public static void main(String[] args) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d:\\person.txt"));
oos.writeObject(new Person("zkn",22));
oos.close();
}
ObjectInputStream类
ObjectInputStream反序列化流,将之前使用ObjectOutputStream序列化的原始数据恢复为对象。
构造方法
-
public ObjectInputStream(InputStream in)
: 创建一个指定InputStream的ObjectInputStream。
反序列化操作前提
如果能找到一个对象的class文件,我们可以进行反序列化操作,调用ObjectInputStream
读取对象的方法:
-
public final Object readObject ()
: 读取一个对象。
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("d:\\person.txt"));
Object o = ois.readObject();
ois.close();
System.out.println(o);
}
打印流
java.io.PrintStream
类,该类能够方便地打印各种数据类型的值,是一种便捷的输出方式。
PrintStream类
构造方法
-
public PrintStream(String fileName)
: 使用指定的文件名创建一个新的打印流。 -
public PrintStream(OutputStream out)
: 使用指定的字节输出流创建一个新的打印流。 -
public PrintStream(File file)
: 输出的目的地是一个文件
public class PrintDemo {
public static void main(String[] args) throws IOException {
// 调用系统的打印流,控制台直接输出97
System.out.println(97);
// 创建打印流,指定文件的名称
PrintStream ps = new PrintStream("ps.txt");
// 设置系统的打印流流向,输出到ps.txt
System.setOut(ps);
// 调用系统的打印流,ps.txt中输出97
System.out.println(97);
}
}
网友评论