Java之IO流技术

                           ----IO------

1. 分类

输入流:
        字节输入流:InputStream-----》FileInputStream---------------》BufferInputStream
输出流:
        字节输出流:OutputStream-----》FileOutputStream---------------》BufferOutputStream
内存流:
        字节数组:ByteArrayInputStream和ByteArrayOutputStream
        字符流:CharArrayReader和CharWriter
        字符串流:StringReader和StringWriter
打印流:只有输出流:PrintWriter
标准输入输出流:System.in和System.out
随机流:RandomAccessFile
合并流:SequenceInputStream
序列化流:ObjectInoputStream
反序列化流:ObjectOutputStream

2. 举例

读文件1:------------------------------------------------------------》try {
            FileInputStream inputStream = new FileInputStream("test.txt");
            int by=-1;
        while((by=inputStream.read())!=-1){
            System.out.print((char)by);
        }           
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
读文件2(缓存):--------------------------------------------------------》FileInputStream inputStream = new FileInputStream("test.txt");
        FileOutputStream outputStream = new FileOutputStream("copy.txt");
        int by=-1;
        while((by=inputStream.read())!=-1){
            outputStream.write(by);
        }
        inputStream.close();
        outputStream.close();
读文件3(缓存):----------------------------------------------------》FileInputStream inputStream = new FileInputStream("test.txt");
        byte[]buffer=new byte[1024];
        FileOutputStream outputStream = new FileOutputStream("copy1.txt");
        int by=-1;
        while((by=inputStream.read(buffer))!=-1){
            outputStream.write(buffer,0,by);
        }
读文件4(缓存):-------------------------------------------------》BufferedOutputStream fox = new BufferedOutputStream(new FileOutputStream("fox.txt"));
        fox.write("1234567".getBytes());
        fox.close();

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("fox.txt"));
        int by=-1;
        while((by=bis.read())!=-1){
            System.out.print((char)by);
        }
写文件:----------------------------------------------------------------》String content = "hello,IO";
        byte[] buffer = content.getBytes();
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream("hello.txt",true);
            for (int i = 0; i < 10; i++) {
                outputStream.write(("hello,IO" + i).getBytes());
                outputStream.write("\r\n".getBytes());              
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
        try {
            outputStream.close();           
        } catch (IOException e) {
            e.printStackTrace();
        }       
        }   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值