zoukankan      html  css  js  c++  java
  • BufferedOutputStream缓冲流

    1.字节缓冲输出流

    /*
    使用步骤:
    1.创建FileOutputStream对象,构造方法中绑定要输出的目的地
    2.创建FileOutputStream对象,构造方法中传递FileOutputStream对象,提高FileOutputStream对象效率
    3.使用FileOutputStream对象中的方法write,把数据写到内部的缓冲区中
    4.使用FileOutputStream对象中的flush,把内部缓冲区的数据,刷新到文件中
    5.释放资源(会先调用flush方法刷新数据,4可省略)
    
    
    */
    
    import java.io.BufferedOutputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class Demo01BufferedOutputStream {
        public static void main(String[] args) throws IOException {
            FileOutputStream fos=new FileOutputStream("F:\basic\untitled13\src\it\cast\day15\demo04\a.txt");
            BufferedOutputStream bos=new BufferedOutputStream(fos);
            bos.write("我把数据写入到内部缓冲区中".getBytes());
            bos.flush();
            bos.close();
        }
    }

    2.字节缓冲输入流

    //字节缓冲输入流
    
    import java.io.BufferedInputStream;
    import java.io.FileInputStream;
    import java.io.IOException;
    /*使用步骤:
    1.创建BufferedInputStream对象,构造方法中绑定要读取的数据源
    2.创建BufferedInputStream对象,构造方法中传递BufferedInputStream对象,提高读取效率
    3.BufferedInputStream对象read,读取文件
    4.释放资源*/
    public class Demo02BufferedInputStream {
        public static void main(String[] args) throws IOException {
            FileInputStream fis=new FileInputStream("F:\basic\untitled13\src\it\cast\day15\demo04\a.txt");
            BufferedInputStream bis=new BufferedInputStream(fis);
         /*   int len=0;
            while ((len=bis.read())!=-1){
                System.out.println(len);
            }*/
            byte[] bytes=new byte[1024];
            int len=0;
            while ((len=bis.read(bytes))!=-1){
                System.out.println(new String(bytes,0,len));
            }
            bis.close();
        }
    }
  • 相关阅读:
    angular基础
    函数&闭包
    springboot + 拦截器 + 注解 实现自定义权限验证
    idea点击RUN启动报错: Broken configuration due to unavailable plugin or invalid configuration dat
    解决“指定的服务已经标记为删除”问题
    Mybatis中的XML中需要用到的转义符号整理
    springboot 2.0+ 自定义拦截器
    idea中lombok的使用
    springboot集成PageHelper,支持springboot2.0以上版本
    IDEA 修改JSP和后端数据后,页面刷新可以实时更新
  • 原文地址:https://www.cnblogs.com/cy2268540857/p/13795720.html
Copyright © 2011-2022 走看看