【16种重要流操作】

2023-10-30


前言

        输入输出流的使用已经非常的广泛,除过采用了不同的类操作,其方法大同小异。下文针对16种重要的流进行分解,模型化。


一、16种重要流是什么?

二、输入输出流模型化

1.输入流操作

1.创建流对象,加载文件;
 FileInputStream inputStream = new FileInputStream("D:\\备份\\test.txt");
2.按照一定的字节进行读取,返回的是读取的字节数
int readCount = inputStream.read(byte[]);
3.可以转换成具体的字符串
new String(byte, 0, readCount);
4.在finally中关闭流
代码实例:
  public static void fileInputStream() {
        //创建文件输入流对象
        //D:\备份\test.txt" idea中路径会自动将\编译成\\
        //路径也可以写成D:/备份/test.txt
        String path ="D:\\备份\\test.txt";
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(path);
            /*一次读取多个字节  */
            byte[] b = new byte[40];
            //读取到的字节数量(不是字节本身),读取不到的时候,返回-1
            int readCount = 0;
            while ((readCount = inputStream.read(b)) != -1) {
                //读多少个字节就转多少个字节为字符串
                System.out.print(new String(b, 0, readCount));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    //关闭流
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

2.输出流操作

1.创建流对象,加载文件;

 FileOutputStream outputStream = outputStream = new FileOutputStream(path);
2.按照一定的字节进行写入
outputStream.write(byte2);
3.在finally中刷新、关闭流
//刷新
outputStream.flush();
//关闭流
outputStream.close();

代码实例:
   public static void outputStreamDemo() {
        //定义流
        FileOutputStream outputStream = null;
        try {
            //要写入的文件路径
            String path ="D:\\备份\\test.txt";
            //这种会清空文件中原有的数据,在进行写入操作
//            outputStream = new FileOutputStream("D:\\备份\\test.txt");
            //true表示在文件已有字符的末尾添加
            outputStream = new FileOutputStream(path, true);
            byte[] bytes = {'r', 'g', 'i'};
            //写入
            outputStream.write(bytes);
            String str = "我的名字叫张三";
            //字符串转字节数组
            byte[] byte2 = str.getBytes();
            //写入
            outputStream.write(byte2);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    //刷新
                    outputStream.flush();
                    //关闭流
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

三、16中输入输出流,代码通

  • FileInputerStream          //文件的字节输入流 字节流,从磁盘到内存
 public static void fileInputStream2() {

        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream("D:\\备份\\test.txt");
            int available = inputStream.available();//剩下多少个字节没有读。这里获取的是文件的总字节数
            inputStream.skip(3);//跳过3个字节
            byte[] b = new byte[available]; //不适合太大文件,byte[]数组不能太大
            //不用进行循环直接读取
            int readCount = inputStream.read(b);
            System.out.print(new String(b, 0, readCount));


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    //关闭流
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
  • FileOutputStream 输出流(从内存到硬盘)
/**
     * 字节流-输出流--从内存到硬盘
     */
    public static void fileOutputStream() {


        FileOutputStream outputStream = null;
        try {
            //这种会清空文件中原有的数据,在进行写入操作
//            outputStream = new FileOutputStream("D:\\备份\\test.txt");
            //在文件已有字符的末尾添加
            outputStream = new FileOutputStream("D:\\备份\\test.txt",true);
            byte[] bytes = {'r','g','i'};
           //写入
            outputStream.write(bytes);
            String str ="我的名字叫张三";
            //字符串转字节数组
            byte[] byte2= str.getBytes();
            //写入
            outputStream.write(byte2);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    //刷新
                    outputStream.flush();
                    //关闭流
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
  • FileReader  文件字符输入流
/*
文件字符输入流
字符流读取文件,只限制普通的文本文件
*/
public static void fileReader() {
    FileReader fileReader = null;
    try {
        //创建字符流
         fileReader = new FileReader("D:\\备份\\test.txt");
        char[] chars = new char[6]; //按照字符去读,一次读取6个字符
        int readCount = 0;
        while ((readCount = fileReader.read(chars)) != -1) {
            System.out.println(new String(chars, 0, readCount));
        }


    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        if (fileReader != null) {
            try {
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
  • fileWriter 文件输出流
/**
* @Description:字符输出流
* @Author: yz
* @Version: 1.0.0
* @Date: 2022/1/19
*/
public static void fileWriter(){
    FileWriter fileWriter = null;
    try {
         fileWriter =new FileWriter("D:\\备份\\test.txt");
        fileWriter.write("战胜疫情!");
        fileWriter.write("\n");
        char[] chars = {'1','2','撒'};
        fileWriter.write(chars);
        fileWriter.write(chars,1,2);
     
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        try {
               fileWriter.flush();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • BufferedReader:带缓冲的字符输入流
/**
* 带缓冲的字符输入流
*/
public static void buffered() {
    BufferedReader buffRed = null;
    try {
        //FileInputStream输入流
        FileInputStream ios = new FileInputStream("D:\\备份\\test3.txt");
        //InputStreamReader转换流
        InputStreamReader red = new InputStreamReader(ios);
        // 带缓冲的字符输入流
        buffRed = new BufferedReader(red);
        String line = null;
        while ((line = buffRed.readLine()) != null) {
            System.out.println(line);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (buffRed != null) {
        try {
            buffRed.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

总结

        主要对输入输出流进行代码分析,如果有用,可以点赞收藏。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【16种重要流操作】 的相关文章

随机推荐

  • Dockerfile 中 ENTRYPOINT 的使用

    先占个坑 以免忘记 有空了来填
  • 2、Java基础-NIO、IO、ThreadLocal类

    1 IO和NIO 1 1 传统IO和NIO区别在哪 Java NIO和IO之间最大的区别在 IO是面向流的 而NIO是面向缓存区buffer的 Java NIO是非阻塞式的 意味着使用一个线程向某通道发送一个请求读取数据 如果buffer中
  • Squid日志分析与访问控制详解

    squid日志分析与访问控制 squid的日志系统能够帮助我们查看访问者的记录 包括来访者Internet的站点信息 时间占用信息 排名 连接次数和访问量 是一个很完善的日志系统 squid常用日志分为如下两个 分别是access log
  • 笔试题14:用TCP通信模型创建一个Web服务器(源码)

    我们都知道 IIS Apache和tomcat等Web服务器可以用来创建Web站点 负责接受客户端浏览器的HTTP请求 那么 他们是如何实现的呢 其实基本原理是采用TCP通信模型 下面给出一个采用Java的TCP编程API创建的简易Web服
  • deque容器和list容器学习

    1 deque简介 deque容器同样是一种顺序容器 你可以了解你的元素的存储位置 你可以安排你的元素的存储位置 和vector相比 deque可以实现用常数的时间在容器头部插入元素 同样deque也没有容量的概念 这是因为deque可以动
  • AUTO sharding policy will apply DATA sharding policy as it failed to apply FILE sharding policy

    使用tf distribute MirroredStrategy 时 出现警告 AUTO sharding policy will apply DATA sharding policy as it failed to apply FILE
  • MySQL 8.0.23中复制架构从节点自动故障转移

    接触MGR有一段时间了 MySQL 8 0 23的到来 基于MySQL Group Replicaion MGR 的高可用架构又提供了新的架构思路 灾备机房的slave 如何更好的支持主机房的MGR MGR 到底可以坏几个节点 这次我就以上
  • fastgithub

    之前我在网上搜过解决办法 其中一个是修改 hosts 文件 但是效果不太理想 我在这里给大家推荐github上的一个开源项目 FastGithub 用了这个之后 效果就比较理想了 次次都能访问到 源码地址 GitHub dotnetcore
  • 简易虚拟培训系统-UI控件的应用2

    目录 Text组件 文字显示 Text组件 文字动态显示 ScrollView组件 使用文件流动态读取硬盘文件 本篇介绍Text和ScrollView的简单应用 以及读取硬盘中 txt文本的内容 Text组件 文字显示 1 加入Text 在
  • 小程序导入npm包 注意事项

    官方api https developers weixin qq com miniprogram dev devtools npm html 1 需要在小程序文件夹 npm init 2 安装需要的npm包 先安装需要的npm 包在构建np
  • 解释器与编译器区别

    让我们看看编译器和解释器之间的主要区别 1 编译器将一个程序作为一个整体进行翻译 而解释器则一条一条地翻译一个程序 2 在编译器的情况下生成中间代码或目标代码 而解释器不创建中间代码 3 编译器比解释器要快得多 因为编译器一次完成整个程序
  • Java基础面试题附带答案(八)

    106字节流和字符流的区别 1 字节流读取的时候 读到一个字节就返回一个字节 字符流读取的时候会读到一个或多个字节 这个要根据字符流中编码设置 一般中文对应的字节数是两个 在UTF 8码表中是3个字节 2 字节流可以处理所有类型数据 如 图
  • Unity如何开发微信小游戏

    微信小游戏现在非常的火 很多开发Unity的同学 都想自己开发微信小游戏 无奈tiny还不成熟 导致很多同学有自己创业做微信小游戏的想法 但是由于技术 却放弃了这样的机会 今天我给大家讲述unity同学如何转型做微信小游戏 1 解放思想 我
  • angular使用websocket和Rxjs

    文章目录 前言 一 Websocket是什么 二 在服务端搭建websocket服务 三 使用rxjs中的websocketsubject 1 使用websocket 2 搭建服务 3 如何拦截到socket关闭 4 如何断线重连 5 如何
  • 运行时动态库:not found 及介绍-linux的-Wl,-rpath命令

    一 运行时动态库 not found 今天在使用linux编写c c 程序时 需要用到第三方的动态库文件 刚开始编译完后 运行提示找不到动态库文件 我就使用了ldd命令查看了一下 发现是有一个库文件显示 not found 如下图所示 库文
  • 法兰克焊接机器人编程入门_Fanuc焊接机器人编程实例

    L 11 89 IF R 2 2 JMP LBL 12 90 IF R 2 3 JMP LBL 13 91 IF R 2 4 JMP LBL 14 92 IF R 2 5 JMP LBL 15 93 IF R 2 6 JMP LBL 16
  • CommandNotFoundError: Your shell has not been properly configured to use ‘conda activate‘.To initi

    看起来你的shell没有正确配置使用conda activate命令 这可能是因为你在安装Anaconda时没有选择将其添加到系统路径中 解决这个问题的方法是运行conda init
  • Power BI第三方图表

    KPI类 Bullet Chart 子弹图 用来展现目标完成率 可定义红 黄 绿区域 Bullet Chart by OKViz 以垂直或水平形式展现目标达成情况 同时可以显示多个指标 Card with States by OKViz 一
  • 钉钉开放平台查询宜搭表单实例数据

    本例结合钉钉开放平台相关api实现获取宜搭表单数据 可在faas中做实现 本例采用python编写 已在本地实现 对于免登获取数据 数据归档到本地能提供参考 faas具体实现请根据实际 参考使用 采用alibabacloud dingtal
  • 【16种重要流操作】

    文章目录 前言 一 16种重要流是什么 二 输入输出流模型化 1 输入流操作 2 输出流操作 三 16种输入输出流 代码通 总结 前言 输入输出流的使用已经非常的广泛 除过采用了不同的类操作 其方法大同小异 下文针对16种重要的流进行分解