我的test.properties 文件内容是这样的
logonActionForm.username.displayname=UserName:
logonActionForm.password.displayname=Password:logonActionForm.username.msg=Error UserName
logonActionForm.password.msg=Error Passworderrors.required={0} is required.
理想效果是 读出后再加一条属性进去;但是现在写进去后变成以下情况 ,请问下该如何解决:logonActionForm.password.displayname=Password\:
gid=10000000
errors.required={0} is required.
logonActionForm.password.msg=Error Password
logonActionForm.username.msg=Error UserName
logonActionForm.username.displayname=UserName\:
我的代码:import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Enumeration;
import java.util.Properties;public class Test {
public static void main(String[] args) {
String filePath = "E:\\test.properties";
Test t = new Test();
t.getContext(filePath);
System.out.println("============================");
t.setContext(filePath, "gid", "10000000");
System.out.println("============================");
t.getContext(filePath);
}
Properties props = new Properties();
//读取properties文件的信息
public void getContext(String filePath) {
InputStreamReader reader = null;
try {
reader = new InputStreamReader(
new FileInputStream(filePath),"GBK");
props.load(reader);
Enumeration en = props.propertyNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String property = props.getProperty(key);
System.out.println(key + "\t" + property);
}
} catch (Exception e) {
// TODO: handle exception
System.out.println("读取文件异常");
e.printStackTrace();
}finally{
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//给properties文件写入指定信息
public void setContext(String filePath,String key,String value){
InputStreamReader reader = null;
try {
reader = new InputStreamReader(
new FileInputStream(filePath),"GBK");
props.load(reader);
props.setProperty(key, value);
props.store(new FileOutputStream(filePath),null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private OutputStreamWriter OutputStreamWriter(
FileOutputStream fileOutputStream) {
// TODO Auto-generated method stub
return null;
}
}
logonActionForm.username.displayname=UserName:
logonActionForm.password.displayname=Password:logonActionForm.username.msg=Error UserName
logonActionForm.password.msg=Error Passworderrors.required={0} is required.
理想效果是 读出后再加一条属性进去;但是现在写进去后变成以下情况 ,请问下该如何解决:logonActionForm.password.displayname=Password\:
gid=10000000
errors.required={0} is required.
logonActionForm.password.msg=Error Password
logonActionForm.username.msg=Error UserName
logonActionForm.username.displayname=UserName\:
我的代码:import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Enumeration;
import java.util.Properties;public class Test {
public static void main(String[] args) {
String filePath = "E:\\test.properties";
Test t = new Test();
t.getContext(filePath);
System.out.println("============================");
t.setContext(filePath, "gid", "10000000");
System.out.println("============================");
t.getContext(filePath);
}
Properties props = new Properties();
//读取properties文件的信息
public void getContext(String filePath) {
InputStreamReader reader = null;
try {
reader = new InputStreamReader(
new FileInputStream(filePath),"GBK");
props.load(reader);
Enumeration en = props.propertyNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String property = props.getProperty(key);
System.out.println(key + "\t" + property);
}
} catch (Exception e) {
// TODO: handle exception
System.out.println("读取文件异常");
e.printStackTrace();
}finally{
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//给properties文件写入指定信息
public void setContext(String filePath,String key,String value){
InputStreamReader reader = null;
try {
reader = new InputStreamReader(
new FileInputStream(filePath),"GBK");
props.load(reader);
props.setProperty(key, value);
props.store(new FileOutputStream(filePath),null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private OutputStreamWriter OutputStreamWriter(
FileOutputStream fileOutputStream) {
// TODO Auto-generated method stub
return null;
}
}
解决方案 »
- 开个帖子,分享我的博客,java程序员从笨鸟到菜鸟系列博客,每天持续更新,更新马上通知本帖!!!
- struts2怪问题,在Firefox下正常,在IE下就不正常了
- SSH整合出现java.lang.NullPointerException,在线等!
- hibernate求解
- 高手帮忙解决下
- 急!!如何能统计页面的点击次数!
- 我是菜鸟:tomcat装好后可以显示主界面,自带的例子也可以运行,为什么我的程序解析不了呢
- 怎样通过一个类返回一个EJB实体?(工厂方法模式结合EJB)
- 關於struts中logic的用法.
- SSM框架用Junit进行测试,一直报错,JDK必须用1.6的,有大神能帮我解决下问题吗?
- ajax怎么传一个JSON都JAVA后台?
- 请教一个sql语句
http://www.iteye.com/problems/13258
logonActionForm.password.displayname=Password:
errors.required={0} is required.
logonActionForm.password.msg=Error Password
logonActionForm.username.msg=Error UserName
logonActionForm.username.displayname=UserName:预期结果:logonActionForm.password.displayname=Password:
gid=10000000
errors.required={0} is required.
logonActionForm.password.msg=Error Password
logonActionForm.username.msg=Error UserName
logonActionForm.username.displayname=UserName:每次都会把 Password: 变成 : Password\:怎么处理这种情况?
public static void write(File file, String key, String value) {
try {
FileOutputStream fos = new FileOutputStream(file);
Properties p = new Properties();
p.setProperty(key, value);
p.store(fos, null);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}