java读入文件之后打印输出,怎么输出源文件的行号???

majianxiao143 2007-07-13 08:43:28
java读入文件之后打印输出,怎么输出源文件的行号???
...全文
222 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
public static void main(String[] args) throws IOException {
  // 从文件中读取数据
  BufferedReader br = new BufferedReader(new FileReader("java.java"));
  String s = "";  
  String line = System.getProperty("line.separator");
  StringBuffer sb = new StringBuffer();
  while((s = br.readLine())!=null) {
    sb.append(s).append(line);
  }
  br.close();
  // 添加行号,输出
  Scanner sc = new Scanner(sb.toString());
  int i = 0;
  while(sc.hasNextLine()) {
    i++;
    System.out.printf("%03d %s%n",i,sc.nextLine());
  }    
}
baobao28 2007-07-13
  • 打赏
  • 举报
回复
public void ReadFile(){
try {
FileReader f=new FileReader("d:\\aaa.txt");
BufferedReader in=new BufferedReader(f);
String s=null;
int i=0;

while ((s=in.readLine())!=null){
System.out.print(i+1+" ");
System.out.println(s);
i++;
}
} catch (Exception e) {
e.printStackTrace();
}
}

62,623

社区成员

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

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