java

weili86yy 2007-07-24 03:04:07
3、请编写一个字符输入流的包装类,通过这个包装类对底层字符输入流进行包装,让程序通过这个包装类读取某个文本文件(例如,一个java源文件)时,能够在读取的每行前面都加上有行号和冒号。
...全文
154 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
cursor_wang 2007-07-24
  • 打赏
  • 举报
回复
import java.io.*;
public class ReadFile{

public static void main(String[] args){
try{
BufferedReader br=new BufferedReader(new FileReader("D:/ReadFile.java"));
String s;
int i=0;
while((s=br.readLine())!=null)
{
System.out.println(++i+":"+s);

}
br.close();
}catch(FileNotFoundException e1){
System.err.print("File not found!");
}catch(IOException e2){
System.out.print(e2);
}
}
}
sureyor 2007-07-24
  • 打赏
  • 举报
回复
给个方法吧:

public String getLineData(String filePath, int lineNum) {
BufferedReader in = new BufferedReader(new FileReader(filePath));
String str = null;
int count = 0;
while ((str = in.readLine()) != null) {
count++;
if(count == lineNum){
break;
}
}
in.close();

if (str != null) {
str = new Integer(count).toString() + ":" + str;
}

return str;
}
shan1119 2007-07-24
  • 打赏
  • 举报
回复
try{
BufferedReader brF = new BufferedReader(new FileReader("fileName"));

String line = "";
int i=0;
while((line=brF.readLine()) != null){
System.out.println(++i+":"+line);
}
brF.close();
}catch(Exception e){}

62,623

社区成员

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

试试用AI创作助手写篇文章吧