网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

java中读取文件的方式有哪几种

读取文件有多种方式,基于传统的输入流方式或基于nio的Buffer缓冲对象和管道读取方式甚至非常快速的内存映射读取文件。

站在用户的角度思考问题,与客户深入沟通,找到岳普湖网站设计与岳普湖网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站建设、成都网站建设、企业官网、英文网站、手机端网站、网站推广、空间域名、网站空间、企业邮箱。业务覆盖岳普湖地区。

java中四种读取文件方式:

1、RandomAccessFile:随机读取,比较慢优点就是该类可读可写可操作文件指针

2、FileInputStream:io普通输入流方式,速度效率一般

3、Buffer缓冲读取:基于nio Buffer和FileChannel读取,速度较快

4、内存映射读取:基于MappedByteBuffer,速度最快

RandomAccessFile读取

//RandomAccessFile类的核心在于其既能读又能写

public void useRandomAccessFileTest() throws Exception {

    RandomAccessFile randomAccessFile = new RandomAccessFile(new File("e:/nio/test.txt"), "r");

    byte[] bytes = new byte[1024];
    int len = 0;
    while ((len = randomAccessFile.read(bytes)) != -1) {
        System.out.println(new String(bytes, 0, len, "gbk"));
    }

    randomAccessFile.close();
}

FielInputStream读取

//使用FileInputStream文件输入流,比较中规中矩的一种方式,传统阻塞IO操作。

public void testFielInputStreamTest() throws Exception {

    FileInputStream inputStream = new FileInputStream(new File("e:/nio/test.txt"));

    // 使用输入流读取文件,以下代码块几乎就是模板代码
    byte[] bytes = new byte[1024];
    int len = 0;
    while ((len = inputStream.read(bytes)) != -1) {// 如果有数据就一直读写,否则就退出循环体,关闭流资源。
        System.out.println(new String(bytes, 0, len, "gbk"));
    }
    inputStream.close();
}

Buffer缓冲对象读取

// nio 读取

public void testBufferChannel() throws Exception {

    FileInputStream inputStream = new FileInputStream(new File("e:/nio/test.txt"));

    FileChannel fileChannel = inputStream.getChannel();
    ByteBuffer buffer = ByteBuffer.allocate(1024);

    // 以下代码也几乎是Buffer和Channle的标准读写操作。
    while (true) {
        buffer.clear();
        int result = fileChannel.read(buffer);
        buffer.flip();
        if (result == -1) {
            break;
        }
        System.out.println(new String(buffer.array(), 0, result, "gbk"));
    }
    inputStream.close();
}

内存映射读取

public void testmappedByteBuffer() throws Exception {

    FileInputStream inputStream = new FileInputStream(new File("e:/nio/test.txt"));
    FileOutputStream outputStream = new FileOutputStream(new File("e:/nio/testcopy.txt"),true);

    FileChannel inChannel = inputStream.getChannel();
    FileChannel outChannel = outputStream.getChannel();

    System.out.println(inChannel.size());
    MappedByteBuffer mappedByteBuffer = inChannel.map(MapMode.READ_ONLY, 0, inChannel.size());

    System.out.println(mappedByteBuffer.limit());
    System.out.println(mappedByteBuffer.position());

    mappedByteBuffer.flip();
    outChannel.write(mappedByteBuffer);

    outChannel.close();
    inChannel.close();
    outputStream.close();
    inputStream.close();

}

//基于内存映射这种方式,这么写好像有问题。


MappedByteBuffer和RandomAcessFile这两个类要单独重点研究一下。

//TODO 大文件读取

以上就是java中如何读取文件?的详细内容,更多请关注创新互联其它相关文章!


本文题目:java中读取文件的方式有哪几种
当前链接:http://bjjierui.cn/article/gegcsh.html

其他资讯