读书人

大家都来看看吧-gt;帮小弟我看看

发布时间: 2012-01-03 22:16:06 作者: rapoo

大家都来看看吧->帮我看看
编写一程序通过读取某个文本文件(例如,.txt文件)时,能够在读取的每行前面都加上有行号和冒号。 请问应该怎么编??具体有代码最好。谢谢啊!实在想不出来了!

[解决办法]

int count=1;
while(...)
{
String temp = bufferedReader.readLine();
temp = count+":" +temp;
count++;


}


[解决办法]
读取每行时,直接在前面加个行号和冒号

reader ..
int count = 0;
String line= reader.readLine();
List lt;

while(line != null){
line = count + ":" + line;
lt.add(line);
}
[解决办法]
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class TestMainFile {
public static void main(String[] args) {
File f = new File("d:/aaa.txt");
try {
FileReader rd = new FileReader(f);
BufferedReader br = new BufferedReader(rd);
String brl = br.readLine();
int i=0;
while (brl != null) {
System.out.println(++i+":"+brl);
brl = br.readLine();
}
br.close();
rd.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

读书人网 >J2EE开发

热点推荐