缓冲流
是对基础流的增强,会多一个缓冲区数组进行读取或者写入
字节缓冲输出流
BufferedOutputStream
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("fw.txt");
//创建一个缓冲区对象 把FileOutputStream对象放进去
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write("写入第一个东西".getBytes());
bos.close();
}
字节缓冲输入流
BufferedInputStream
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("f.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
int len = 0;//记录每次读取到的字节
// while((len=bis.read())!=-1){
// System.out.println(len);
// }
//使用数组读取
byte[] b = new byte[1024];
while((len=bis.read(b))!=-1){
System.out.println(new String(b,0,len));
}
bis.close();
}
字符缓冲输出流
BufferedWrite
public static void main(String[] args) throws IOException {
BufferedWriter bfw = new BufferedWriter(new FileWriter("a1.txt"));
for(int i = 0; i<10; i++){
bfw.write("真的秀");
bfw.newLine();
}
bfw.close();
}
字符缓冲输入流
BufferedRead
特有的成员方法:ReadLine() 读一行数据
public static void main(String[] args) throws IOException {
BufferedReader bfr = new BufferedReader(new FileReader("a1.txt"));
//循环读取 ,但是要注意是读取行数 , 所以读到null就结束
String line;
while((line=bfr.readLine())!=null){
System.out.println(line);
}
bfr.close();
}
对文件内容进行排序
public static void main(String[] args) throws IOException {
//1.创建一个HashMap对象,存储文本序号和每行文本
HashMap<String,String> map = new HashMap<>();
//2.创建字符缓冲输入流
BufferedReader bfr = new BufferedReader(new FileReader("in.txt"));
//3.创建字符缓冲输出流
BufferedWriter bfw = new BufferedWriter(new FileWriter("out.txt"));
//4.使用字符输入流readLine 每行读取文本
String line;
while((line=bfr.readLine())!=null){
//5.进内容进行切割
String[] arr = line.split("\\.");
//6.把切割的序号存储到HashMap
map.put(arr[0],arr[1]);
}
//7.遍历HashMap
for (String key : map.keySet()) {
//8.把键值对 拼接文本行
String value = map.get(key);
line = key + "." + value;
//9.使用字节缓冲输出流
bfw.write(line);
bfw.newLine();
}
//10.释放
bfr.close();
bfw.close();
}
字符编码和字符集
字符编码Character Encoding
: 就是一套自然语言的字符与二进制数之间的对应规则。
编码表:生活中文字和计算机中二进制的对应规则
字符集
字符集 Charset
:也叫编码表。是一个系统支持的所有字符的集合,包括各国家文字、标点符号、图形符号、数字等。
转换流
InputStreamReader : 字节流通向字符流的桥梁,创建读取指定编码表
OutputStreamWriter: 字符流通向字节流的桥梁,创建写入指定编码表
序列化
ObjectOutputStream : 对象的序列化流(把对象以流方式写入到文件中)
构造方法:ObjectOutputStream(OutputStream on)
成员方法:writeObject(Object obj)
当进行序列化和反序列化的时候,会检测类中有没有添加一个标记(有没有实现Serializable)
//Person类
public class Person implements Serializable {
private String name;
private int age;
get/set/toString
}
public static void main(String[] args) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("oos.txt"));
oos.writeObject(new Person("漂亮子",18));
oos.close();
}
反序列化
ObjectInputputStream:对象的反序列化流
构造方法:ObjectOutputStream(InputputStream in)
成员变量:readObject()
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("os.txt"));
Object o = ois.readObject();
ois.close();
System.out.println(o);
}
transient
static关键字:静态关键字
静态优先于非静态加载到内存中(静态优先于对象进入到内存中)
被static修饰的成员变量不能被序列化,序列化都是对象
transient:瞬态关键字
被transient修饰的成员变量不能被序列化
避免序列化冲突
自己定义一个序列化编号
加入序列版本号
private static final long serialVersionUID = 1L;
PrintStream 打印流
- 只负责数据的输出,不负责数据的读取
- 不会出现IOException
- 特有方法 void print(任意类型的值) void println(任意类型的值)