代码说明:为了支持中文,需要使用new InputStreamReader(new FileInputStream(filePath),"utf-8")控制中文的格式 package com.my; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Properties; public class Main { public static void main(String[] args) { try { String path = Main.class.getResource("/").getPath()+"/files/test.properties"; GetAllProperties(path); } catch (Exception e) { e.printStackTrace(); } } public static void GetAllProperties(String filePath) throws IOException { Properties pps = new Properties(); //用inputStreanReader设置中文编码 InputStreamReader is = new InputStreamReader(new FileInputStream(filePath),"utf-8"); pps.load(is); Object pwd = pps.get("db.main.password"); Object name = pps.get("db.main.username"); Object driver = pps.get("db.main.driver"); is.close(); System.out.println("密码:"+pwd+"\n"+"名字:"+name+"\n"+"驱动:"+driver); } }
代码说明:为了支持中文,需要使用new InputStreamReader(new FileI