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)则先将数据读入缓存,这样读写的速度更快,然后再通过流读入到内存

  • 相关阅读:
    函数定义、调用
    条件语句和循环语句
    eclipse中安装pydev插件出现duplicate location
    编码类型
    除法
    数据类型
    命令和python模式转换
    前言
    SpringMVC_json
    解决eclipse中Tomcat服务器的server location选项不能修改的问题
  • 原文地址:https://www.cnblogs.com/guochengfirst/p/9461574.html
Copyright © 2011-2022 走看看