如何读取properties文件
我有一个购物网站每天的下单量会以日期命名自动生成1个properties文件(如:20081225.properties,20081226.properties),我想在页面显示properties文件中的下单量,根据系统时间显示当天的下单量,怎么显示?请高手指点
[解决办法]
import java.io.BufferedInputStream;
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.Properties;
public class TestMain {
//根据key读取value
public static String readValue(String filePath,String key) {
Properties props = new Properties();
try {
InputStream in = new BufferedInputStream (new FileInputStream(filePath));
props.load(in);
String value = props.getProperty (key);
System.out.println(key+value);
return value;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
//读取properties的全部信息
public static void readProperties(String filePath) {
Properties props = new Properties();
try {
InputStream in = new BufferedInputStream (new FileInputStream(filePath));
props.load(in);
Enumeration en = props.propertyNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String Property = props.getProperty (key);
System.out.println(key+Property);
}
} catch (Exception e) {
e.printStackTrace();
}
}
//写入properties信息
public static void writeProperties(String filePath,String parameterName,String parameterValue) {
Properties prop = new Properties();
try {
InputStream fis = new FileInputStream(filePath);
//从输入流中读取属性列表(键和元素对)
prop.load(fis);
//调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
//强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
OutputStream fos = new FileOutputStream(filePath);
prop.setProperty(parameterName, parameterValue);
//以适合使用 load 方法加载到 Properties 表中的格式,
//将此 Properties 表中的属性列表(键和元素对)写入输出流
prop.store(fos, "Update '" + parameterName + "' value");
} catch (IOException e) {
System.err.println("Visit "+filePath+" for updating "+parameterName+" value error");
}
}
public static void main(String[] args) {
readValue("info.properties","url");
writeProperties("info.properties","age","21");
readProperties("info.properties" );
System.out.println("OK");
}
}
[解决办法]
用ResourceBundle 啊。。给你找个例子:如先在有src下有个messages.properties
有数据:
greetings="22"
import java.util.ResourceBundle; public class Untitled1 { public static void main(String[] args) { ResourceBundle messages = ResourceBundle.getBundle("mesages"); String message = messages.getString("greetings"); System.out.println(message); //22 } }
[解决办法]
我不确定我src下的properties文件名称啊 只能根据服务器系统时间去读,如:今天25日就读20081225.properties,26日就读20081226.properties,还有怎么在页面显示,页面是不是还得用Ajax啊?
1. 先取得服务器时间
Date time = getServerSystemTime();
2. 将服务器时间转换成你需要的格式"yyyyMMdd"的字符串
String timeStr = getDateString(time);
3. 用这个字符串作为参数获取properties文件
ResourceBundle messages = ResourceBundle.getBundle(timeStr);
[解决办法]
两种方式:
第一个:采用java.util.Properties来操作
Properties p = new Properties();
FileInputStream fi = new FileInputStream(new File("文件绝对路径"));
p.load(fi);
fi.close();
然后通过p.get("这里传入在properties定义的KEY就可以了"); 返回是Object对象 可以自己转换
第二个:apache的commons对这个Properties封装操作 使用起来更简单:
import org.apache.commons.configuration.PropertiesConfiguration;
PropertiesConfiguration config = new PropertiesConfiguration();
config.load("这里有很多个重载分别可以传入{File,InputStream,Reader,URL,InputStream和编码字符串,直接文件名("test.properties",但是必须在classpath下)}");
config.getString("这里传入在properties定义的KEY"); 就可以获取String类型的
config.getInt("这里传入在properties定义的KEY"); 就可以获取int类型的
其他的自己去找! 第二种需要commons-configuration-1.2.jar
[解决办法]
Properties prop = new Properties()
prop.load(LocalDBConnection.class.getResourceAsStream("properties文件绝对路径"));
prop.getProperty("你要取的Name");