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
String line
rf seek(nextend)
int c =
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 ) charset))
}else {
System out println(line) // 输出为null 可以注释掉
}
nextend
}
nextend
rf seek(nextend)
if (nextend == ) {// 当文件指针退至文件开始处 输出第一行
System out println(new String(rf readLine() getBytes( ISO ) charset))
}
}
} 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:\\ txt gbk )
}
lishixinzhi/Article/program/Java/hx/201311/26379public static void main(String[] args) throws Exception {
File file = new File("E:/a.txt")// 100M
long start = System.currentTimeMillis()
String lastLine = readLastLine(file, "gbk")
long delt = System.currentTimeMillis() - start
System.out.println(lastLine)
System.out.println("读取时间(毫秒):" + delt)
file = new File("E:/b.txt")// 仅一行文字
start = System.currentTimeMillis()
lastLine = readLastLine(file, "gbk")
delt = System.currentTimeMillis() - start
System.out.println(lastLine)
System.out.println("读取时间(毫秒):" + delt)
}
public static String readLastLine(File file, String charset) throws IOException {
if (!file.exists() || file.isDirectory() || !file.canRead()) {
return null
}
RandomAccessFile raf = null
try {
raf = new RandomAccessFile(file, "r")
long len = raf.length()
if (len == 0L) {
return ""
} else {
long pos = len - 1
while (pos >0) {
pos--
raf.seek(pos)
if (raf.readByte() == '\n') {
break
}
}
if (pos == 0) {
raf.seek(0)
}
byte[] bytes = new byte[(int) (len - pos)]
raf.read(bytes)
if (charset == null) {
return new String(bytes)
} else {
return new String(bytes, charset)
}
}
} catch (FileNotFoundException e) {
} finally {
if (raf != null) {
try {
raf.close()
} catch (Exception e2) {
}
}
}
return null
}
package testfileimport java.io.BufferedReader
import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.FileReader
import java.io.InputStreamReader
import java.util.ArrayList
public class TestFile {
ArrayList<String>ar=new ArrayList<String>()
public static void main(String[] args) {
// TODO Auto-generated method stub
TestFile t=new TestFile()
try {
t.readFile("F://exe2.txt")
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
public void readFile(String st) throws Exception{
File file=new File(st)
FileReader fr=new FileReader(file)
BufferedReader bw=new BufferedReader(fr)
String str=null
while((str=bw.readLine())!=null){
ar.add(str)
}
for(int i=0i<ar.size()i++){
System.out.println(ar.get(i)+"\n\r")
}
}
}
读取最后一行只要取出 ar的最后一个值即可。。
注意:你写的问题在于你一次while循环 bw读了两次。。。小括号里一次,大括号里一次。。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)