我用Properties的getProperty()可以读取ini文件内的值,为什么用它的setProperty却不能对ini文件进行写操作啊?
求高手解答!谢谢

解决方案 »

  1.   

    SetProperty只能修改内存中的内容, 需要写到文件中才行. 自己写一个函数, 也就几十行的代码
      

  2.   

    apache上面有一个小工具可以用,叫做configure,专门用来帮助解决读写配置文件的问题,各种配置文件都可以解决
      

  3.   

    你从ini文件转成文件流FileInputStream然后用
    public void load(InputStream inStream)
              throws IOException
    方法就可以加载这个文件,进行相应的操作之后调用
    public void store(OutputStream out,
                      String comments)
               throws IOException
    方法进行保存,首先你要有一个FileOutputStream流,你让FileInputStream和FileOutputStream流都指向同一个文件就可以了
      

  4.   

    类似下面的写法FileOutputStream out = new FileOutputStream(path);
    PrintStream ps = new PrintStream(out);
    properties.setProperty(key, value);
    properties.list(ps);
    out.close();
    ps.close();
      

  5.   

    不好意思漏写了点,全帖出来public class PropertiesConfiguration {
      public String path = "" ;
      private Properties properties; 
      public PropertiesConfiguration(String file){
       //String url = this.getClass().getClassLoader().getResource("config.properties").toString();
       this.path = file;
    properties = new Properties();
      }
      public String[] getProperty(String[] key) {
        String temp[] = new String[key.length];
        try {
          FileInputStream in = new FileInputStream(path);
          properties.load(in);
          for(int i= 0 ; i < key.length ; i++){        temp[i] = properties.getProperty(key[i]).trim();
          }      in.close();
        }
        catch (IOException e) {
        }    return temp ; 
      }  public void setProperty(String[] key, String[] value) {    try {
          FileOutputStream out = new FileOutputStream(path);
          PrintStream ps = new PrintStream(out);
          for(int i = 0; i<key.length ; i++){
                    //System.out.println(value[i]);
            properties.setProperty(key[i], value[i]);
          }      properties.list(ps);
          out.close();
          ps.close();
        }
        catch (IOException e) {
         System.out.println(e.toString());
        }  }}
      

  6.   

    谢谢,我自己写了个类解决了,没有用Properties。