How Java implements reading files on a specified line

Source: Internet
Author: User

Recently, in the development of actual combat, encountered a such a technical scenario:

Log4j generated log files timed to the MySQL database, such as three hours brush once, then each time you brush the data, how to control the file read from the last time the file read the end of the beginning to continue to read? And this secondary reads to the end of the file. After a variety of online search questions, found a call Randomaccessfile Java class solved the problem.

Static intsize=1;//mainly to control the number of cycles, because it is a timed brush, the number of files per brush may be different    Static Longchars=0;//chars refers to the number of characters    /*** Read File contents *@paramFileName*/       PublicMap ReadAndWrite (String fileName) {//A large collection, with SessionID as key, to store all access records of a session to the value listMap<string,map> Bigmap =NewHashMap (); //a collection of access records for a sessionFile File =NewFile (fileName); //Java provides a class that can read files in a page, and instances of this class support reading and writing to random access filesRandomaccessfile RF =NULL; String tempstring=NULL; Try {                                    //Initialize Randomaccessfile, parameter one is file path, one is permission setting, this is similar to Linux, R is read, W is writeRF =NewRandomaccessfile (FileName, "RW"); //sets the offset of the file pointer measured to the beginning of this file, where the next read or write operation occursRf.seek (chars); //gets the number of rows in the file                intFileSize =gettotallines (file);  for(inti = size-1; i < fileSize; i++) {//the number of file lines from the end of the last read to the total number of rows to read the file in the middle of this difference is the number of cycles//A row of rows readsTempString =Rf.readline (); //file Chinese garbled processingtempstring = Tempstring.replaceall ("% (?! [0-9a-fa-f] {2}) ","%25 "); TempString= Tempstring.replaceall ("\\+", "%2b"); TempString= Java.net.URLDecoder.decode (tempstring, "GB2312"); //converts a string json to an entity JSON so that the value can be taken by keyJsonobject JSON =Jsonobject.fromobject (tempstring); String Refpage= Json.get ("Refpage"). toString ();                    System.out.println (Refpage); Map TMap=NewHashMap (); if(Bigmap.containskey (refpage)) TMap=(MAP) bigmap.get (refpage); Else{TMap=NewHashMap (); }                    //CountString TCount = "Count"; intPvcount = 1; if(Tmap.containskey (TCount)) {Pvcount=(Integer) tmap.get (TCount); } Pvcount++;                    Tmap.put (TCount, Pvcount);                Bigmap.put (Refpage, TMap); }                //returns the current offset in this file. chars =Rf.getfilepointer (); Size=fileSize; } Catch(IOException e) {e.printstacktrace (); }Catch(Jsonexception j) {}finally {                if(RF! =NULL) {                    Try{rf.close (); } Catch(IOException E1) {}}} returnBigmap; }      //gets the number of rows in the file      Static intGettotallines (File file)throwsIOException {FileReader in=Newfilereader (file); LineNumberReader Reader=NewLineNumberReader (in); String s=Reader.readline (); intLines = 0;  while(s! =NULL) {lines++; S=Reader.readline ();            } reader.close ();            In.close (); returnlines; }
View Code

How Java implements reading files on a specified line

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.