Java Quick Read Large file

Source: Internet
Author: User

Private String fileseparator = File.separator; Distinguish between different window platform directory separators
Private String FilePath = "E:" +fileseparator+ "Code" +fileseparator+ "pdm-server-parent" +fileseparator+ "Pdm-server" + fileseparator+ "Logs";
Private final int buffer_size = 0x300000;//buffer size is 3M

Public string getlogallcontent (string filename) throws IOException {
String flagstring = "";
/*filereader reader = new FileReader (filepath+fileseparator+filename);
BufferedReader breader = new BufferedReader (reader);
while ((Breader.readline ())!=null) {
Flagstring + = Breader.readline () + "\ n";
} */
File File = new file (filepath+fileseparator+filename);
Mappedbytebuffer inputbuffer = new Randomaccessfile (file, "R")
. Getchannel (). Map (FileChannel.MapMode.READ_ Only, 0, file.length ()); Read all content
byte[] logbyte = new byte[buffer_size];//each read 3M

for (int offset = 0; offset < inputbuffer.capacity ( ); Offset + = buffer_size) {
if (inputbuffer.capacity ()-offset >= buffer_size) {
for (int i = 0; i < buffer_size; i++)
Logbyte[i] = inputbuffer.get (offset + i);
} else{
for (int i = 0; i < inputbuffer.capacity ()-offset; i++)
Logbyte[i] = inputbuffer.get (offset + i);
}

int length = (inputbuffer.capacity ()% Buffer_size = = 0)? Buffer_size
: inputbuffer.capacity ()% Buffer_size;
flagstring = new String (logbyte, 0, length);
}
return flagstring;
}

Java Quick Read Large file

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.