首页 > 编程知识 正文

第十三类商标明细,properties类的直接父类

时间:2023-05-03 08:42:26 阅读:119263 作者:3961

我把你的头像,设定为我的名字,现在你和我在一起。

我把你的名字写在我的代码里,从今以后,我的世界里会有你。

一. Properties类Properties类位于java.util.Properties中,是Java语言配置文件中使用的类,而Xxx.properties是Java语言的常规配置文件,例如

将值存储为key=value的键-值对。 key值不能重复。

继承Hashtable类,并以映射的形式放置值、put(key,value ) put(key )

主要方法:

这里只说明常用的形式。

2 .打印JVM参数JVM可以获取Properties并打印输出JVM已知的属性值。

list ) )方法打印到控制台。

@ testpublicvoidprinttest ((属性属性=system.get properties ) ); properties.list (系统. out; }常见的是:

3 .将jdbc.properties文件置于要打印自定义. properties文件值的src目录下。 这是数据库的配置文件。

JDBC.driver=com.MySQL.JDBC.driver JDBC.URL=JDBC 3360 MySQL 3360//localhost :3306/my batis? LQ dbl=utf8 JDBC.username=root JDBC.password=ABC 123将三.一list输出到控制台并以绝对路径加载@Testpublic void name1Test () try { PPP 使用了磁盘字符的绝对路径inputstream input=newbufferedinputstream (new file inputstream (d : (workspace (javalearn ) ) Srrearn ) properties.list (系统. out; }catch(exceptione ) {e.printStackTrace ); }} url已被剪切。

三、二propertyNames输出getClass ()加载@Testpublic void name2Test ) ) try { properties properties=new properties ) ); //在文件名中,/是根目录inputstream input=properties test.class.getclass (.getresourceasstream ()/JDBC.properties properties.load (输入; enumerationstringnames=(enumeration string ) properties.propertyNames ); while(names.hasmoreelements () ) /这是key值String key=names.nextElement ); string value=properties.getproperty (key; system.out.println(key'='value; }catch(exceptione ) {e.printStackTrace ); }

三、三stringPropertyNames输出getClassLoader加载(推荐) @Testpublic void name3Test ) (try ) properties properties=new properties ) ) //src类路径下的文件名inputstream input=properties test.class.getclass loader (.getresourceasstream (' JDBC.properties ' ) properties.load (输入; //key值转换为set的形式,并使用setsetstringnames=properties.stringpropertynames (; iteratorstringiterator=names.iterator (; wile(iterator.Hasnext () ({String key=iterator.next ) ); string value=properties.getproperty (key; system.out.println(key'='value; }catch(exceptione ) {e.printStackTrace ); }

四.获取值get properties @ testpublicvoidname3test () {try{Properties proper

ties=new Properties();InputStream input=PropertiesTest.class.getClassLoader().getResourceAsStream("jdbc.properties");properties.load(input);//String value=properties.getProperty("jdbc.url");String value=properties.getProperty("jdbc.url1","没有该key值");System.out.println("输出值:"+value);}catch(Exception e){e.printStackTrace();}}

输出时,getProperty() 有当前的key值,则输出Key值对应的value 值。
如果没有key值,则输出 null 值。
后面可以跟 default 值,如果没有该值,则输出设置的默认值。

五. 写入到Properties 文件 五.一 普通写入,中文时乱码 @Testpublic void writeTest(){try{Properties properties=new Properties();InputStream input=PropertiesTest.class.getClassLoader().getResourceAsStream("jdbc.properties");properties.load(input);//多添加几个值。properties.setProperty("name","两个蝴蝶飞");properties.setProperty("sex","男");//properties.put("name","两个蝴蝶飞"); 可以用继承Hashtable 的put 方法写入值// properties.put("sex","男");//将添加的值,连同以前的值一起写入 新的属性文件里面。OutputStream out=new FileOutputStream("D:\jdbc.properties");properties.store(out,"填充数据");}catch(Exception e){e.printStackTrace();}}

五.二 解决乱码写入的问题

在构建输入流和输出流时,指定编码格式, 编码的格式相同。 如均是 utf-8的形式。

@Testpublic void write2Test(){try{Properties properties=new Properties();//用绝对路径InputStream input=new BufferedInputStream(new FileInputStream("D:\workspace\JavaLearn\src\jdbc.properties"));properties.load(new InputStreamReader(input,"utf-8"));//多添加几个值。properties.setProperty("name","两个蝴蝶飞");properties.setProperty("sex","男");OutputStream output=new FileOutputStream("D:\jdbc.properties");OutputStreamWriter out=new OutputStreamWriter(output,"utf-8");properties.store(out,"填充数据");}catch(Exception e){e.printStackTrace();}}

测试运行之后:

这样便解决了乱码的问题。

六 . 加载和导出到 xml 配置文件 六.一 导出到 .xml 配置文件 storeToXML

将Properties 类中定义的属性,导出成 .xml 的形式.

@Testpublic void xmlWriteTest(){try{//处理成编码样式。Properties properties=new Properties();//多添加几个值。properties.setProperty("name","两个蝴蝶飞");properties.setProperty("sex","男");OutputStream output=new FileOutputStream("D:\jdbc.xml");//编码设置成utf-8的形式。 properties.storeToXML(output,"填充到xml","utf-8");}catch(Exception e){e.printStackTrace();}}

测试结果为:

用 <entry> 节点 key为属性, 后面跟值来进行输入。
可按照这种形式,继续添加。

六.二 导出XML 配置文件 loadFromXML @Testpublic void xmlReadTest(){try{Properties properties=new Properties();InputStream input=new BufferedInputStream(new FileInputStream("D:\jdbc.xml"));properties.loadFromXML(input);properties.list(System.out);}catch(Exception e){e.printStackTrace();}}

这就是Properties 类的常见用法 。

谢谢!!!

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。