zoukankan      html  css  js  c++  java
  • 文件输入输出流

    一,FileInputStream

    public static void fileinputstreamDemo(){
            File file=new File("F:\filetest\file01.txt");
            try {
                FileInputStream in=new FileInputStream(file);
                byte[] bytes=new byte[1024];
                int total = in.read(bytes);
                System.out.println("读出来的数据是:"+new String(bytes,0,total));//读出来的数据是:你好,文件输入流
                in.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    

      步骤:创建输入流--将路径文件中的数据通过流读入数组--关闭流

    二,FileOutputStream

    public static void fileoutputstreamDemo(){
            File file=new File("F:\filetest\file02.txt");
            try {
                FileOutputStream out=new FileOutputStream(file);
                String str="你好,文件输出流";
                byte[] bytes=str.getBytes();
                out.write(bytes);
                out.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    

      步骤:创建输出流--将数组中的文件通过输出流写入磁盘文件--关闭流

    个人猜想(未验证):数据读写速度(外存<内存<缓存),缓存在cpu中,当然最快,字节输入流(fileinputstream),是把数据直接读入到一个数组中,数组能就存在于内存中,而带缓存的输入流(BufferInputStream)则先将数据读入缓存,这样读写的速度更快,然后再通过流读入到内存

  • 相关阅读:
    OpenMP
    linux下编写C++程序播放音频
    Canny Edge Detector
    部署服务器
    第五周--论文泛读
    AI研习社“看图猜作者”优秀代码技术总结
    Neural Multimodal Cooperative Learning Toward Micro-Video Understanding学习笔记
    第二次作业:卷积神经网络 part 2
    循环神经网络
    ORB-SLAM学习笔记
  • 原文地址:https://www.cnblogs.com/guochengfirst/p/9461574.html
Copyright © 2011-2022 走看看