知乎专栏 |
目录
package netkiller.test; import java.io.FileNotFoundException; import java.io.IOException; import java.io.FileInputStream; import java.util.Properties; public class PropertiesTest { public static void main(String[] args) { System.out.println("Working Directory = " + System.getProperty("user.dir")); Properties ps = new Properties(); try { ps.load(new FileInputStream("netkiller.properties")); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println(ps.getProperty("key")); } }
BufferedReader br = null; Properties properties = new Properties(); br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("data.properties")), "UTF8")); properties.load(br);
package cn.netkiller.properties; import java.util.Enumeration; import java.util.Map.Entry; import java.util.Properties; public class PropertiesTest { public PropertiesTest() { // TODO Auto-generated constructor stub } public static void main(String[] args) { // TODO Auto-generated method stub Properties properties = new Properties(); properties.put("K1", "V1"); properties.put("K2", "V2"); for (Entry<Object, Object> x : properties.entrySet()) { System.out.println(x.getKey() + " " + x.getValue()); } Enumeration<?> e = properties.propertyNames(); while (e.hasMoreElements()) { String key = (String) e.nextElement(); String value = properties.getProperty(key); System.out.println(key + ": " + value); } } }
import java.io.FileInputStream; import java.util.Enumeration; import java.util.Properties; public class MainClass { public static void main(String args[]) throws Exception { Properties p = new Properties(); p.load(new FileInputStream("test.properties")); Enumeration e = p.propertyNames(); for (; e.hasMoreElements();) { System.out.println(e.nextElement()); } } }
package cn.netkiller.properties; import java.util.Properties; import java.util.Set; public class PropertiesTest { public PropertiesTest() { // TODO Auto-generated constructor stub } public static void main(String[] args) { // TODO Auto-generated method stub Properties properties = new Properties(); properties.put("K1", "V1"); properties.put("K2", "V2"); Set<Object> states = properties.keySet(); for (Object name : states) System.out.println("The value of " + name + " is " + properties.getProperty((String) name) + "."); } }
package cn.netkiller.properties; import java.util.Map.Entry; import java.util.Properties; public class PropertiesTest { public PropertiesTest() { // TODO Auto-generated constructor stub } public static void main(String[] args) { // TODO Auto-generated method stub Properties properties = new Properties(); properties.put("K1", "V1"); properties.put("K2", "V2"); for (Entry<Object, Object> x : properties.entrySet()) { System.out.println(x.getKey() + " " + x.getValue()); } } }
@RequestMapping("/host") public Enumeration<Object> host() throws IOException { Properties properties = PropertiesLoaderUtils.loadProperties(new ClassPathResource(String.format("/%s.properties", "host"))); return properties.keys(); } @RequestMapping("/mail") public Collection<Object> mail() throws IOException { Properties properties = PropertiesLoaderUtils.loadProperties(new ClassPathResource(String.format("/%s.properties", "mail"))); return properties.values(); } @RequestMapping("/nameserver") public Set<Entry<Object, Object>> nameserver() throws IOException { Properties properties = PropertiesLoaderUtils.loadProperties(new ClassPathResource(String.format("/%s.properties", "dns"))); return properties.entrySet(); } @RequestMapping("/dns") public Properties dns() throws IOException { Properties properties = PropertiesLoaderUtils.loadProperties(new ClassPathResource(String.format("/%s.properties", "dns"))); return properties; }
Properties prop = new Properties(); prop.load(getServletContext().getResourceAsStream("/WEB-INF/resource/sample.properties"));
prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("netkiller.properties"));
package cn.netkiller.config; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Properties; public class Config { public static void main(String[] args) { // TODO Auto-generated method stub Properties prop = new Properties(); OutputStream output = null; try { output = new FileOutputStream("config.properties"); // set the properties value prop.setProperty("host", "localhost"); prop.setProperty("port", "8000"); prop.setProperty("user", "neo"); prop.setProperty("pass", "password"); // save properties to project root folder prop.store(output, null); } catch (IOException io) { io.printStackTrace(); } finally { if (output != null) { try { output.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
getProperty 取出key的值
package cn.netkiller.config; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class LoadConfig { public static void main(String[] args) { // TODO Auto-generated method stub Properties prop = new Properties(); InputStream input = null; try { input = new FileInputStream("config.properties"); // load a properties file prop.load(input); // get the property value and print it out System.out.println(prop.getProperty("host")); System.out.println(prop.getProperty("port")); System.out.println(prop.getProperty("user")); System.out.println(prop.getProperty("pass")); } catch (IOException ex) { ex.printStackTrace(); } finally { if (input != null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
循环打印所有 Properties 内容
package test; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import java.util.Properties; public class Application { public static void main(String[] args) { Application app = new Application(); app.config(); } private void config() { Properties prop = new Properties(); InputStream input = null; try { String filename = "config.properties"; prop.load(new FileInputStream(filename)); Enumeration<?> e = prop.propertyNames(); while (e.hasMoreElements()) { String key = (String) e.nextElement(); String value = prop.getProperty(key); System.out.println(key + ": " + value); } } catch (IOException ex) { ex.printStackTrace(); } finally { if (input != null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
准备语言包文件 chinese.properties 内容如下
hello=你好世界
english.properties
hello=Helloworld
@GetMapping("/lang") public String lang(@RequestHeader String lang) throws IOException { System.out.println(lang); Properties properties = PropertiesLoaderUtils.loadProperties(new ClassPathResource(lang + ".properties")); String tmp = properties.getProperty("hello"); return tmp; }
测试效果
curl -s -H lang:chinese http://localhost:8080/lang 你好世界 curl -s -H lang:english http://localhost:8080/lang Helloworld