package test;import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;public class PropertiesUtil {
private static Properties properties = new Properties();

 public static void writeProperties(String filePath, String parameterName, String parameterValue){
        Properties props = new Properties();
        OutputStream fos=null;
        InputStream fis =null;
        try{
            InputStream in = new FileInputStream(filePath);
            props.load(in);
            System.out.println("old=" + props.getProperty(parameterName));
            OutputStream foss = new FileOutputStream(filePath);
            props.setProperty(parameterName, parameterValue);
            props.store(foss,null);
            System.out.println("new=" + props.getProperty(parameterName));
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}
package test;import java.util.Map;
public class propertiesTest {
public static void main(String[] args) {
PropertiesUtil.writeProperties("test.properties", "ye", "hhhh");
}
}test.properties文件:
ye=yxx2这样写会报错的:
java.io.FileNotFoundException: test.properties (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at test.PropertiesUtil.writeProperties(PropertiesUtil.java:50)
at test.propertiesTest.main(propertiesTest.java:8)

解决方案 »

  1.   

    InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("test.properties")
      

  2.   

    这种方式已经改掉了只不过改的是class的路径下的test.properties而不是楼主要的src目录下的
      

  3.   

    如果你要修改src目录下的那么你方法调用的时候传的路径应该是
    PropertiesUtil.writeProperties("src/test.properties", "ye", "hhhh");
      

  4.   


    为什么加了"src/"就可以了? test.properties不也是在存在"\WebRoot\WEB-INF\classes"路径下吗?
      

  5.   

    你的方法里面是直接new FileInputStream(filepath)的这个的路径是当前的项目目录不是classes目录