Welcome 微信登录

首页 / 操作系统 / Linux / Java读取资源文件

Properties props = new Properties();

props.load(new FileInputStream("db.properties")); //相对src的路径

String s = props.getProperty(key);========================================================
  1. import java.io.IOException;   
  2. import java.io.InputStream;   
  3. import java.util.Enumeration;   
  4. import java.util.Properties;   
  5.   
  6. public class Spike {   
  7.     public static void main(String[] args) throws Exception {   
  8.          readProperties();   
  9.          System.out.println("==================================================");   
  10.          readXml();   
  11.      }   
  12.   
  13.     private static void readProperties() throws IOException {   
  14.          Properties props = new Properties();   
  15.          InputStream inStream = Spike.class.getResourceAsStream("Mock.properties");   
  16.          props.load(inStream);   
  17.          Enumeration enums = props.propertyNames();   
  18.         while (enums.hasMoreElements()) {   
  19.              String key = (String) enums.nextElement();   
  20.              System.out.println("Property--->>>>[" + key + "]     " + "Value--->>>>" + props.getProperty(key));   
  21.          }   
  22.      }   
  23.        
  24.     private static void readXml() throws IOException {   
  25.          Properties props = new Properties();   
  26.          InputStream inStream = Spike.class.getResourceAsStream("Mock.xml");   
  27.          props.loadFromXML(inStream);   
  28.          Enumeration enums = props.propertyNames();   
  29.         while (enums.hasMoreElements()) {   
  30.              String key = (String) enums.nextElement();   
  31.              System.out.println("Property--->>>>[" + key + "]     " + "Value--->>>>" + props.getProperty(key));   
  32.          }   
  33.      }   
  34. }  
=======================================================================1、web项目中读properties文件,一般放到class根目录下web/WEB-INF/classes/
  1. public static String FILE_PATH = "/comms.properties";   
  2. private InputStream inputFile;   
  3. private Properties propertie;   
  4.   
  5. propertie = new Properties();   
  6. inputFile = getClass().getResourceAsStream(FILE_PATH);   
  7. propertie.load(inputFile);   
  8. 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文件写入时,需要获取绝对路径才能写入操作
  1. public void saveFile(String fileName, String description)   
  2.      {   
  3.          String filePath = getClass().getResource("/").getPath()+fileName;   
  4.          File file = new File(filePath);     
  5.         try {   
  6.              outputFile = new FileOutputStream(file);   
  7.              propertie.store(outputFile, description);   
  8.              outputFile.close();   
  9.          } catch (FileNotFoundException e) {   
  10.              e.printStackTrace();   
  11.          } catch (IOException ioe){   
  12.              ioe.printStackTrace();   
  13.          }   
  14.      }  
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的根目录