我试了下 用下面的方法是可以读完的。
import java.util.*
import java.text.*
import java.io.*
public class ScannerDemo{
public static void main(String args[]){
File f = new File("D:" + File.separator + "test.txt") // 指定 *** 作文件
Scanner scan = null
try{
scan = new Scanner(f)
}catch(Exception e){}
StringBuffer str = new StringBuffer()
while(scan.hasNext()){
str.append(scan.next()).append('\n') //取数据
}
System.out.println("文件内容为:" + str)
}
}
JAVA如何实现从最后一行读取文件
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/26379
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)