Home > Java > JavaBase > body text

How to read files in java?

Release: 2019-12-04 10:01:05
Original
4610 people have browsed it

How to read files in java?

There are many ways to read files, based on the traditional input stream method or nio-based Buffer buffer object and pipe reading method, or even very fast memory mapping to read files.

Four ways to read files in java: (Recommended: java video tutorial)

1. RandomAccessFile: random reading, relatively slow, the advantage is that this type is readable Writable and operable file pointer

2. FileInputStream: io ordinary input stream mode, average speed and efficiency

3. Buffer buffer reading: based on nio Buffer and FileChannel reading, faster

4. Memory mapped reading: based on MappedByteBuffer, the fastest

RandomAccessFile reading

//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();
}
Copy after login

FielInputStream reading

//使用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();
}
Copy after login

Buffer buffer object reading

// 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();
}
Copy after login

Memory mapping reading

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 大文件读取
Copy after login

For more java knowledge, please pay attention java basic tutorial column.

The above is the detailed content of How to read files in java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!