Properties props = new Properties();
props.load(new FileInputStream("db.properties")); //相对src的路径
String s = props.getProperty(key);========================================================
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Enumeration;
- import java.util.Properties;
-
- public class Spike {
- public static void main(String[] args) throws Exception {
- readProperties();
- System.out.println("==================================================");
- readXml();
- }
-
- private static void readProperties() throws IOException {
- Properties props = new Properties();
- InputStream inStream = Spike.class.getResourceAsStream("Mock.properties");
- props.load(inStream);
- Enumeration enums = props.propertyNames();
- while (enums.hasMoreElements()) {
- String key = (String) enums.nextElement();
- System.out.println("Property--->>>>[" + key + "] " + "Value--->>>>" + props.getProperty(key));
- }
- }
-
- private static void readXml() throws IOException {
- Properties props = new Properties();
- InputStream inStream = Spike.class.getResourceAsStream("Mock.xml");
- props.loadFromXML(inStream);
- Enumeration enums = props.propertyNames();
- while (enums.hasMoreElements()) {
- String key = (String) enums.nextElement();
- System.out.println("Property--->>>>[" + key + "] " + "Value--->>>>" + props.getProperty(key));
- }
- }
- }
=======================================================================1、web项目中读properties文件,一般放到class根目录下web/WEB-INF/classes/
- public static String FILE_PATH = "/comms.properties";
- private InputStream inputFile;
- private Properties propertie;
-
- propertie = new Properties();
- inputFile = getClass().getResourceAsStream(FILE_PATH);
- propertie.load(inputFile);
- inputFile.close();
public static String FILE_PATH = "/comms.properties";private InputStream inputFile;private Properties propertie;propertie = new Properties();inputFile = getClass().getResourceAsStream(FILE_PATH);propertie.load(inputFile);inputFile.close();2、向properties文件写入时,需要获取绝对路径才能写入操作
- public void saveFile(String fileName, String description)
- {
- String filePath = getClass().getResource("/").getPath()+fileName;
- File file = new File(filePath);
- try {
- outputFile = new FileOutputStream(file);
- propertie.store(outputFile, description);
- outputFile.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException ioe){
- ioe.printStackTrace();
- }
- }
public void saveFile(String fileName, String description){ String filePath = getClass().getResource("/").getPath()+fileName; File file = new File(filePath);try {outputFile = new FileOutputStream(file);propertie.store(outputFile, description);outputFile.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException ioe){ioe.printStackTrace();}}=================================================================================package com.ks.tools;import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;/**
* @author 李英夫
* @ClassName PropertiesLoader
* @Version
* @ModifiedBy
* @Copyright @ 2010 H&L Technology Inc.
* @date 2010-1-18 上午09:17:13
* @description 资源文件读取器
*/
public class PropertiesLoader {
//properties文件路径
private static final String PROPERTY_LOCATION = "properties/util.properties";
/**
* 根据Key找value,默认的文件路径是properties/util.properties
* @author 李英夫(2010-1-18 上午10:54:39)
* @param key
* @return
* @throws IOException String
*/
public static String getText(String key)throws IOException{
Properties props = new Properties();
String filePath = PropertiesLoader.class.getResource("/").getPath()+PROPERTY_LOCATION;
filePath = filePath.replace("%20"," ");
props.load(new FileInputStream(filePath));
return props.getProperty(key);
}
/**
* 根据Key和url找value
* @author 李英夫(2010-1-18 上午10:57:43)
* @param key
* @param url
* @return
* @throws IOException String
*/
public static String getText(String key, String url)throws IOException{
Properties props = new Properties();
String filePath = PropertiesLoader.class.getResource("/").getPath()+url;
props.load(new FileInputStream(filePath));
return props.getProperty(key);
}}
getClass().getResource("/").getPath() 为获取class的根目录