java獲取Properties檔案中的值
阿新 • • 發佈:2019-02-02
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class PropertyDemo { public static Properties method1() { //注:配置檔案需與類檔案同目錄 Properties prop = new Properties(); InputStream in = PropertyDemo.class.getResourceAsStream("a.properties"); // PropertyDemo loadProp = new PropertyDemo(); // InputStream in = loadProp.getClass().getResourceAsStream("a.properties"); try { prop.load(in); } catch (Exception e) { e.printStackTrace(); } return prop; } public static Properties method2() { Properties prop = new Properties(); try { InputStream in = new FileInputStream(new File("D:/share/tenchong/vchat/Test/config/a.properties")); prop.load(in); } catch (IOException e) { e.printStackTrace(); } return prop; } }