io

IO流

基本流

字节流

字节输入流构造方法

FileInputStream fis = new FileInputStream("a.txt");

字节输入流读取方法

FileInputStream fis = new FileInputStream("a.txt");
        byte[] buf = new byte[1024];//一次读取1024个字节
        int len; //次数
        while ((len = fis.read(buf))!=-1){
            System.out.println(new String(buf,0,len));
        }

字节输出流构造方法

FileOutputStream fos = new FileOutputStream("a.txt");

字节输出流写出方法

FileOutputStream fos = new FileOutputStream("a.txt");
        fos.write("aaa".getBytes());
        fos.close();

字符流

字符输入流构造方法

FileOutputStream fos = new FileOutputStream("a.txt");

字符输入流读取方法

FileReader fr = new FileReader("a.txt");
        char[] ch = new char[1024];
        int len;
        while ((len = fr.read(ch))!=-1){
            System.out.println(new String(ch,0,len));
        }

字符输出流构造方法

FileWriter fw = new FileWriter("a.txt");

字符输出流写出方法

   FileWriter fw = new FileWriter("a.txt");
        fw.write("aaa");
        fw.flush();//刷新
        fw.close();

缓冲流

字节流

字节输入缓冲流构造方法

FileInputStream fis = new FileInputStream("a.txt");
        BufferedInputStream bis = new BufferedInputStream(fis);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("a.txt"));//匿名对象

字节输入缓冲流读取方法

 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("a.txt"));
        byte[] buf = new byte[1024];
        int len;
        while ((len = bis.read(buf))!=-1){
            System.out.println(new String(buf,0,len));
        }

字节输出缓冲流构造方法

 FileOutputStream fos = new FileOutputStream("a.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("a.txt"));//匿名对象

字节输出缓冲流写出方法

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("a.txt"));
        bos.write("aaa".getBytes());
        bos.close();

字符流

字符输入缓冲流构造方法

FileReader fr = new FileReader("a.txt");
        BufferedReader br = new BufferedReader(fr);
BufferedReader br = new BufferedReader(new FileReader("a.txt"));

字符输入缓冲流读取方法

BufferedReader br = new BufferedReader(new FileReader("a.txt"));
        String line;
        while ((line = br.readLine())!=null){//一次读取一行
            System.out.println(line);
        }

字符输出缓冲流构造方法

FileWriter fw = new FileWriter("a.txt");
        BufferedWriter bw = new BufferedWriter(fw);
BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));

字符输出缓冲流写出方法

BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
        bw.write("aaa");
        bw.flush();//刷新
        bw.close();

转换流

转换流输入方法

InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"));

转换流指定编码读取

InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"), "GBK");

转换流输出方法

OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("a.txt"));

操作流

序列化

序列化的类必须继承Serializable接口 最好自己定义一个序列号这样修改序列化的类就无需再次序列化

序列化类

public class Student implements Serializable {

    private static final long serialVersionUID = 1L;

序列化操作

 Student stu = new Student("张三","男",18);
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("a.txt"));
        oos.writeObject(stu);
        oos.close();

反序列化操作

 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("a.txt"));
        Student stu = (Student) ois.readObject();
        System.out.println(stu.getName()+stu.getSex()+stu.getAge());
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值