java 操作属性_java 操作属性文件

public class Test { public static void main(String[] args) { systenGet(); filestreamGet(); filestreamGetSrc(); classloaderGet(); classLoadUTFGet(); init(); setProParams(); } /** * system读取propertise文件 / public static void systenGet(){ Properties properties=System.getProperties(); System.out.println(properties.getProperty("user.dir")); System.out.println("全局系统参数key_______:"+System.getProperty("000000")); } /* * 属相文件信息 / public static void filestreamGet() { Properties p = new Properties(); try { FileInputStream fileInputStream = new FileInputStream("test.properties"); p.load(fileInputStream); System.out.println("工程下___:" + p.getProperty("000000")); fileInputStream.close(); } catch (Exception e) { e.printStackTrace(); } } public static void filestreamGetSrc() { Properties p = new Properties(); try { FileInputStream fileInputStream = new FileInputStream("test.properties"); p.load(fileInputStream); System.out.println("工程src下___:"+p.getProperty("000000")); fileInputStream.close(); } catch (Exception e) { e.printStackTrace(); } } /* * classloader读取属性文件 / public static void classloaderGet(){ ClassLoader load=ClassLoader.getSystemClassLoader(); Properties p=new Properties(); try { p.load(load.getResourceAsStream("code.properties")); } catch (IOException e) { e.printStackTrace(); } System.out.println(p.getProperty("000000")); } /* * 读取properties 文件乱码解决方法 / public static void classLoadUTFGet(){ ClassLoader load=ClassLoader.getSystemClassLoader(); Properties p=new Properties(); try { p.load(new InputStreamReader(load.getResourceAsStream("code.properties"), "UTF-8")); } catch (IOException e) { e.printStackTrace(); } System.out.println("解决中文乱码的方法___:"+p.getProperty("000000")); } public static void init(){ try { Properties prop = new Properties(); FileInputStream fis = null; fis = new FileInputStream("src/config.properties"); prop.load(fis); Iterator> itr = prop.entrySet().iterator(); while (itr.hasNext()) { Entry e = itr.next(); System.setProperty(e.getKey().toString(), e.getValue().toString()); } } catch (Exception e) { e.printStackTrace(); } } /* * 项目启动时 加载属性文件数据到系统内存中 */ public static void setProParams(){ try { ClassLoader load=ClassLoader.getSystemClassLoader(); Properties properties=new Properties(); properties.load(new InputStreamReader(load.getResourceAsStream("config.properties"), "UTF-8")); Iterator>iterator=properties.entrySet().iterator(); while(iterator.hasNext()){ Entry e = iterator.next(); System.out.println("建本地属性文件写到内存中___:{"+e.getKey().toString()+"="+e.getValue().toString()+"}"); System.setProperty(e.getKey().toString(), e.getValue().toString()); } System.out.println(System.getProperty("username")); } catch (Exception e) { e.printStackTrace(); } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值