1.缓冲流
提高执行效率,,让操作更加灵活
1.字节缓冲输入流BufferedInputStream的使用
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
//读取一个文件
public class Test {
public static void main(String[] args) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("source.txt"));
byte[] bs = new byte[30];
int num = 0;
while((num = bis.read(bs))!=-1) {
String str = new String(bs,0,num);
System.out.println(str);
}
if (bis!=null) {
bis.close();
}
}
}
2.BufferedOutputStream缓冲字符输出流的使用
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test2 {
public static void main(String[] args) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("b.txt"));
byte[] bs = {
97,98,99,100,101,102};
bos.write(bs,0,bs.length);
bos.flush();
if (bos!=null) {
bos.close();
}
}
}
3.BufferedReader字节输入流的使用
readLine() 读取一行数据
package com.zx.demo2;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Test3 {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("a.txt"));
// 正常读取方式
char[] cs = new char[10];
int len = 0;
while((len = reader.read(cs))!=-1){
String str = new String(cs,0,len);
System.out.println(str);
}
String str = null;
while((str = reader.readLine())!=null){
System.out.println(str);
}
if(reader!=null){
reader.close();
}
}
}
4.BufferedWriter字节输出流的使用
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class Test4 {
public static void main(String[] args) throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter("c.txt"));
writer.write("abc");
writer.newLine();
writer.write(new char[]{
'a','b','c'},0,3);
writer.write("离离原上草,");
writer.newLine();
writer.write("离离原上草.");
writer.newLine();
writer.write("离离原上草,");
writer.newLine();
writer.write("离离原上草.");
writer.flush();
if(writer!=null){
writer.close();
}
}
}
5.缓冲流复制文件
public static void copy3() throws IOException{
BufferedReader reader = new BufferedReader(new FileReader("source.txt"));
BufferedWriter writer = new BufferedWriter(new FileWriter("d.txt"));
String str = null;
while((str=reader.readLine())!=null){
writer.write(str);
writer.newLine();
}
writer.flush();
if(reader!=null){
reader.close();
}
if(writer!=null){
writer.close();
}
}
6.序列化版本号
private static final long serialVersionUID = 1L;
保持版本一致,在修改前序列化的对象, 在修改后可以进行照常反序列化
transient 暂态 new出来的对象是可以使用此属性值,但是不能序列化到文件
import java.io.Serializable;
public class Student implements Serializable{
private static final long serialVersionUID = 1L;
//transient 暂态 new出来的对象是可以使用此属性值,但是不能序列化到文件
String name;
transient int age;
public Student() {
super();
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
7.对象流
ObjectInputStream,ObjectOutputStream
正常的对象只能在程序运行期间可以存在, 但是jvm停止的时候,就消失了
想要把对象持久化的存储起来
中转站:序列化 Serializable
对象–>字节 序列化 前提: 类进行实现 Serializable
字节–>对象 反序列化
读取数据的顺序是按照写入的顺序读取的
流 直接操作的是 字节或者字符
如果要存储多个对象 尽量装到集合中, writeObject一次
如果存储多个对象,读取的时候循环读取, 只能依赖 抛出EOFException 这个异常来标志文件结尾
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Student stu = new Student("张三",18);
Student stu2 = new Student("李四",19);
//将对象--->字节 序列化
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("w.txt"));
oos.writeObject(stu);
oos.writeObject(stu2);
oos.flush();
//读取出来 将字节--->对象 反序列化
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("w.txt"));
//Object stu3 = ois.readObject();
// 存的时候 按照顺序存, 读的时候 是按照存入的顺序读
//EOFException 对象流 没有-1 null这种标志的, 当读到文件结尾,再继续去读取的时候,会以抛异常的方式提示你文件到结尾了
try {
while(true) {
Object obj = ois.readObject();
System.out.println(obj);
}
}catch (EOFException e) {
System.out.println("文件已达结尾");
}
//更加推荐的写法是 一个文件中只写一个对象
List<Student> list = new ArrayList<>();
list.add(new Student("张三",18));
list.add(new Student("李四",18));
list.add(new Student("王五",18));
list.add(new Student("赵六",18));
//将一个集合通过对象流存入到了文件 序列化
ObjectOutputStream oos2 = new ObjectOutputStream(new FileOutputStream("z.txt"));
oos2.writeObject(list);
oos2.flush();
//将集合通过对象流从文件中读取出来 反序列化
ObjectInputStream ois2 = new ObjectInputStream(new FileInputStream("z.txt"));
List<Student> list2 = (List<Student>) ois2.readObject();
for (Student student : list2) {
System.out.println(student);
}
}
}
8.properties
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
public class Test {
public static void main(String[] args) {
Properties properties = new Properties();
properties.setProperty("1", "aaa");
properties.setProperty("2", "bbb");
properties.setProperty("3", "ccc");
String value1 = properties.getProperty("1");
String value2 = properties.getProperty("2");
String value3 = properties.getProperty("3");
//getProperty键不存在的时候可以设定默认的value值
String value4 = properties.getProperty("5","ddd");
//遍历properties的一种方式
Set<Entry<Object, Object>> set = properties.entrySet();
for(Entry<Object, Object> entry : set) {
System.out.println(entry.getKey()+"="+entry.getValue());
}
//遍历properties的第二种方式
Set<Object> set2 = properties.keySet();
for (Object key : set2) {
Object value = properties.get(key);
System.out.println(key+"="+value);
}
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class Test2 {
public static void main(String[] args) throws FileNotFoundException, IOException {
Properties properties = new Properties();
// 将db.properties中的信息读取到 properties
properties.load(new FileInputStream("db.properties"));
String driver = properties.getProperty("driver");
String url = properties.getProperty("url");
String username = properties.getProperty("username");
String password = properties.getProperty("password");
// 下一步就是注册jdbc
Properties properties2 = new Properties();
properties2.setProperty("money", "1000000000000");
properties2.setProperty("girlfriend", "java");
properties2.setProperty("girlfriend2", "riyu");
properties2.setProperty("girlfriend3", "go");
properties2.setProperty("girlfriend4", "clenrabush");
properties2.store(new FileOutputStream("do-dream.txt"), "注释 you are dream");
}
}
9.转换流
字节流和字符流之间进行转换,也可以解决因为编码格式不同造成的乱码问题
转换流: 实质上是字符流 父类是Reader Writer
ascii unicode编码表
编码格式
高级语言 —> 机器语言 再给计算机执行 编码
机器语言 —> 高级语言 再给人去看 解码
UTF-8 编码格式 规则 汉字 100
GBK 编码格式 规则 sa 100
InputStreamReader 字节–>字符
OutputStreamWriter 字符–>字节
二进制 对应适当的规则 进行转换成不同的编码格式
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
public class Test {
public static void main(String[] args) throws IOException {
/*BufferedReader reader = new BufferedReader(new FileReader("zz.txt"));
String str = reader.readLine();
System.out.println(str);*/
InputStreamReader isr = new InputStreamReader(new FileInputStream("zz.txt"), "UTF-8");
char[] cs = new char[20];
int num = isr.read(cs);
String str2 = new String(cs,0,num);
System.out.println(str2);
}
}