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;
}
}