IO流第33天(IO流体系,FileInputStream,FileOutputStream使用)

IO流体系图

InputStream:字节输入流

  • Inputstream是抽象类的所有类字节输入流超类
  • InputStream常用子类
  1. FileInputStream:文件输入流
  2. BufferedInputStream:缓冲字节输入流
  3. ObjectInputStream:对象字节输入流
    鼠标右键==>Diagrams(或者Ctrl+Alt+Shift+u)查看类关系
    image
    Ctrl+Alt+B查看下面的类
    image

FileInputStream

使用read()一次读取一个字节

@Test
    public void readFile01(){
        String filePath="d:\\hello.txt";
        int readDate=0;
        FileInputStream fileInputStream =null;
        try {
            //创建FileInputStream对象,用于读取文件
             fileInputStream = new FileInputStream(filePath);
            //从该输入流读取一个字节的数据,如果没有输入可用,此方法将阻止
            //如果返回-1,表示读取完毕
            while ((readDate= fileInputStream.read())!=-1){//判断是否读取完毕
                System.out.print((char)readDate);//转成一个char显示
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关闭文件流,释放资源
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

使用read(byte[]b)来读取文件,提高效率

@Test
    public void readFile02(){
        String filePath="d:\\hello.txt";
        //定义字节数组,一次读8取个字节
        byte[]b=new byte[8];
        int readLength=0;
        FileInputStream fileInputStream =null;
        try {
            //创建FileInputStream对象,用于读取文件
            fileInputStream = new FileInputStream(filePath);
            //从该输入流读取最多b.length字节的数据到字节数组.此方法将阻塞,直到某些输入可用
            //如果返回-1,表示读取完毕
            //如果读取正常,返回实际读取的字节数
            while ((readLength= fileInputStream.read(b))!=-1){//判断是否读取完毕
                System.out.print(new String(b,0,readLength));//转成一个char显示
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关闭文件流,释放资源
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

FileOutputStream

image
案例说明:使用FileOutputStream在a.txt文件中写入"hello,world",如果文件不存在,则创建文件(注意:前提是目录存在)

@Test
    public void writeFile(){
        //创建FileOutputStream对象
        String filePath="d:\\a.txt";
         FileOutputStream fileOutputStream=null;
        try {
            //得到FileOutputStream对象
            //注意:new FileOutputStream(filePath)创建方式,当写入内容时,会覆盖原有的内容
            //注意:new FileOutputStream(filepath,true)创建,当写入内容时,不会覆盖原有内容,而是追加
            fileOutputStream= new FileOutputStream(filePath,true);
//            //1.写入一个字节
//            fileOutputStream.write('a');
//            //2.写入字符串
//            String str="hello,world";
//            //str.getBytes()方法可以将  字符串  转换成  字节数组
//            fileOutputStream.write(str.getBytes());
            //3.方法三,写入指定数量的字符串长度,超过大小将会报错下标越界,并且情况文件
            String str="gjl,world2";
            fileOutputStream.write(str.getBytes(),0,str.length());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

注意:

  1. 使用write()方法,当文件存在时直接写入,当文件不存在时会自动创建文件并写入
  2. 创建FileOutputStream时,默认写入内容为覆盖,加上参数true后,写入内容将会变成追加
    new FileOutputStream(filePath)创建方式,当写入内容时,会覆盖原有的内容
    new FileOutputStream(filepath,true)创建,当写入内容时,不会覆盖原有内容,而是追加
  3. 写入内容可以写入单个字符,也可以写入字符串,还可以写入从某个到某个位置的字符串
  4. 写入覆盖状态下,写入超出字符串长度时将报错并清空文件内容.追加状态下文件无变化
posted @   不再犹豫27  阅读(23)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示