以下是随便写的代码,主要是提供思路,需要调试的。下班了,没时间调了 RandomAccessFile r = null; // LineNumberReader lnr = new LineNumberReader(new FileReader(file)); // ArrayList al = new ArrayList(); try{ r = new RandomAccessFile(file,"r"); long start = r.getFilePointer(); long cur = start + r.length()-1; String result = ""; r.seek(cur); int c=-1; while(cur>=start){ c = r.read(); if(c=='\n'||c=='\r'){ r.seek(cur+1); result = r.readLine(); // do more handle about the result here // ... } cur--; } }finally{ if(r!=null) r.close(); }
RandomAccessFile r = null;
// LineNumberReader lnr = new LineNumberReader(new FileReader(file));
// ArrayList al = new ArrayList();
try{
r = new RandomAccessFile(file,"r");
long start = r.getFilePointer();
long cur = start + r.length()-1;
String result = "";
r.seek(cur);
int c=-1;
while(cur>=start){
c = r.read();
if(c=='\n'||c=='\r'){
r.seek(cur+1);
result = r.readLine();
// do more handle about the result here
// ...
}
cur--;
}
}finally{
if(r!=null)
r.close();
}
package rfile;import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;public class FromEndRF {
public static void main(String args[]){
try {
RandomAccessFile rf=new RandomAccessFile("H:/JSP/tech/login.jsp","r");
long len=rf.length();
long start=rf.getFilePointer();
System.out.println(start);
long nextend=start+len-1;
String line;
rf.seek(nextend);
int c=-1;
while(nextend>start){
c=rf.read();
if(c=='\n'||c=='\r'){
line=rf.readLine();
System.out.println(line);
}
nextend--;
rf.seek(nextend);
if(nextend==0){//当文件指针退至文件开始处,输出第一行
System.out.println(rf.readLine());
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{
try {
rf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;public class FromEndRF {
public static void main(String args[]) {
RandomAccessFile rf = null;
try {
rf = new RandomAccessFile("d:\\2.txt", "r");
long len = rf.length();
long start = rf.getFilePointer();
System.out.println(start);
long nextend = start + len - 1;
String line;
rf.seek(nextend);
int c = -1;
while (nextend > start) {
c = rf.read();
if (c == '\n' || c == '\r') {
line = rf.readLine();
System.out.println(line);
nextend--;
}
nextend--;
rf.seek(nextend);
if (nextend == 0) {// 当文件指针退至文件开始处,输出第一行
System.out.println(rf.readLine());
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (rf != null)
rf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
if (c == '\n' || c == '\r') {
line = rf.readLine();
System.out.println(line);
nextend--;
}
// 2 null 代表这一行到了文件末尾,而且啥也没有读到,而且这是API里面说明的
// null if end of file is encountered before even one byte is readpackage test;import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;public class FromEndRF {
public static void read(String filename) {
read(filename, "GBK");
} public static void read(String filename, String charset) { RandomAccessFile rf = null;
try {
rf = new RandomAccessFile(filename, "r");
long len = rf.length();
long start = rf.getFilePointer();
long nextend = start + len - 1;
String line;
rf.seek(nextend);
int c = -1;
while (nextend > start) {
c = rf.read();
if (c == '\n' || c == '\r') {
line = rf.readLine();
if (line != null) {
System.out.println(new String(line.getBytes("ISO-8859-1"), charset));
}else {
System.out.println(line);
}
nextend--;
}
nextend--;
rf.seek(nextend);
if (nextend == 0) {// 当文件指针退至文件开始处,输出第一行
System.out.println(rf.readLine());
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (rf != null)
rf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} public static void main(String args[]) {
read("d:\\2.txt", "gbk");
}
}
package zipfile;import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;public class FromEndRF {
public static void main(String args[]){
RandomAccessFile rf=null;
try {
rf=new RandomAccessFile("H:/JSP/tech/login.jsp","r");
long len=rf.length();
long start=rf.getFilePointer();
long nextend=start+len-1;
String line;
rf.seek(nextend);
int c=-1;
while(nextend>start){
c=rf.read();
if(c=='\n'||c=='\r'){
line=rf.readLine();
if(line==null){//处理文件末尾是空行这种情况
nextend--;
rf.seek(nextend);
continue;}
System.out.println(line);
nextend--;
}
nextend--;
rf.seek(nextend);
if(nextend==0){//当文件指针退至文件开始处,输出第一行
System.out.println(rf.readLine());
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{
try {
rf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
给个简单点的思路,是我刚刚在做的过程中想到的.
直接一行行把读读到的内容放到一个list对象中.然后反着打印list里面的记录,不就可以了吗?
这个方法是不是很好呢?做程序真的是写的好不如想的好,好的思路永远是最重要的