IO流概念:数据以流动的形式,先入先出的方式进行传输。流动的方向从数据源 -->数据–>目的地
流的分类:
一、节点流
1、字节流
任意类型的数据都能读写
- 字节输入流
InputStream抽象类是表示字节输入流的所有类的超类
其中比较重要的ByteArrayInputStream, FileInputStream
方法
read()
从输入流中读取数据的下一个字节
FileInputStream is=new FileInputStream("D:/haha.txt");
//read() 读取一个字节数据 返回读取到的字节数|没读到的-1
int num=is.read();
read(byte[] b)
从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。
read(byte[] b, int off, int len)
将输入流中最多 len 个数据字节读入 byte 数组。
FileInputStream is = new FileInputStream("D:/haha.txt");
byte[] car=new byte[1024];
int len=-1; //读入到字节数组中字节的个数
//以此卡车装不完的数据重复读取
while((len=is.read(car))!=-1){
System.out.println(Arrays.toString(car));
System.out.println(new String(car,0,len));
}
例子如下
- 字节输出流
OutputStream此抽象类是表示输出字节流的所有类的超类。
其中比较重要的ByteArrayOutputStream, FileOutputStream
方法
write(byte[] b)
将 b.length 个字节从指定的 byte 数组写入此输出流
write(byte[] b, int off, int len)
将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流
flush()
刷新此输出流并强制写出所有缓冲的输出字节
OutputStream os=new FileOutputStream("D:/lalala.txt",true);
byte[] str="不吃饭修仙哈哈哈".getBytes();
os.write(str);
os.write(str,0,3);
os.flush()
2、字符流
只度写纯文本
字符输入流CharArrayReader, FileReader
字符输出流CharArrayWriter, FileWriter
二、功能流
功能流作用于节点流
1、缓冲流
增加功能,提高性能,提高读写效率
字节缓冲输入流BufferedInputStream
方法和字节流一样
字节缓冲输出流BufferedOutputStream
方法和字节流一样
字符缓冲输入流BufferedReader
新增方法
readLine()
读取一个文本行。所以要想使用不能用多态
字符缓冲输出流BufferedWriter
新增方法
newLine()
写入一行分隔符
{
// TODO Auto-generated method stub
//创建源选择流
BufferedReader br = new BufferedReader(new FileReader("D:/aa.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("D:/ee.txt"));
String len = null;
//读入
while ((len = br.readLine())!=null) {//读取一个文本行
//写出
bw.write(len);
bw.newLine();//写入一行分隔符
}
//刷出
bw.flush();
//关闭
bw.close();
br.close();
}
2、转换流
字节转字符
InputStreamReade
r是字节流通向字符流的桥梁
BufferedReader rd=new BufferedReader(new InputStreamReader(new BufferedInputStream(new FileInputStream("D:/haha.txt"))));
OutputStreamWriter
是字符流通向字节流的桥梁
BufferedWriter rt=new BufferedWriter(new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream("D:/bbb.txt"))));
3、Data流
基本数据类型|String+数据
DataInputStream可以读取基本 Java 数据类型和String
新增方法
DataOutputStream将基本 Java 数据类型和字符串类型写入输出流中
新增方法
例子如下
//写入
public static void read(String src) throws IOException{
//1.输入流
DataInputStream in=new DataInputStream(new FileInputStream(src));
//2.读入 读入和写出的顺序要保持一致
boolean b=in.readBoolean();
int i=in.readInt();
String s=in.readUTF();
System.out.println(b+"-->"+i+"-->"+s);
//3.关闭
in.close();
}
//写出
public static void write(String dest) throws IOException{
//1.输出流
DataOutputStream out=new DataOutputStream(new FileOutputStream(dest));
//2.准备数据
boolean flag=false;
int i=100;
String s="哈哈";
//3.写出
out.writeBoolean(flag);
out.writeInt(i);
out.writeUTF(s);
//4.刷出
out.flush();
//5.关闭
out.close();
}
4、对象流
任意类型,包括对象+数据
ObjectInputStream序列化输出流
ObjectOutputStream反序列化输入流
对象如果向通过数据流输入或者输出,此类需要实现实例化接口Serializable
。
如果一些属性不想序列化可以用transient
修饰,序列化其他的规则:
- 静态的内容不能序列化。
- 如果父类有实现序列化,子类没有,子类中所有的内容都能序列化。
- 如果父类中没有实现序列化,子类有实现序列化,子类只能序列化自己的内容。
例:如下
序列化
//序列化输出
public static void read() throws FileNotFoundException, IOException, ClassNotFoundException{
//1.流
ObjectInputStream is=new ObjectInputStream(new FileInputStream("D:/object.txt"));
//2.读入写出读入的顺序保持一致
Object p=is.readObject();
System.out.println(p);
if(p instanceof Person){
Person p1=(Person)p;
System.out.println(p1);
}
System.out.println(Arrays.toString((int[])is.readObject()));
//3.关闭
is.close();
}
反序列化
//序列化输出
public static void write() throws FileNotFoundException, IOException{
//1.流
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("D:/object.txt"));
//2.写出
Person p=new Person("lisi",20);
int[] arr=new int[]{1,2,3,4,5};
//写出读入的顺序保持一致
out.writeObject(p);
out.writeObject(arr);
//3.刷出
out.flush();
//4.关闭
out.close();
p.setAge(100);
p.setName("王五");
}
}
类实现序列化接口
class Person implements Serializable{
private String name;
// private transient int age;
private static int age;