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)
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)
解决方案 »
- 老话重提: 我看 forward 和redirect的区别,请高人check
- 新手提问,简单的一个java练习题,不知怎么修改最合理。
- 请教一道选择题
- 北京诚聘架构师、java高级工程师
- !!!JAVA接收消息的问题,参与给分,谢谢!!!!!!!!!!!!!!
- 怎样进行类型转换
- java如何实现将两个数组中不同的元素放到另一个数组中(不用集合)
- 怎么使用jad?
- 请问有哪位大侠用过jxl.jar这个包,假如一个cell是多行多列的怎么设置?
- 终于有了4个绿三角了,高兴,散分!!!顺便问个个人小问题!
- 用java的流复制文件,并改变编码格式,由ansi改为utf-8格式
- 64位win2008R2下,java内存溢出问题
PropertiesUtil.writeProperties("src/test.properties", "ye", "hhhh");
为什么加了"src/"就可以了? test.properties不也是在存在"\WebRoot\WEB-INF\classes"路径下吗?