Java NiO reads large files by line

Source: Internet
Author: User

Do the project process encountered to parse more than 100 m TXT file, and storage. With the previous FileInputStream, BufferedReader obviously not, although readline this method can be read directly by line, but to read a 140M, 68W data files, not only time-consuming and memory overflow, That is, you cannot wait until you have finished reading the 68W data when the memory overflows. So we have to use the related objects and methods under NiO.

Use a byte buffer (Java.nio.ByteBuffer), a channel for reading, writing, mapping, and manipulating Files (java.nio.channels.FileChannel); Set Text note sets ( Java.nio.charset.Charset); supports read and write to random access files (java.io.RandomAccessFile).

The specific idea is: set two buffers, one big small, large buffer for each read amount, small buffer to store each row of data (make sure the size can hold the longest line of text). Read when the judgment is not a newline character 13, is the word to return a row of data, not the words continue to read, until the completion of the file.

Implementation method:

FileChannel Fc=raf.getchannel ();

Read a file at a time, read the number of bytes cache
Bytebuffer fbb=bytebuffer.allocate (1024*5);
Fc.read (FBB);
Fbb.flip ();

Bytes per row cached according to your actual needs

Bytebuffer bb=bytebuffer.allocate (500);

Decide whether to finish reading the file

public Boolean Hasnext () throws IOException {

if (EOF) return false;
if (Fbb.position () ==fbb.limit ()) {//Determines whether the current position is bound to a buffer
if (ReadByte () ==0) return false;
}
while (true) {
if (Fbb.position () ==fbb.limit ()) {
if (ReadByte () ==0) break;
}
byte A=fbb.get ();
if (a==13) {
if (Fbb.position () ==fbb.limit ()) {
if (ReadByte () ==0) break;
}
return true;
}else{
if (Bb.position () < Bb.limit ()) {
Bb.put (a);
}else {
if (ReadByte () ==0) break;
}
}
}
return true;
}

Private int ReadByte () throws ioexception{
        // Prepare the buffer for re-reading the contained data: it keeps the limit constant and sets the position to zero.  
        fbb.rewind ();
        //makes the buffer ready for a new sequential channel read or relative get operation: It sets the limit to the current position and then sets the position to zero.  
        fbb.clear ();
        if (This.fc.read (FBB) ==-1) { 
             EOF=true;
            return 0;
        }else{
             Fbb.flip ();
            return fbb.position ();
        }
    }

    public byte[] Next () {
        bb.flip ();

It is important here to return a byte array convenient, the row is divided into the case of merging, otherwise if the buffer limit is reached, a Chinese character is removed two bytes, it will show abnormal
byte tm[] = Arrays.copyofrange (Bb.array (), Bb.position (), Bb.limit ());
Bb.clear ();
return TM;
}

Java NiO reads large files by line

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.