从文件读出数据,放为VECTOR,或者hash*类的东西,修改后再覆盖原来的~

解决方案 »

  1.   

    用java.io.RandomAccessFile。
    RandomAccessFile(File file, String mode),mode为"rw",可以以追加模式打开文件。
    seek(long l)可以实现文件光标定位。
      

  2.   

    同意楼上的方法,不过建议把每行都应该是相等的字符,这样用seek()就比较方便定位在某一行上。不够的可以补空格,读取的时候可以用StringTokenize类进行出去,并用trim()去掉多余空格。
      

  3.   

    lixiang823517大哥:
                    不会吧,取出来再放回去,如果我有很多数据呢?
      

  4.   

    Danger2000(飞鱼)大哥,能否给点定位的代码就像,将文件指针定位于23的2的位置,谢谢大哥了
      

  5.   

    to  rainboy20024(鹰鹰) :long lineLength = 每行的长度;
    long nameLength = 姓名部分的长度;
    in.seek(lineLength * 2 + nameLength +1);  //把指针定位在23的2的位置前
      

  6.   

    可以跳过已经写入的文件长度再写试试
    import java.io.*;public class myFile
    {
    public myFile()
    {
    init();
    RecordAppend("xuguo");
    }

    public void init()
    {
    try
    {
      String Record="zhoukun";
              String myFilePath="D:/jspServer";
          String myFileName="myWriteFile.txt";
          File dir=new File(myFilePath,"newFile");
          dir.mkdir();
          File f=new File(dir,myFileName);
          FileOutputStream o=new FileOutputStream(f);
          BufferedOutputStream myin=new BufferedOutputStream(o);
          byte c[]=Record.getBytes();
          myin.write(c);
          myin.flush();
          myin.close();
        }
        catch(IOException e)
        {
         System.out.println(e);
        }
    }

    public void RecordAppend(String strAppend)
    {
    try
    {
      int filelength;
      int writelength;
              String myFilePath="D:/jspServer/newFile";
          String myFileName="myWriteFile.txt";
          File f=new File(myFilePath,myFileName);
          FileOutputStream o=new FileOutputStream(f);
          BufferedOutputStream myin=new BufferedOutputStream(o);
          byte c[]=strAppend.getBytes();
          filelength=(int)f.length();
          writelength=strAppend.length();
          
          myin.write(c,filelength,writelength);
          myin.flush();
          myin.close();
        }
        catch(IOException e)
        {
         System.out.println(e);
        }
    }

    public static void main(String args[])
    {
    new myFile();
    }
    }
      

  7.   

    谢谢大家,还麻烦大家一下:
      如果我要用文件进行存储,并在图形界面进行查询,譬如:我要查询"小唐"该怎么找啊,用VECTOR,或者 hash* 之类吗?还是读取每行,并进行切割,比较?