Java implementation reverse reading file features sample sharing _java

Source: Internet
Author: User

Long End,long num,file file,string CharSet
4 parameter Descriptions
End corresponds to the coordinates, tail up the starting point, NUM is the number of rows read, the file destination file charset character set default UTF8
End is a null representation that is fetched up from the bottom of the file.

Map M=fileutil.tail (null,10,file,null)/Read the last 10 lines of the file, resulting in M.get (Fileutil.arr)
Fileutil.tail (M.get (fileutil.point), 3,file,null)/read the file to the bottom 11 lines to 13 lines, in fact, is the top 10th row and then read up 3 lines

Copy Code code as follows:

public class Fileutil {

Private static final long step=5000;

public static final String arr= "ARR";
public static final String point= "point";

public static Map tail (Long end,long num,file file,string charset) throws exception{
if (num<=0| | (end!=null&&end<0)) {
throw new IllegalArgumentException ();
}
Map map=new HashMap ();
Randomaccessfile Acc=null;
try {
ACC = new Randomaccessfile (file, "R");
Long Temp_end = (end = null. Acc.length (): End);
Long my_point = temp_end > step? (Temp_end-step): 0;
Acc.seek (My_point);
linkedlist<object[]> queue = new linkedlist<object[]> ();
String temp;
int n=0;
while ((Temp=acc.readline ())!=null) {
if (++n==1&&my_point!=0) {
Continue
}
Object[] Objects=new object[2];
Long Point = Acc.getfilepointer ();
if (point>=temp_end&&end!=null) {break;}
Objects[0]=point;
Objects[1]=new String (temp.getbytes ("8859_1"), CharSet);
if (Queue.size () ==num) {
Queue.poll ();
}
Queue.offer (objects);
}

if (Queue.size () <num&&my_point>0) {
Long Last_num=num-queue.size ();
Object[] Header = Queue.peek ();
if (header==null) {throw new RuntimeException ("Fileutil Step:" +step+ "not enough Long");
Map m = Tail ((Long) header[0], last_num, file,charset);
Map.put (point) (Point,m.get);
Map.put (Arr,arrayutils.addall (object[)) M.get (ARR), Queue.toarray ());
}else if (queue.size () >0) {//Gets insufficient number of rows and does not reach top
Map.put (Point,queue.peek () [0]);
Map.put (Arr,queue.toarray ());
}
}finally {
if (acc!=null) {
try {
Acc.close ();
catch (IOException e) {
E.printstacktrace ();
}
}
}
return map;
}


}

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.