java中,java中为什么要用注解 Properties P=System.getProperties();说返回的是双类集合?????

Java.lang.System之getProperties()方法 - 推酷
Java.lang.System之getProperties()方法
java.lang.System类维护了一个java.util.Properties实例,此实例用来保存系统参数,开发者可以通过System类的相关方法访问和设置系统参数。
获取系统参数
getProperties方法有三个重载,如下:
System.getProperties()
System.getProperties(String key)
System.getProperties(String key , String default)
打印当前系统参数
for (Map.Entry&Object, Object& entry : System.getProperties().entrySet()){
System.out.println(entry.getKey()+“——&”+entry.getValue());
运行结果如下:
Environmentjava.vm.version——&25.60-b23
java.vm.vendor——&Oracle Corporation
java.vendor.url——&/
idea.launcher.port——&7533
java.vm.name——&Java HotSpot(TM) 64-Bit Server VM
file.encoding.pkg——&sun.io
user.country——&CN
sun.java.launcher——&SUN_STANDARD
java.vm.specification.name——&Java Virtual Machine Specification
java.runtime.version——&1.8.0_60-b27
java.awt.graphicsenv——&sun.awt.Win32GraphicsEnvironment
os.arch——&amd64
java.vm.specification.vendor——&Oracle Corporation
os.name——&Windows 10
sun.jnu.encoding——&GBK
java.specification.name——&Java Platform API Specification
mand——&com.intellij.rt.execution.application.AppMain
设置系统参数
System.load(String filename)等同于:System.getProperties().load(String filename)
System.setProperties(Properties propes)
System.setProperties(String key,String value)等同于System.getProperties().setProperties(String key,String value)
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致Java中的Properties类的操作_Linux编程_Linux公社-Linux系统门户网站
你好,游客
Java中的Properties类的操作
来源:Linux社区&
作者:linyuhuan
Java中有个比较重要的类:Properties类,该类主要用于读取java的配置文件。每种语言都自己所支持的配置文件。java中的配置常为*.perperties文件,文件格式为文本格式,内容格式为&健=值&对格式,文本注释信息可用#注释。
Properties继承Hashtable
它提供了几个主要的方法:
1. getProperty ( String key),用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。
2. load ( InputStream inStream),从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文件中的所有键 - 值对。以供 getProperty ( String key) 来搜索。
3. setProperty ( String key, String value) ,调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。
4. store ( OutputStream out, String comments),以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。
5. clear (),清除所有装载的 键 - 值对。该方法在基类中提供。
二、Java读取Properties文件
& & Java读取Properties文件的方法有很多
但是最常用的还是通过java.lang.Class类的getResourceAsStream(String name)方法来实现,如下可以这样调用:
InputStream in = getClass().getResourceAsStream("资源Name");作为我们写程序的,用此一种足够。
或者下面这种也常用:
InputStream in = new BufferedInputStream(new FileInputStream(filepath));
三、相关实例
下面列举几个实例,加深对Properties类的理解和记忆。
我们知道,Java虚拟机(JVM)有自己的系统配置文件(system.properties),我们可以通过下面的方式来获取。
1、获取JVM的系统属性
public class Property {& & public static void main(String args[]){& & & & System.out.println(new Date());& & & & Properties p=System.getProperties();& & & & p.list(System.out);& //print all properties about JVM& & }
2.读取本地配置文件test.properties并输出
public class Property {& & public static void main(String args[]){//& & & & System.out.println(new Date());//& & & & Properties p=System.getProperties();//& & & & p.list(System.out);& //print all properties about JVM& & & & & & & & Properties text=new Properties();& & & & try {& & & & & & text.load(new FileInputStream("E:\\workspace\\ch01\\text.properties"));& & & & } catch (FileNotFoundException e) {& & & & & & e.printStackTrace();& & & & } catch (IOException e) {& & & & & & e.printStackTrace();& & & & }& & & & & & & & @SuppressWarnings("rawtypes")& & /*& & Enumeration接口也在java.util包中 它的功能与Iterator差不多 都是用来遍历集合中的元素 & & & & 但是枚举Enumeration只提供了遍历Vector和Hashtable类型集合元素的功能*/& & & & Enumeration num=text.propertyNames();& //获取配件文件内容中的关于key的Enumeration& & & & & & & & while(num.hasMoreElements()){& & & & & & String key=(String)num.nextElement();& & & & & & String value=text.getProperty(key);& & & & & & System.out.println(key + "=" + value);& & & & }& & }
age=23name=linyuhuanweight=140long=212
3、一个比较综合的实例
根据key读取value
读取properties的全部信息
写入新的properties信息
package ch01;
import java.io.BufferedInputSimport java.io.FileInputSimport java.io.FileOutputSimport java.io.IOEimport java.io.InputSimport java.io.OutputSimport java.util.Eimport java.util.P
/**&* 关于Properties类的常用操作&* @author lin&* 日&*/public class TestProperties {
& & public static void main(String[] args) throws IOException{& & & & & & WriteProperties("E:\\workspace\\ch01\\text.properties", "add2","2121");& & }& & & & //根据key读取value& & public static String GetValueByKey(String filePath,String key){& & & & Properties pps = new Properties();& & & & try{& & & & & & InputStream in= new BufferedInputStream(new FileInputStream(filePath));& & & & & & pps.load(in);& & & & & & String value = pps.getProperty(key);& & & & & & System.out.println(key + "=" + value);& & & & & && & & & }catch(IOException e){& & & & & & e.printStackTrace();& & & & & && & & & }& & }& & & & //读取配置文件的全部信息& & @SuppressWarnings("rawtypes")& & public static void GetAllProperties(String filePath) throws IOException{& & & & Properties pps = new Properties();& & & & InputStream in = new BufferedInputStream(new FileInputStream(filePath));& & & & pps.load(in);& & & & Enumeration num = pps.propertyNames(); //获取配置文件中的所有属性名enumeration& & & & while(num.hasMoreElements()){& & & & & & String key= (String)num.nextElement();& & & & & & String value=pps.getProperty(key);& & & & & & System.out.println(key + "=" + value);& & & & }& & }& & & & //写入Properties信息& & public static void WriteProperties(String filePath,String pKey,String pValue) throws IOException {& & & & Properties pps = new Properties();& & & & InputStream in= new FileInputStream(filePath);& & & & pps.load(in);& & & & OutputStream out = new FileOutputStream(filePath);& & & & pps.setProperty(pKey, pValue);& & & & pps.store(out, "Update " + pKey + " name");& & }}
结果:test.properties文件
#Update add2 name#Tue Jun 30 17:07:55 CST 2015age=23name=linyuhuanweight=140add2=2121long=212
本文永久更新链接地址:
相关资讯 & & &
& (12/19/:21)
& (11/08/:39)
& (01月14日)
& (12/14/:42)
& (09/19/:03)
   同意评论声明
   发表
尊重网上道德,遵守中华人民共和国的各项有关法律法规
承担一切因您的行为而直接或间接导致的民事或刑事法律责任
本站管理人员有权保留或删除其管辖留言中的任意内容
本站有权在网站内转载或引用您的评论
参与本评论即表明您已经阅读并接受上述条款遍历Properties的值_Java_ThinkSAAS
遍历Properties的值
遍历Properties的值
内容来源: 网络
第一种遍历:
Properties p=System.getProperties();
for(Enumeration e=p.propertyNames(); e.hasMoreElements();){
String key=(String) e.nextElement();
System.out.println(key+"===&"+p.getProperty(key));
第二种遍历:
Properties p=System.getProperties();
for(Object t:p.keySet()){
System.out.println(t+"="+p.getProperty(t.toString()));
import java.util.*;
class TestSystem
public static void main(String[] args)
Map&String ,String& map = System.getenv();
//System.out.println(map);
// 遍历系统Map中system
for(String name:map.keySet()){
System.out.println(name+"-&&&&&&&&"+map.get(name));
System.out.println(System.getenv("JAVA_HOME"));
Properties p=System.getProperties();
//遍历一下properties里面的值
for(Enumeration e=p.propertyNames(); e.hasMoreElements();){
String key=(String) e.nextElement();
System.out.println(key+"===&"+p.getProperty(key));
// System.out.println(p);
System.out.println(System.getProperty("os.name"));
PHP开发框架
开发工具/编程工具
服务器环境
ThinkSAAS商业授权:
ThinkSAAS为用户提供有偿个性定制开发服务
ThinkSAAS将为商业授权用户提供二次开发指导和技术支持
让ThinkSAAS更好,把建议拿来。
开发客服微信&>&&>&&>&&>&Java中通过System.getProperties()获取系统参数测试.docx
Java中通过System.getProperties()获取系统参数测试.docx
上传大小:74KB
Java中通过System.getProperties()获取系统参数测试
综合评分:0(0位用户评分)
所需积分:0
下载次数:1
审核通过送C币
创建者:ls
创建者:nigelyq
创建者:sinat_
课程推荐相关知识库
上传者其他资源上传者专辑
开发技术热门标签
VIP会员动态
您因违反CSDN下载频道规则而被锁定帐户,如有疑问,请联络:!
android服务器底层网络模块的设计方法
所需积分:0
剩余积分:720
您当前C币:0
可兑换下载积分:0
兑换下载分:
兑换失败,您当前C币不够,请先充值C币
消耗C币:0
你当前的下载分为234。
Java中通过System.getProperties()获取系统参数测试.docx
会员到期时间:
剩余下载次数:
你还不是VIP会员
开通VIP会员权限,免积分下载
你下载资源过于频繁,请输入验证码
您因违反CSDN下载频道规则而被锁定帐户,如有疑问,请联络:!
若举报审核通过,可奖励20下载分
被举报人:
举报的资源分:
请选择类型
资源无法下载
资源无法使用
标题与实际内容不符
含有危害国家安全内容
含有反动色情等内容
含广告内容
版权问题,侵犯个人或公司的版权
*详细原因:}

我要回帖

更多关于 java中 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信