这个试试 import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class b {
public static void main(String [] args){ read("D:/s.log"); }
public static void read(String filename) { try { BufferedReader in = new BufferedReader(new FileReader(filename)); String s = ""; String result=""; while ((s=in.readLine())!= null) { result = s; } System.out.println(result); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } /*RandomAccessFile rf = null; try {
rf = new RandomAccessFile(filename, "r"); long len = rf.length(); long start = rf.getFilePointer();//返回此文件中的当前偏移 String line; int c = -1; //long nextend = start + len - 1; // rf.seek(nextend); //设置相对文件头的指针偏移量,在此位置进行下一次读写操作。
for( long nextend = start + len - 1;nextend >=nextend;nextend--) { c = rf.read(); if (c == '\n' || c == '\r') { line = rf.readLine(); if (line != null ) {
String aa =new String(line); System.out.println(aa);
我得意思是想
只打印出 aac其他的不打印!!
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class b {
public static void main(String [] args){
read("D:/s.log");
}
public static void read(String filename) {
try {
BufferedReader in = new BufferedReader(new FileReader(filename));
String s = "";
String result="";
while ((s=in.readLine())!= null) {
result = s;
}
System.out.println(result);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*RandomAccessFile rf = null;
try {
rf = new RandomAccessFile(filename, "r");
long len = rf.length();
long start = rf.getFilePointer();//返回此文件中的当前偏移
String line;
int c = -1;
//long nextend = start + len - 1;
// rf.seek(nextend); //设置相对文件头的指针偏移量,在此位置进行下一次读写操作。
for( long nextend = start + len - 1;nextend >=nextend;nextend--) {
c = rf.read();
if (c == '\n' || c == '\r') {
line = rf.readLine();
if (line != null ) {
String aa =new String(line);
System.out.println(aa);
if(aa.equalsIgnoreCase("even_chung")){
break;
}
}else {
System.out.println(line+"-");
}
nextend--;
}
try{
rf.seek(nextend);
}
catch(Exception e){
e.getStackTrace();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (rf != null)
rf.close();
} catch (IOException e) {
e.printStackTrace();
}
}*/
}
}
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class b {
public static void main(String [] args){
read("D:/s.log");
}
public static void read(String filename) {
try {
BufferedReader in = new BufferedReader(new FileReader(filename));
String s = "";
String result="";
while ((s=in.readLine())!= null) {
result = s;
}
System.out.println(result);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
result = s;
String aa =new String(line);
aa.trim();
if(aa.length>0)
{
lastStr=aa;
}lastStr应该就是最后一行了吧!
还有一点,就是在生成日志文件的时候,应该先换行再写入你要添加的记录(文件为空时不添加换行),而不是再添加后换行,也许可以避免有空行产生