标签:blog http io ar sp java 文件 on div
properties文件也叫资源文件,以键值对的形式存放文本内容。一个properties对象代表一个资源文件
步骤:
1、生成properties对象
2、生成InputStream/Reader来读取properties文件的内容
3、通过load方法把InputSteram/Reader装载到properties对象中来
4、调用properties对象的get方法:通过键,获取值
key1=\u6D4B\u8BD5 key2=test
package chavez;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.util.Properties;
public class Test {
public static void main(String[] args) {
Properties ps = new Properties();
//获取工程文件夹的路径
String projectPath = System.getProperty("user.dir");
//获取properties的路径
String propertiesPath = projectPath + File.separator + "resources"
+ File.separator + "first.properties";
System.out.println(propertiesPath);
try {
Reader reader = new FileReader(propertiesPath);
ps.load(reader);
// InputStream in = new FileInputStream(propertiesPath);
// ps.load(in);
System.out.println(ps.get("key1"));
System.out.println(ps.get("key2"));
} catch (Exception e) {
e.printStackTrace();
}
}
}

properties文件中的键可以任意命名,值可以输入中文,但会被eclipse自动用unicode进行编码
标签:blog http io ar sp java 文件 on div
原文地址:http://www.cnblogs.com/chavez-wang/p/4119816.html