集合_Properties

Posted changzuidaerguai

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了集合_Properties相关的知识,希望对你有一定的参考价值。

技术分享图片
写配置信息

public class PropertiesJava {
     public static void main(String[] args) throws IOException{
           String path = "d:" + File.separator + "javatest" + File.separator +"PropertiesT.txt";
           File f = new File(path);
           OutputStream out = new FileOutputStream(f);
           //PrintStream ps = new PrintStream(new FileOutputStream(f));
           Properties p = new Properties();
           p.setProperty("name", "zed");
           p.setProperty("age", "zed");
           p.setProperty("pass", "zed");
           
           //list()方法只能输出打印流,而store()方法只要是输出流就可以,使用store方法
           //store()方法属性列表信息更详细
           //p.list(ps);
           p.store(out, "comments");
           out.close();
     }
}

读配置信息

public class PropertiesJava {
     public static void main(String[] args) throws IOException{
           String path = "d:" + File.separator + "javatest" + File.separator +"PropertiesT.txt";
           File f = new File(path);
           InputStream in = new FileInputStream(f);
           
           Properties p = new Properties();
           p.load(in);
           Enumeration<?> en = p.propertyNames();
           while(en.hasMoreElements()) {
                String strKey = (String)en.nextElement();
                String strValue = p.getProperty(strKey);
                System.out.println(strKey + " = " + strValue);
           }
           
           System.out.println("------------------");
           
           //已知key求value
           String strAge = p.getProperty("age");
           String strName = p.getProperty("name");
           System.out.println(strName);
           System.out.println(strAge);
           
           in.close();
           
     }
}

以上是关于集合_Properties的主要内容,如果未能解决你的问题,请参考以下文章

25_IO_第25天(Properties序列化流打印流CommonsIO)_讲义

25_IO_第25天(Properties序列化流打印流CommonsIO)

00096_Properties类

Java Properties集合代码详解

代码片段 - Golang 实现集合操作

laravel特殊功能代码片段集合