java 读取文件指定行的内容

justmewei 2010-04-19 10:35:11
比如现有一d:\11.txt,内容为
<NUMBER 2>
ddddddddddd
fffffffffff
ggggggggggg
hhhhhhhhhhh
...
如何读取指定行的内容? 查看了API,LineNumberInputStream类似乎可以解决这个问题,但是函数getLineNumber()得到的只是当前的行号。
另外一个思路就是:按行读取文件内容,把每行作为String存入ArrayList<String>中,然后在通过get(index)获取指定行的内容。功能是实现了,但是感觉效率不高,尤其是对内容比较大的文本的读取。

网上找到的一个程序:
/** 
* 读取文件指定行。
*/
public class ReadSelectedLine {
// 读取文件指定行。
static void readAppointedLineNumber(File sourceFile, int lineNumber) throws IOException {
FileReader in = new FileReader(sourceFile);
LineNumberReader reader = new LineNumberReader(in);
String s = reader.readLine();

if (lineNumber < 0 || lineNumber > getTotalLines(sourceFile)) {
System.out.println("不在文件的行数范围之内。");
}

{
while (s != null) {
System.out.println("当前行号为:" + reader.getLineNumber());

System.out.println(s);
System.exit(0);
s = reader.readLine();
}
}
reader.close();
in.close();
}

// 文件内容的总行数。
static int getTotalLines(File file) throws IOException {
FileReader in = new FileReader(file);
LineNumberReader reader = new LineNumberReader(in);
String s = reader.readLine();
int lines = 0;
while (s != null) {
lines++;
s = reader.readLine();
}
reader.close();
in.close();
return lines;
}

public static void main(String[] args) throws IOException {

// 读取文件
File sourceFile = new File("d:/11.txt");
// 获取文件的内容的总行数
int totalNo = getTotalLines(sourceFile);
System.out.println("There are "+totalNo+ " lines in the text!");

// 指定读取的行号
int lineNumber = 2;

// 读取指定的行
readAppointedLineNumber(sourceFile, lineNumber);



}


}

在这个程序中, 函数readAppointedLineNumber(File sourceFile, int lineNumber)的第二个参数好象起到读取指定行内容的作用

再就是如何让程序直接从文本的第二行开始读取呢?
...全文
7659 13 打赏 收藏 转发到动态 举报
写回复
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
hezhiqili 2012-11-07
  • 打赏
  • 举报
回复
这方面太难了 老是记不住
yangxuserene 2012-10-11
  • 打赏
  • 举报
回复
直接用while的控制不行么 读到需要的那一行进行处理 其他的直接忽略
阿诺 2012-01-13
  • 打赏
  • 举报
回复
正好有这个需求
蒙奇D路飞 2011-05-12
  • 打赏
  • 举报
回复
网上的那个程序 我跑的结果不对 不是读出我指定的行 楼主你读出来了?其中的system.exit(0)程序就直接退出了呀
[Quote=引用楼主 justmewei 的回复:]
比如现有一d:\11.txt,内容为
<NUMBER 2>
ddddddddddd
fffffffffff
ggggggggggg
hhhhhhhhhhh
...
如何读取指定行的内容? 查看了API,LineNumberInputStream类似乎可以解决这个问题,但是函数getLineNumber()得到的只是当前的行号。
另外一个思路就是:按行读取文件内容,把每行作为Stri……
[/Quote]
OopsJeff 2010-04-20
  • 打赏
  • 举报
回复
每一行行的读取吧
hbgzg3006 2010-04-20
  • 打赏
  • 举报
回复
我给楼主提供一个空间换时间的方法。文档的换行无论是\r\n还是\n你都可以用\n来分隔。然后分隔完成的字符串数组0 1 2就是第1,2,3行,读取指定一行的内容直接取【i-1】数组内容即可。
wd9053 2010-04-20
  • 打赏
  • 举报
回复
每行长度相等时,可以这样做
import java.io.*;

public class Test
{
private static final int line = 4;//每行的长度,包括\t或\r\t,注意区别

public static void main(String[] args) throws Exception
{
RandomAccessFile raf = new RandomAccessFile("a.txt", "r");
int lineIndex = 3;
raf.seek(line * lineIndex);
System.out.println(raf.readLine());
}
}
cwjieNo1 2010-04-20
  • 打赏
  • 举报
回复

