文件输入输出流

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

原文地址:https://www.cnblogs.com/guochengfirst/p/9461574.html