文件I/O的问题

quietwater 2003-03-06 12:16:05
如何打印文件的最后10行,,,,,,,求最简单的方法,,,,,,,,,,,,,
...全文
23 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunny32 2003-03-07
  • 打赏
  • 举报
回复
xue xi
marf_cn 2003-03-06
  • 打赏
  • 举报
回复
我的想法:
将文件中的每一行作为一个string入栈,然后出栈读取即可。
zxhong 2003-03-06
  • 打赏
  • 举报
回复
最简单的,我喜欢
在一个jsp文件中取出要打印的10条记录
tomcat/conf/web.xml下加:
<mime-mapping>
<extension>xls</extension>
<mime-type>application/msexcel</mime-type>
</mime-mapping>
需要导出的文件头上加:
<%@ page contentType="application/vnd.ms-excel" %>

caoze 2003-03-06
  • 打赏
  • 举报
回复

读取文件,计数总行数

后退十行,

输出。

没其他的办法,这样可能最简单的了。

或(不知可行不可行)

读取文件,指向文件尾部,往前找换行符,

至第十个时读取,

输出。
quietwater 2003-03-06
  • 打赏
  • 举报
回复
给出我的答案,大家参考一下:
希望大家给出源代码,,,,,谢谢,,,,,

import java.io.*;
class Tail{
public static void main(String[] args)throws IOException{
if(args.length!=1){
System.err.println("请指定一个文件名");
System.exit(1);
}
int num=0;
String s=null;
BufferedReader br=null,b=null;
try{br=new BufferedReader(new FileReader(args[0]));}
catch(FileNotFoundException e){
System.err.println("系统找不到指定的文件"+args[0]);
}
finally{if (br==null) {System.exit(1);}}
while(br.readLine()!=null){
num++;
}
if(num>=10){
b=new BufferedReader(new FileReader(args[0]));
for(int i=0;i<num;i++){
if(i>=num-10){
System.out.println(b.readLine());
}
else b.readLine();
}
}
else System.err.println("指定文件"+args[0]+"不足十行");
}
}

62,614

社区成员

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

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