/**
* 随机读取文件内容
* @param fileName 文件名
*/
public static void readFileByRandomAccess(String fileName){
RandomAccessFile randomFile = null;
try {
System.out.println("随机读取一段文件内容:");
// 打开一个随机访问文件流,按只读方式
randomFile = new RandomAccessFile(fileName, "r");
// 文件长度,字节数
long fileLength = randomFile.length();
// 读文件的起始位置
int beginIndex = (fileLength > 4) ? 4 : 0;
//将读文件的开始位置移到beginIndex位置。
randomFile.seek(beginIndex);
byte[] bytes = new byte[10];
int byteread = 0;
//一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
//将一次读取的字节数赋给byteread
while ((byteread = randomFile.read(bytes)) != -1){
System.out.write(bytes, 0, byteread);
}
} catch (IOException e){
e.printStackTrace();
} finally {
if (randomFile != null){
try {
randomFile.close();
} catch (IOException e1) {
}
}
}
}
/**

网上找的随机读取文件的例子
justmewei 2010-04-20
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 hbgzg3006 的回复:]
引用 7 楼 justmewei 的回复:

引用 4 楼 hbgzg3006 的回复:
我给楼主提供一个空间换时间的方法。文档的换行无论是\r\n还是\n你都可以用\n来分隔。然后分隔完成的字符串数组0 1 2就是第1,2,3行,读取指定一行的内容直接取【i-1】数组内容即可。

String[] line = allContentInTxt.split(\\n);
再去遍历Stri……
[/Quote]嗯,谢谢!
hbgzg3006 2010-04-20
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 justmewei 的回复:]

引用 4 楼 hbgzg3006 的回复:
我给楼主提供一个空间换时间的方法。文档的换行无论是\r\n还是\n你都可以用\n来分隔。然后分隔完成的字符串数组0 1 2就是第1,2,3行,读取指定一行的内容直接取【i-1】数组内容即可。

String[] line = allContentInTxt.split(\\n);
再去遍历String[],是吗? 我感觉这样的方法可行,但不可取……
[/Quote]

//对于不是特别大的文件,我想这个只需要读取文件一次就可以了。所以可取。特大文件就不行了。
InputStream in=new FileInputStream("D:\\test\\changes.txt");
byte[]b=new byte[in.available()];
in.read(b);
String []line=new String(b).split("\\n");
System.out.println(line.length);//总行数
System.out.println(line[900].replaceAll("\\r", ""));//第900行数据
justmewei 2010-04-20
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 hbgzg3006 的回复:]
我给楼主提供一个空间换时间的方法。文档的换行无论是\r\n还是\n你都可以用\n来分隔。然后分隔完成的字符串数组0 1 2就是第1,2,3行,读取指定一行的内容直接取【i-1】数组内容即可。
[/Quote]
String[] line = allContentInTxt.split(\\n);
再去遍历String[],是吗? 我感觉这样的方法可行,但不可取吧。自我感觉。
justmewei 2010-04-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wd9053 的回复:]
每行长度相等时,可以这样做

Java code
import java.io.*;

public class Test
{
private static final int line = 4;//每行的长度,包括\t或\r\t,注意区别

public static void main(String[] args) throws Exception
……
[/Quote]
在我的应用中,每一行的内容长度是不等,所以你的方法,在我的这里解决不了的。
下面的代码可以实现每行长度不等的情况下,读取指定行的内容。
public class ReadSelectedLine {

static void readLineVarFile(String fileName, int lineNumber) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(fileName)));
String line = reader.readLine();
if (lineNumber < 0 || lineNumber > getTotalLines(fileName)) {
System.out.println("不在文件的行数范围之内。");
}
int num = 0;
while (line != null) {
if (lineNumber == ++num) {
System.out.println("line " + lineNumber + ": " + line);
}
line = reader.readLine();
}
reader.close();
}


// 文件内容的总行数。
static int getTotalLines(String fileName) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream(fileName)));
LineNumberReader reader = new LineNumberReader(in);
String s = reader.readLine();
int lines = 0;
while (s != null) {
lines++;
s = reader.readLine();
}
reader.close();
in.close();
return lines;
}

public static void main(String[] args) throws IOException {

// 读取文件
String fileName = "d:/hst_23.txt";

// 获取文件的内容的总行数
int totalNo = getTotalLines(fileName);
System.out.println("There are "+totalNo+ " lines in the text!");

// 指定读取的行号
int lineNumber = 10;

//读取指定行的内容
readLineVarFile("d:/hst_23.txt", lineNumber);
}
}
canoe982 2010-04-19
  • 打赏
  • 举报
回复
用随机文件操作。有个类似叫RandomAccessFile的文件吧。不记得正确名字是什么呢,反应有Random Access这两个词的。
相关推荐

62,567

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告