LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 2202|回复: 2

RandomAccessFile怎么删除文本文件的一行?

[复制链接]
发表于 2005-12-9 17:31:22 | 显示全部楼层 |阅读模式
RandomAccessFile怎么更新文本文件的一行?谢谢了!
因为我写一个程序,更新文件中的某一行。我截取这一行的前后两段first,last,再用新的内容newline代替这一行,最后write((first+newline+last).getBytes());
只要新内容比原来少,就会出错--原来的内容的文件结尾部分并没有消失。

目的是更新一个HTML文件。
RandomAccessFile rf;
String contentsFirst="";
String contentsLast="";
String modifiedLatestlist = null;
try{
rf = new RandomAccessFile(leftbar.html","rw");
byte[] text = new byte[(int)(rf.length())];
rf.readFully(text);
contents = new String(text);
rf.close();

int first = contents.indexOf("<ol id=\"latestlists\">");
int last = contents.indexOf("</ol>",first);
contentsFirst = contents.substring(0,first);
contentsLast = contents.substring(last,(int)contents.length());

modifiedLatestlist = "<ol id=\"latestlists\">\n";
modifiedLatestlist+="<li>动态内容,省去</li>\n";

rf = new RandomAccessFile(leftbar.html","rw");
rf.write((contentsFirst+modifiedLatestlist+contentsLast).getBytes());
rf.close();
}catch(Exception e){}
发表于 2005-12-10 00:45:39 | 显示全部楼层
RandomAccessFile不熟,它不属于Java I/O hierarchy,个人感觉像C/C++的操作方式

要是不用RandomAccessFile的话,可以参考下面

-----database.txt----
ay51 helsinki beijing 10 600


  1.         public void deleteData() throws IOException {
  2.                 System.out.println("---Remove entity in the database---");
  3.                 System.out.println("Input the flight number you want to delete:");
  4.                 BufferedReader stdin = new BufferedReader(new InputStreamReader(
  5.                                 System.in));
  6.                 String queryFlightNum = stdin.readLine();
  7.                 BufferedReader in = new BufferedReader(new FileReader(fileName));  // fileName为"database.txt"
  8.                 String s = null;
  9.                 boolean flightNumFound = false;
  10.                 StringBuffer sb = new StringBuffer();
  11.                 while ((s = in.readLine()) != null) {
  12.                         String[] result = s.split(" ");
  13.                         String flightNum = result[0];
  14.                         if (!flightNum.equals(queryFlightNum))
  15.                                 sb.append(s + "\n");
  16.                         else
  17.                                 flightNumFound = true;
  18.                 }
  19.                 if (flightNumFound) {
  20.                         PrintWriter out = new PrintWriter(new BufferedWriter(
  21.                                         new FileWriter(fileName)));
  22.                         out.print(sb.toString());
  23.                         out.close();
  24.                         System.out.println("success delete");
  25.                         calcTotalPrice();
  26.                 } else {
  27.                         System.out.println("Cann't find such flight number!");
  28.                 }
  29.         }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-12-10 09:35:39 | 显示全部楼层
谢谢你。
不过问题还没解决。RandomAccessFile所操作的文件,不能减小文件,只能增大文件吗?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表