admin管理员组

文章数量:1531657

以下就是将数据从硬盘读取到内存中详细解析

package com.liujinghe.c_lianxi.p1;
import java.io.*;
public class Demo1 {
    public static void main(String[] args) throws IOException {
        //将数据从硬盘写入内存中
        //1先创建文件类对象
        long start = System.currentTimeMillis();
        File file = new File("D:/file/aaa/1.txt");
        //2创建字节输入流对象
        FileInputStream fis = new FileInputStream(file);
        //3创建缓冲输入流对象
        BufferedInputStream bis = new BufferedInputStream(fis);
        //4创建缓冲数组
        byte[] buf = new byte[1024*4];
        //5将数据写入内存中
        int length=-1;
        int num=0;
        while((length=bis.read(buf))!=-1) {
            num++;
            System.out.println(length);
            System.out.println(new String(buf,0,length));
        }
        System.out.println("循环了"+num+"次");
        //6关闭流文件
        fis.close();
        bis.close();
        long end = System.currentTimeMillis();
        System.out.println(end-start);
    }
}

本文标签: 详解内存硬盘数据