java.io
Class RandomAccessFilejava.lang.Object
  extended byjava.io.RandomAccessFileAll Implemented Interfaces:
    DataInput, DataOutputpublic class RandomAccessFile
extends Object
implements DataOutput, DataInputInstances of this class support both reading and writing to a random access file. A random access file behaves like a large array of bytes stored in the file system. There is a kind of cursor, or index into the implied array, called the file pointer; input operations read bytes starting at the file pointer and advance the file pointer past the bytes read. If the random access file is created in read/write mode, then output operations are also available; output operations write bytes starting at the file pointer and advance the file pointer past the bytes written. Output operations that write past the current end of the implied array cause the array to be extended. The file pointer can be read by the getFilePointer method and set by the seek method.It is generally true of all the reading routines in this class that if end-of-file is reached before the desired number of bytes has been read, an EOFException (which is a kind of IOException) is thrown. If any byte cannot be read for any reason other than end-of-file, an IOException other than EOFException is thrown. In particular, an IOException may be thrown if the stream has been closed.Since:
    JDK1.0

解决方案 »

  1.   

    我的建议是进行字符串的对比匹配,然后进行替换还有一种方法是将所有内容一起读进内存中,存放在数组里,变成对数组的操作,最后再写回txt文件中等待更加好的答案
      

  2.   

    最简单的方法,实例化文件的流后做5个readLine()
      

  3.   

    import java.io.*;public class test {
      public static void main(String args[]) {
        int line = 5; // 这里指定的是修改第几行
        try {
          RandomAccessFile Raf = new RandomAccessFile("c:/test.txt", "rw");
          for (int i = 1; i < line; i++) {
            Raf.readLine();
          }
          Raf.writeBytes("111"); // 写入修改字符
        }
        catch (Exception e) {
        }
      }
    }
      

  4.   

    我之前也写了一个文件:我是通过循环的方式来达到的,我在我本地是可以正常使用,但是我移到服务器上后就没有回应了,JDK也都是在1.4以上,我不知道为什么会出现这种现像,是不是因为其它版本的包不支持呢,还是什么原因,请各位高手帮我查一下,无比郁闷中import java.io.*;public class systemConfigEdit
    {
    public static void main(String args[])
    {
    String conStr="---test---",temp;
    int rows=3;
    try{
    File file = new File("c:/test.txt");
    FileInputStream fis = new FileInputStream(file);
    InputStreamReader isr = new InputStreamReader(fis);
    BufferedReader br = new BufferedReader(isr);
    StringBuffer buf = new StringBuffer();
    for(int j=1;j < rows && (temp=br.readLine())!= null;j++){
    buf = buf.append(temp);
    buf = buf.append( System.getProperty("line.separator"));
    }
    buf = buf.append(conStr);
    br.readLine();
    while ((temp=br.readLine())!= null)
    {
    buf = buf.append( System.getProperty("line.separator"));
    buf = buf.append(temp);
    }
    br.close();
    FileOutputStream fos = new FileOutputStream(file);
    PrintWriter pw = new PrintWriter(fos);
    pw.write(buf.toString().toCharArray());
    pw.flush();
    pw.close();
    }catch (IOException e)
    {
    System.out.println(e);
    } }
    }给我个回应 ,谢谢
      

  5.   

    用readline一行行的读应该没问题的阿
      

  6.   

    是啊,我也这么想!我现在一步步测过去,发现:
    我的空间不支持或者碰到这个就发生异常,不明白为什么????????  RandomAccessFile file = new RandomAccessFile(file,"rw");难道我少了什么吗
      

  7.   

    ????楼主这个语句能成立吗?
    RandomAccessFile file = new RandomAccessFile(file,"rw");
    对象file做为文件名来源的字符串,同时又作为随机文件对象的实例,怎么可能没问题?
      

  8.   

    楼上的朋友,file变量是我发上来的时候写错了,SORRY我写了几个类似的文件,在本地均可正常编译运行,但移到我的JSP虚拟空间上时就不行了,我推测是因为空间屏蔽了对文件的写操作,大家说,我的推测对吗,能对写操作进行屏蔽吗?即不能对文件进行写的操作???
      

  9.   

    还有一种方法是将所有内容一起读进内存中,存放在数组里,变成对数组的操作,最后再写回txt文件中
    ---------
    我暂时是用这方法 ,效率很高
      

  10.   


    你完全后有没有用close关掉文件连接,如果没有关写的内容是不会保存的
    在运行完毕后用RandomAccessFile的close()方法,如果是在写的中途可以用flush()方法来完成文件保存