Java读取properties配置文件
1 import java.io.BufferedReader; 2 import java.io.IOException; 3 import java.io.InputStream; 4 import java.io.InputStreamReader; 5 import java.io.UnsupportedEncodingException; 6 import java.util.Enumeration; 7 import java.util.Iterator; 8 import java.util.Properties; 9 import java.util.Set; 10 11 import javax.servlet.ServletContext; 12 import javax.servlet.ServletException; 13 import javax.servlet.annotation.WebServlet; 14 import javax.servlet.http.HttpServlet; 15 16 @WebServlet(urlPatterns={"/PropertiesServlet"},loadOnStartup=1) 17 public class PropertiesServlet extends HttpServlet { 18 19 private static final long serialVersionUID = 1L; 20 21 public PropertiesServlet() { 22 super(); 23 } 24 25 public void init()throws ServletException{ 26 27 // 获取 ServletContext 28 ServletContext context = this.getServletContext(); 29 // 获取 ServletConfig 30 //ServletConfig servletConfig = this.getServletConfig(); 31 32 // 获取所有初始化参数的名称 33 Enumeration<String> enumer = context.getInitParameterNames(); 34 //Enumeration<String> initParameterNames = servletConfig.getInitParameterNames();// 同样是获取所有初始化参数的名称 35 //enumer = initParameterNames; 36 37 while (enumer.hasMoreElements()) { 38 String name = enumer.nextElement(); 39 String value = context.getInitParameter(name);// 通过参数名获取参数值 40 System.out.println(name + ":" + value); 41 } 42 43 44 // 通过 ServletContext 获取资源文件输入流 InputStream 的三种方式 45 //URL url = context.getResource("WEB-INF/classes/jdbc.properties"); 46 //InputStream is1 = url.openStream(); 47 48 //String path = context.getRealPath("WEB-INF/classes/jdbc.properties"); 49 //File file = new File(path); 50 //InputStream is2 = new FileInputStream(file); 51 52 InputStream is3 = context.getResourceAsStream("WEB-INF/classes/jdbc.properties");//方便 53 54 // 获取 ClassLoader 的三种方式 55 //ClassLoader classLoader1 = this.getClass().getClassLoader(); 56 //ClassLoader classLoader2 = Thread.currentThread().getContextClassLoader(); 57 //ClassLoader classLoader3 = ClassLoader.getSystemClassLoader(); 58 59 // 通过 ClassLoader 获取 InputStream 60 //InputStream is = classLoader3.getResourceAsStream("jdbc.properties"); 61 62 //InputStream is4 = ClassLoader.getSystemResourceAsStream("jdbc.properties");//方便 63 64 Properties prop = new Properties(); 65 66 //Java在网络传输中使用的编码是"ISO-8859-1",如果配置文件有中文,可能出现乱码问题 67 //解决方案:1.native2ascii.exe将中文转成Unicode编码;2.将InputStream封装成Reader 68 //InputStream字节流无法读取中文,把InputStream转换成Reader字符流,将字节解码为字符来读取中文 69 InputStreamReader isr = null; 70 try { 71 isr = new InputStreamReader(is3, "UTF-8"); 72 } catch (UnsupportedEncodingException e) { 73 e.printStackTrace(); 74 } 75 //其中UTF-8,用于明确指定解码字符流的字符集 76 //不指定则默认使用OS的,这会造成同一份配置文件同一份代码,在linux和windows上、英文windows和中文windows之间的表现都不一致 77 //这个参数应该和具体读取的properties文件的编码格式匹配 78 79 80 //建议用 BufferedReader 包装所有其 read() 操作可能开销很高的 Reader(如 FileReader 和 InputStreamReader) 81 //如果不缓冲的话会通过read方法一次又一次的访问文件 82 BufferedReader bf = new BufferedReader(isr); 83 84 //将内容读到 Properties 里 85 try { 86 prop.load(bf); 87 } catch (IOException e) { 88 // TODO Auto-generated catch block 89 e.printStackTrace(); 90 } 91 92 String user = prop.getProperty("姓名"); 93 System.out.println(user); 94 95 // 获取所有Property 的 Name 96 Set<String> set = prop.stringPropertyNames(); 97 Iterator<String> it = set.iterator(); 98 while (it.hasNext()) { 99 String name = it.next(); 100 String value = prop.getProperty(name);// 通过name获取value 101 System.out.println(name + ":" + value); 102 } 103 } 104 }
posted on 2016-08-22 02:03 guodefu909 阅读(342) 评论(0) 编辑 收藏 举报