package com.lxk.propertyFileTest;
import java.io.*;
import java.util.Properties;
/**
* 读写properties文件测试
* <p>
* Created by lxk on 2017/4/25
*/
public class Main {
public static void main(String[] args) {
Properties prop = new Properties();
InputStream in = null;
FileOutputStream oFile = null;
try {
in = new BufferedInputStream(new FileInputStream("D:config.properties"));
//prop.load(in);//直接这么写,如果properties文件中有汉子,则汉字会乱码。因为未设置编码格式。
prop.load(new InputStreamReader(in, "utf-8"));
for (String key : prop.stringPropertyNames()) {
System.out.println(key + ":" + prop.getProperty(key));
}
//保存属性到b.properties文件
oFile = new FileOutputStream("b.properties", false);//true表示追加打开,false每次都是清空再重写
prop.setProperty("phone", "10086");
//prop.store(oFile, "此参数是保存生成properties文件中第一行的注释说明文字");//这个会两个地方乱码
//prop.store(new OutputStreamWriter(oFile, "utf-8"), "汉字乱码");//这个就是生成的properties文件中第一行的注释文字乱码
prop.store(new OutputStreamWriter(oFile, "utf-8"), "lll");
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
if (oFile != null) {
try {
oFile.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
}
}
package com.lxk.propertyFileTest;
import java.io.*;
import java.util.Properties;
/**
* 读写properties文件测试
* <p>
* Created by lxk on 2017/4/25
*/
public class Main {
public static void main(String[] args) {
Properties prop = readPropertiesFile();
writePropertiesFile(prop);
}
/**
* 读Properties文件
*/
private static Properties readPropertiesFile() {
Properties prop = new Properties();
InputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream("D:config.properties"));
//prop.load(in);//直接这么写,如果properties文件中有汉子,则汉字会乱码。因为未设置编码格式。
prop.load(new InputStreamReader(in, "utf-8"));
for (String key : prop.stringPropertyNames()) {
System.out.println(key + ":" + prop.getProperty(key));
}
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
return prop;
}
/**
* 写Properties文件
*/
private static void writePropertiesFile(Properties prop) {
prop.setProperty("phone", "10086");
FileOutputStream oFile = null;
try {
//保存属性到b.properties文件
oFile = new FileOutputStream("b.properties", false);//true表示追加打开,false每次都是清空再重写
//prop.store(oFile, "此参数是保存生成properties文件中第一行的注释说明文字");//这个会两个地方乱码
//prop.store(new OutputStreamWriter(oFile, "utf-8"), "汉字乱码");//这个就是生成的properties文件中第一行的注释文字乱码
prop.store(new OutputStreamWriter(oFile, "utf-8"), "lll");
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
if (oFile != null) {
try {
oFile.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有