目录
1.17.1缓冲流
缓冲流(高效流),是对4个基本的FileXxx
流的增强,所以也是4个流,按照数据类型分类
名称 | 类 |
---|---|
字节缓冲流 | BufferedInputStream BufferedOutputStream |
字符缓冲流 | BufferedReader BufferedWriter |
缓冲流的基本原理:创建流对象时,会创建一个内置的默认大小的缓冲区数组,通过缓冲区读写,减少系统IO次数,从而提高读写的效率。
1.17.1.1字节缓冲流
1.17.1.1.1构造方法
方法名 | 说明 |
---|---|
public BufferedInputStream(InputStream in) | 创建一个 新的缓冲输入流。 |
public BufferedOutputStream(OutputStream out) | 创建一个新的缓冲输出流。 |
1.17.1.1.2读取数据
单个字节读取
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
// 创建流对象
bis = new BufferedInputStream(new FileInputStream("file"));
bos = new BufferedOutputStream(new FileOutputStream("flie"));
// 读写数据
int b;
while ((b = bis.read()) != -1) {
bos.write(b);
}
字节数组读取
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
// 创建流对象
bis = new BufferedInputStream(new FileInputStream("file"));
bos = new BufferedOutputStream(new FileOutputStream("file"));
// 读写数据
int len;
byte[] bytes = new byte[1024];
while ((len = bis.read(bytes)) != -1) {
bos.write(bytes, 0 , len);
}
1.17.1.2字符缓冲流
1.17.1.2.1构造方法
方法名 | 说明 |
---|---|
public BufferedReader(Reader in) | 创建一个 新的缓冲输入流。 |
public BufferedWriter(Writer out) | 创建一个新的缓冲输出流。 |
1.17.1.2.2其他方法
类名 | 方法名 | 说明 |
---|---|---|
BufferedReader | public String readLine() | 一次读取一行文字 |
BufferedWriter | public void newLine() | 写一行行分隔符,由系统属性定义符号 |
// 创建流对象
BufferedReader br = new BufferedReader(new FileReader("file"));
// 定义字符串,保存读取的一行文字
String line = null;
// 循环读取,读取到最后返回null
while ((line = br.readLine())!=null) {
System.out.print(line);
}
// 释放资源
br.close();
1.17.2转换流
1.17.2.1字符编码
- 字符编码
Character Encoding
: 就是一套自然语言的字符与二进制数之间的对应规则。
编码表:生活中文字和计算机中二进制的对应规则
1.17.2.2字符集
-
字符集
Charset
:也叫编码表。是一个系统支持的所有字符的集合,包括各国家文字、标点符号、图形符号、数字等。
计算机要准确的存储和识别各种字符集符号,需要进行字符编码,一套字符集必然至少有一套字符编码。常见字符集有ASCII字符集、GBK字符集、Unicode字符集等。
ASCII字符集:ASCII编码
GBK字符集:GBK编码
Unicode字符集:UTF8编码、 UTF16 编码、UTF32编码
1.17.2.3InputStreamReader类
转换流java.io.InputStreamReader
,是Reader的子类,是从字节流到字符流的桥梁。它读取字节,并使用指定的字符集将其解码为字符。
1.17.2.3.1方法
方法名 | 说明 |
---|---|
InputStreamReader(InputStream in) | 创建一个使用默认字符集的字符流。 |
InputStreamReader(InputStream in, String charsetName) | 创建一个指定字符集的字符流。 |
InputStreamReader input = new InputStreamReader(new FileInputStream("file"));
InputStreamReader input2 = new InputStreamReader(new FileInputStream("file") , "GBK");
1.17.2.4 OutputStreamWriter类
转换流java.io.OutputStreamWriter
,是Writer的子类,是从字符流到字节流的桥梁。使用指定的字符集将字符编码为字节。它的字符集可以由名称指定,也可以接受平台的默认字符集
1.17.2.4.1方法
方法名 | 说明 |
---|---|
OutputStreamWriter(OutputStream in) | 创建一个使用默认字符集的字符输出流。 |
OutputStreamWriter(OutputStream in, String charsetName) | 创建一个指定字符集的字符输出流。 |
1.17.3序列化流
1.17.3.1什么是序列化
在Java中有些字符是不能直接传输的,需要编码才能传输,这个编码的过程就是序列化。
Java 提供了一种对象序列化的机制。用一个字节序列可以表示一个对象,该字节序列包含该对象的数据
、对象的类型
和对象中存储的属性
等信息。字节序列写出到文件之后,相当于文件中持久保存了一个对象的信息。
反之,该字节序列还可以从文件中读取回来,重构对象,对它进行反序列化。对象的数据
、对象的类型
和对象中存储的数据
信息,都可以用来在内存中创建对象。
1.17.3.2ObjectOutputStream类
java.io.ObjectOutputStream
类,将Java对象的原始数据类型写出到文件,实现对象的持久存储。
1.17.3.2.1方法
方法名 | 说明 |
---|---|
public ObjectOutputStream(OutputStream out) | 创建一个指定OutputStream的ObjectOutputStream。 |
public final void writeObject (Object obj) | 将指定的对象写出。 |
1.17.3.2.2序列化
一个对象要想序列化,必须满足两个条件:
-
该类必须实现
java.io.Serializable
接口,Serializable
是一个标记接口,不实现此接口的类将不会使任何状态序列化或反序列化,会抛出NotSerializableException
。 -
该类的所有属性必须是可序列化的。如果有一个属性不需要可序列化的,则该属性必须注明是瞬态的,使用
transient
关键字修饰。
示例
先定义一个类
public class Employee implements java.io.Serializable {
public String name;
public String address;
public transient int age; // transient瞬态修饰成员,不会被序列化
}
------------------------------------------------------------
写出对象
public class SerializeDemo{
public static void main(String [] args) {
Employee e = new Employee();
e.name = "李四";
e.address = "上海";
e.age = 20;
try {
// 创建序列化流对象
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("employee.txt"));
// 写出对象
out.writeObject(e);
// 释放资源
out.close();
fileOut.close();
System.out.println("saved"); // 姓名,地址被序列化,年龄没有被序列化。
} catch(IOException i) {
i.printStackTrace();
}
}
}
1.17.3.3ObjectInputStream类
ObjectInputStream反序列化流,将之前使用ObjectOutputStream序列化的原始数据恢复为对象。
1.17.3.3.1方法
方法名 | 说明 |
---|---|
public ObjectOutputStream(OutputStream out) | 创建一个指定OutputStream的ObjectOutputStream。 |
public final Object readObject () | 读取一个对象。 |
1.17.3.3.2反序列化
public class Deserialize {
public static void main(String [] args) {
Employee e = null;
try {
// 创建反序列化流
FileInputStream fileinput = new FileInputStream("file.txt");
ObjectInputStream obin = new ObjectInputStream(fileinput);
// 调用方法读取一个对象
e = (Employee) obin.readObject();
// 释放资源
obin.close();
fileinput.close();
}catch(IOException i) {
// 捕获其他异常
i.printStackTrace();
return;
}catch(ClassNotFoundException c) {
// 捕获类找不到异常
System.out.println("Employee class not found");
c.printStackTrace();
return;
}
System.out.println("Name: " + e.name);
System.out.println("Address: " + e.address);
System.out.println("age: " + e.age);
}
}
1.17.4打印流
控制台打印输出调用print
方法和println
方法完成的,这两个方法都来自于java.io.PrintStream
类,该类能够方便地打印各种数据类型的值。
打印流:只负责输出数据,不负责读取数据,并且不会抛出IOException。
1.17.4.1分类
名称 | 类名 |
---|---|
字节打印流 | PrintStream |
字符打印流 | PrintWriter |
1.17.4.2字符打印流
1.17.4.2.1特点
- 自动换行
- 必须是通过配置 自动刷新 (println,printf,format)
- boolean autoFlush: true 自动刷新 false,不自动刷新
- 将字节输出流转换字符输出流
1.17.4.2.2方法
方法名 | 说明 |
---|---|
PrintWriter(String fileName) | 使用指定的文件名创建一个新的PrintWriter,而不需要自动执行刷新 |
PrintWriter(Writer out, boolean autoFlush) | 创建一个新的PrintWriter out:字符输出流 autoFlush: 一个布尔值,如果为真,则println , printf ,或format方法将刷新输出缓冲区 |
void write(String s) | 使用指定的文件名创建一个新的PrintWriter,而不需要自动执行刷新 |
void print(String s) | 输出字符串, 没有换行 |
void println(String s) | 输出字符串并换行. 如果启动了自动刷新, 则会执行自动刷新写入数据 |
上一篇:1.16Java-IO流、字节流、字符流 |
下一篇:1.18Java-线程、锁、线程间通信 |