文件内容:
      hello=hi
现在要做的事是从文件中搜索hello这个字符,找到后把hi改成world,完成后文件内容为:
   hello=world
多谢!!!

解决方案 »

  1.   

    import java.io.*;
    import java.util.*;
    class test
    {
    private String str="";
    private String strTotal="";;
    private String strReplace="";;
    test(){
    try {
            BufferedReader in = new BufferedReader(new FileReader("a.txt"));
           
            while ((str = in.readLine()) != null) {


    strTotal=strTotal+str+"\r\n";
            }
            in.close();
        } catch (IOException e) {
        }



    strReplace=strTotal.replaceAll("hi","world");

    System.out.println(strReplace);
    try {
            BufferedWriter out = new BufferedWriter(new FileWriter("a.txt"));
            out.write(strReplace);
            out.close();
        } catch (IOException e) {
        }
    }
    };
    public class pack
    { public static void main(String args[]){

    test t=new test(); }
    };
      

  2.   

    to freshman520(freshman520):
    忘记说了,我这个文件比较大,有100万条记录,内存里放不下的
      

  3.   

    使用PropertyResourceBundle类:
      ……
      public static final String BASE_PROP_FILE =
    “DISP”;
      public static final String SUFFIX =
    “.properties”;
      locale = Locale.getDefault();
      String propFile = BASE_PROP_FILE + “_” + locale.toString()+ SUFFIX;
      ResourceBundle rb;
      try {
       File file = new File(propFile);
       if (file.exists()) {
       is = new FileInputStream(file);
       rb = new PropertyResourceBundle(is);
       if (rb == null) System.out.println(“No Resource”);
       }
      } catch (IOException ioe) {
       System.out.println(“Error open file named ” + propFile);
      }
      Enumeration e = rb.getKeys();
      while (e.hasMoreElements()){
       key = (String)e.nextElement();
       value = (String)rb.handleGetObject(key);
       System.out.println(“KEY: ” + key +
    “\t\t Value: ” + value);
      }
      ……