io流学习笔记

字节流

字节输入流字节输出流
InputStream(抽象类)OutputStream (抽象类)
FileInputStreamFileOutputStream
BufferedInputStreamBufferedOutputStream
ObjectInputStreamObjectOutputStream

1、FileInputStream与 FileOutputStream

 @Test
    public void InputAndOutStream(){
        //字节输入输出流,可以设置FileOutputStream中可以设置true为把内容覆盖
        //下面方式采用try-with-catch方式,在括号内的资源会在执行结束后自动释放
        try (
                InputStream in = new FileInputStream("E:\\mubiao\\onre.png");
                OutputStream out = new FileOutputStream("E:\\mubiao\\b.png",true);)
        {
            byte[] b = new byte[1024];
            int len;
            while((len = in.read(b))!=-1){
                out.write(b,0,len);
                out.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

2、BufferedInputStream 与 BufferedOutputStream

@Test
    public void BufferInputAndBufferOutStream(){
        //字节输入输出流,可以设置FileOutputStream中可以设置true为把内容覆盖
        //下面方式采用try-with-catch方式,在括号内的资源会在执行结束后自动释放
        //BufferedInputStream包裹InputStream提高性能
        try (
                InputStream in = new FileInputStream("E:\\mubiao\\onre.jpg");
                OutputStream out = new FileOutputStream("E:\\mubiao\\b.jpg");
                BufferedInputStream bis = new BufferedInputStream(in);
                BufferedOutputStream bos = new BufferedOutputStream(out);
                )
        {
           int len;
           byte[] by = new byte[1024];
           while((len = bis.read(by))!= -1){
               bos.write(by,0,len);
           }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

3、ObjectInputStream 与 ObjectOutputStream

@Test
    public void ObjectInputAndObjectOutStream(){
        Person person01 = new Person("哈哈");
        Person person02 = new Person("喜喜");
        try (
            OutputStream os = new FileOutputStream("E:\\学习文件\\看源码程序\\src\\io流\\Object.txt",true);
            ObjectOutputStream oos = new ObjectOutputStream(os);
            InputStream is = new FileInputStream("E:\\学习文件\\看源码程序\\src\\io流\\Object.txt");
            ObjectInputStream ois = new ObjectInputStream(is);
        )
        {
            oos.writeObject(person01);
            oos.writeObject(person02);
            Object o ;
            while((o = ois.readObject() ) != null){
                System.out.println(o.toString());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
//会出现java.io.StreamCorruptedException: invalid type code: AC,此处未解决,重写ObjectOutputStream的一些方法能解决

字符流

字符输入流字符输出流
Reader(抽象类)Writer(抽象类)
FileReaderFileWriter
BufferedReaderBufferedWriter

1、FileReader和FileWriter

 @Test
    public void WrideAndReader(){
        //字符输入流
        //BufferedReader包裹FileReader可以提高性能
        try(
                FileReader re = new FileReader("E:\\mubiao\\a.txt");
                FileWriter fw = new FileWriter("E:\\mubiao\\b.txt");)
        {
            char[] by = new char[1024];
            int len;
            while((len = re.read(by)) != -1){
                fw.write(by,0,len);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

2、BufferedReader和BufferedWriter

@Test
    public void BufferedWriterAndBufferReader(){
        //BufferedReader包裹FileReader可以提高性能
        try(
                BufferedReader re = new BufferedReader(new FileReader("E:\\mubiao\\a.txt"));
                BufferedWriter fw = new BufferedWriter(new FileWriter("E:\\mubiao\\b.txt"));)
        {
            char[] by = new char[1024];
            while(re.read(by) != -1){
                fw.write(by);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

3、转换流

转换流可以更改字符集读取

  @Test
    public void InputStreamReaderOutputStreamWriter(){
        //字符转换流
        //可以自定义编码
        try(
                Reader re = new InputStreamReader(new FileInputStream("E:\\mubiao\\a.txt") , "utf-8");
                Writer fw = new OutputStreamWriter(new FileOutputStream("E:\\mubiao\\b.txt"),"utf-8"))
        {
            char[] by = new char[1024];
            int len;
            while((len = re.read(by)) != -1){
                fw.write(by,0,len);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

4、打印流

 @Test
    public void PrintStream(){
        //1.打印流PrintStream
        try(
                PrintStream ps01 = new PrintStream(new FileOutputStream("E:\\学习文件\\看源码程序\\src\\io流\\Print.txt",true));
                //直接通向文件
                //PrintStream ps02 = new PrintStream("E:\\学习文件\\看源码程序\\src\\io流\\Print.txt");
        ) {
            //ps02.print("嘻嘻");
            ps01.println("哈哈");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值