Using Randomaccessfile to read large files in Java

Source: Internet
Author: User

Very large files We use normal file read way are very slow, and in Java provides me with the Randomaccessfile function, can quickly read large files and do not feel the card oh, below to see a demo instance of me.

Server log files tend to reach more than 400 m, simple file read is too slow, too much machine resources.

In particular, if you need 5 minutes to scan the log file, to count some of the real-time data. For example, just 10 minutes of visiting customers (large Web site user statistics system such as 51.la will often use it.) It is important to instantly scan a portion of a big data file.
This article describes the use of Java's Randomaccessfile method to read partial bytes from a very large file

Total size of test file 46,085 bytes

Read the last 85 bytes of a file

File Size: 46085
85

Test effect

Extended Features-Youku video
Other operations, such as the odd arts, potatoes and the like are in the same order. Of course we can also read from 46,000 to read only 20 bytes, see personal needs, here just as an example

Package com.javaer.examples.file; import java.io.ioexception;import java.io.RandomAccessFile; Public classReadbigfile { Public Static voidReadbigfile () throws ioexception{String FileName="/users/mc2/desktop/youku.txt"; Randomaccessfile Randomfile=NULL; Randomfile=NewRandomaccessfile (FileName,"R"); LongFilelength =randomfile.length (); System. out. println ("File Size:"+filelength); intStart =46000;        Randomfile.seek (start); byte[] bytes =New byte[ the]; intByteread =0; //read 10 bytes at a time, or read the remaining bytes if the contents of the file are less than 10 bytes. //assigns the number of bytes read to Byteread         while((byteread = randomfile.read (bytes))! =-1) {           //System.out.write (bytes, 0, byteread);} System. out. println (Bytes.length); System. out. println (NewString (Bytes,"UTF-8")); if(Randomfile! =NULL) {randomfile.close (); }  } /** * @param args * @throws ioexception*/  Public Static voidMain (string[] args) throws IOException {Readbigfile.readbigfile ();}}

Even large files, reading a bit of data from the inside, are fast. Read them all and consume very little memory.

Core tip: Randomfile.seek (start);

Jump read, start reading from here. The pointer directly refers to the start position to begin reading the file.

Bytes get can be replaced as follows, different occasions, different use

byte[] bytes =New byte[ the]; intByteread =0; //read 10 bytes at a time, or read the remaining bytes if the contents of the file are less than 10 bytes. //assigns the number of bytes read to Byteread         while((byteread = randomfile.read (bytes))! =-1) {           //System.out.write (bytes, 0, byteread);} System. out. println (Bytes.length);byte[] bytes; intByteread =0; Bytearrayoutputstream Byteout=NewBytearrayoutputstream (); byteTmp[] =New byte[1024x768]; bytecontext[]; inti =0; inthas=0;  while((i = Randomfile.read (tmp))! =-1) {byteout.write (TMP,0, i); has+=i; if(Has >10240) Break; } bytes= Byteout.tobytearray ();

Using Randomaccessfile to read large files in Java

Related Article

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.