我有一个文件oracle_publicsg.properties
这个文件中有serverName和port两个属性Properties settings = new Properties();
InputStream in = ClassLoaderUtil.getResourceAsStream(“oracle_publicsg.properties”, null); //这个方法是我写的可以返回流的。是正确的
settings.load(in);
settings.getProperty("port"); --打出来的结果是对的settings.setProperty("serverName",dbAddress);
settings.setProperty("port",port);但是
我要保存对这两个文件的修改,该怎么做?

解决方案 »

  1.   

    Properties settings = new Properties();
    InputStream in = ClassLoaderUtil.getResourceAsStream(“oracle_publicsg.properties”, null); settings.load(in);
    settings.put("serverName",dbAddress);
    settings.put("port",port);
    楼主试试看,记得是这样的饿!
      

  2.   

    忘记settings.close();了!呵呵!
      

  3.   

    你没写。肯定保存不了修改嘛。。settings.setProperty并不是写到文件中。只是把这个结果保存到了这个对象中。并没有写到文件中。
      

  4.   

    public class fileWrite {
    private String filepath="/uk/com/uid/example/in.properties";
    public void readFile(String inString){
    File in=new File(filepath);
    String[] tmp=inString.split("\\|");
    try {
    FileInputStream filein=new FileInputStream(in);
    Properties file=new Properties();
    file.load(filein);
    file.put("name",tmp[0]);
    file.put("id",tmp[1]);
    file.put("result",tmp[2]);
    file.put("re",tmp[3]);
    file.put("data",tmp[4]);
    filein.close();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }public static void main(String[] args){
    fileWrite fw=new fileWrite();
    fw.readFile("a|b|c|d|e");
    }
    }试试这个看你就会明白了!呵呵!
      

  5.   

    public static void main(String[] args) {
    Properties settings = new Properties();
    try {
    settings.load(new FileInputStream("oracle_publicsg.properties"));
    System.out.println(settings.getProperty("serverName"));
    settings.setProperty("serverName","3");


    PrintWriter out=new PrintWriter(new FileOutputStream("oracle_publicsg.properties"));
    out.print(settings);
          out.flush();
          out.close();
    } catch (FileNotFoundException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
    } catch (IOException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
    }
    }
      

  6.   

    File in = new File(fileName);
    FileInputStream fileIn = new FileInputStream(in);
    Properties settings = new Properties();
    settings.load(fileIn);
    System.out.println("serverName from file" + settings.getProperty("serverName"));
    settings.put("serverName",dbAddress);
    settings.put("port",port);
    settings.put("username",userName);
    settings.put("password",userPwd);

    fileIn.close();
      

  7.   

    不能put吧。如果他文件里面有这个属性了,在一put,不就重复了吗?会写2个出来。Properties extends Hashtable吧。。
      

  8.   

    对啊,所以我用setProperty()方法呀。奇怪的时,对已存在的文件进行修改就是
    办不到!我很郁闷
      

  9.   

    setProperty()方法其实也是调用的put,我刚发上去的不行么?我试了是可以的,只是格式有点问题,其实你自己可以写个setProperty()方法,我写Properties文件是用的自己写的方法。
      

  10.   

    settings.store(OutputStream out, String header)
    document 是这样的