04.20

public class Test1 {
    @Test
    public void test01(){
        System.out.println("============");
    }
    @Test
    public void test02() throws Exception{
        FileReader fr = new FileReader("D:/AAA/bbb.txt");

        FileWriter fw = new FileWriter("C:/AAA/a.txt");
        int c=0;
        char[] cs=new char[10];
        while( (c=fr.read(cs)) !=-1){
            fw.write(cs,0,c);
            fw.flush();
        }
        fw.close();
        fr.close();
    }
    //字节输入
    @Test
    public void testOutStream() throws Exception{
        OutputStream os = new FileOutputStream("D:/AAA/f.txt");
        String str = "abcd";
        byte[] bytes = str.getBytes();
        os.write(bytes);
        os.flush();
        os.close();
    }
    @Test
    public void testInputStream() throws Exception{
        InputStream is = new FileInputStream("D:/AAA/f.txt");
        byte[] bytes = new byte[3];
        int c = is.read(bytes);
        System.out.println(bytes+"============="+c);

        c=is.read(bytes);
        System.out.println(bytes+"=========="+c);
    }
    @Test
    public void testInputStream2() throws Exception{
        InputStream is = new FileInputStream("D:/AAA/f.txt");
        byte[] bytes = new byte[3];
        int c=0;
        while ((c=is.read(bytes)) !=-1){
            String str = new String(bytes,0,c);
            System.out.println(str);
        }
        is.close();
    }
    //图片复制
    @Test
    public void testCopy() throws Exception{
        InputStream is = new FileInputStream("D:/AAA/1.jpg");
        OutputStream fos=new FileOutputStream("C:/AAA/2.jpg");
        byte[] bytes = new byte[10];
        int c = 0;
        while ((c=is.read(bytes)) !=-1){
            fos.write(bytes,0,c);
        }
        is.close();
        fos.close();
    }
    @Test
    public void TestBuffer() throws Exception{
        OutputStream out=new FileOutputStream("D:/AAA/g.txt");
        BufferedOutputStream  bos=new BufferedOutputStream(out);
        String str="abcdefhijglmn";
        byte[] bytes = str.getBytes();
        bos.write(bytes);
        bos.close();
    }
    @Test
    public void testObjectStream() throws Exception{
        OutputStream out = new FileOutputStream("D:/AAA/a.txt");
        ObjectOutputStream oos = new ObjectOutputStream(out);
        //Role r=new Role("吕布","1级",1,);
        //oos.writeObject(r);
        oos.close();
    }
    @Test
    public void testObjectStream2()throws Exception{
        InputStream input=new FileInputStream("D:/AAA/a.txt");
        ObjectInputStream ois=new ObjectInputStream(input);
        Object o = ois.readObject();

        System.out.println(o);
        ois.close();
    }
}

缓存流是在基础流[InputStream OutputStream Reader Writer]之上 添加了一个缓存池功能.
BufferInutStream  BufferOutputStream BufferReader  BufferWriter 提高IO的效率,降低IO的次数。

对象流--对java对象进行io操作

  在操作Io流的时候 都是将字符串读写操作  将java对象在文件中进行读写

                                                                      Student st=new Student();

1. 序列化: 把内存中的java对象存储到磁盘[网盘]的过程。
         ---java对象所属的类必须实现序列化接口.implements Serializable
2. 反序列化: 把磁盘中的内容读取到java对象内存中的过程。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值