首页 > 编程语言 >无涯教程-Java - Properties 类函数

无涯教程-Java - Properties 类函数

时间:2023-12-18 18:32:50浏览次数:33  
标签:Java String capitals 无涯 streamOut str capital Properties

Properties是Hashtable的子类。它用于维护值列表,其中键是字符串,并且值也是字符串。

属性(Properties)定义以下变量。此变量保存与Properties对象关联的默认属性列表。

Properties defaults;

以下是properties类提供的构造函数的列表。

Sr.No. Constructor & Remark
1

Properties()

此构造函数创建一个没有默认值的Properties对象。

2

Properties(Properties propDefault)

创建一个使用propDefault作为其默认值的对象。

除了Hashtable定义的方法外,Properties还定义了以下方法-

Sr.No. Method & Remark
1

String getProperty(String key)

返回与键关联的值。

2

String getProperty(String key,String defaultProperty)

返回与键关联的值;

3

void list(PrintStream streamOut)

将属性列表发送到链接到streamOut的输出流。

4

vlid list(PrintWriter streamOut)

将属性列表发送到链接到streamOut的输出流。

5

void load(InputStream streamIn) throws IOException

从链接到streamIng的输入流中输入属性列表。

6

Enumeration propertyNames()

返回键的枚举。

7

Object setProperty(String key,String value)

将价值与键相关联。

8

void store(OutputStream streamOut, String description)

写入由description指定的字符串后,将属性列表写入链接到streamOut的输出流。

Properties 示例

以下程序说明了此数据结构支持的几种方法-

import java.util.*;
public class PropDemo {

   public static void main(String args[]) {
      Properties capitals=new Properties();
      Set states;
      String str;
      
      capitals.put("Illinois", "Springfield");
      capitals.put("Missouri", "Jefferson City");
      capitals.put("Washington", "Olympia");
      capitals.put("California", "Sacramento");
      capitals.put("Indiana", "Indianapolis");

      //在哈希表中显示所有州和首都。
      states=capitals.keySet();   //获取键的集合视图
      Iterator itr=states.iterator();
      
      while(itr.hasNext()) {
         str=(String) itr.next();
         System.out.println("The capital of " + str + " is " + 
            capitals.getProperty(str) + ".");
      }     
      System.out.println();

      //查找不在列表中的状态——指定默认值
      str=capitals.getProperty("Florida", "Not Found");
      System.out.println("The capital of Florida is " + str + ".");
   }
}

这将产生以下输出-

The capital of Missouri is Jefferson City.
The capital of Illinois is Springfield.
The capital of Indiana is Indianapolis.
The capital of California is Sacramento.
The capital of Washington is Olympia.

The capital of Florida is Not Found.

参考链接

https://www.learnfk.com/java/java-properties-class.html

标签:Java,String,capitals,无涯,streamOut,str,capital,Properties
From: https://blog.51cto.com/u_14033984/8876528

相关文章

  • java生成word文件
    publicbooleanperformFinish(){ booleanfinished=true; FilemodelFile=worldPage.getModelFile(); StringsavePath=worldPage.getSaveFilePath(); StringfileName=worldPage.getFileName(); ontModel=ModelFactory.createOntologyModel(OntMo......
  • java生成word文件
    publicbooleanperformFinish(){ booleanfinished=true; FilemodelFile=worldPage.getModelFile(); StringsavePath=worldPage.getSaveFilePath(); StringfileName=worldPage.getFileName(); ontModel=ModelFactory.createOntologyModel(OntMo......
  • 无涯教程-Java - Dictionary 类函数
    字典(Dictionary)是一个抽象类,代表一个键/值对,其操作非常类似于Map。下面列出了Dictionary定义的抽象方法-Sr.No.Method&Remark1Enumerationelements()返回字典中包含的值的枚举。2Objectget(Objectkey)返回包含与键关联的值的对象,如果键不在字典中,则返回空对象......
  • 无涯教程-Java - Stack 类函数
    堆栈是Vector的子类,它实现了标准的后进先出堆栈。Stack()除了从其父类Vector继承的方法外,Stack还定义了以下方法-Sr.No.Method&Remark1booleanempty()测试此堆栈是否为空。如果堆栈为空,则返回true;如果堆栈包含元素,则返回false。2Objectpeek()返回位于堆栈顶部......
  • 秦疆的Java课程笔记:74 面向对象 抽象类
    abstract修饰符可以用来修饰方法,也可以用来修饰类。如果修饰方法,那么该方法就是抽象方法;如果修饰类,那么该类就是抽象类。抽象类中可以没有抽象方法,但是有抽象方法的类一定要声明为抽象类。抽象类,不能使用new关键字来创建对象,它是用来让子类继承的。抽象方法,只有方法的......
  • java rsa加解密
    坑点:用其他语言的一定注意,Java秘钥和php秘钥不同,不用混用。  packagecn.muyan.monitoragent.utils.encrypt;importsun.misc.BASE64Decoder;importsun.misc.BASE64Encoder;importjavax.crypto.Cipher;importjava.io.IOException;importjava.security.*;i......
  • jenkins发布Java项目
    1.java项目是什么(springboot)由java语言开发的后端,就是java项目前面于超老师教了大家学习了wordpress---php项目部署,提供LNMP,交给php-fpm进程去解释执行该源码jumpserver的core后端---python项目,提供python3环境,即可运行golang程序,需要安装golan编译器,编译二进制命令......
  • 无涯教程-Java - Vector 类函数
    Vector实现了动态数组。它类似于ArrayList,线程同步sychronized。以下是vector类提供的构造函数的列表。Sr.No.Constructor&Remark1Vector()此构造函数创建一个默认向量,其初始大小为10。2Vector(intsize)此构造函数接受等于所需大小的参数,并创建一个向量,其初始容......
  • 《Java编程思想第四版》学习笔记49--关于第十三章习题
    (9)增加Frog.class到本章出现的清单文件中并运行jar以创建一个包括Frog和BangBean的JAR文件。现在从SUN公司处下载并安装BDK或者使用我们自己的可激活Bean的程序构建工具并增加JAR文件到我们的环境中,因此我们可以测试两个Bean。(10)创建我们自己的包括两个属性......
  • Java里Integer怎么转成Long
    在Java中,你可以使用装箱(Boxing)和拆箱(Unboxing)的概念来进行Integer到Long的转换。具体方法如下:IntegerintValue=42;//你的整数值LonglongValue=intValue.longValue();这里的longValue()方法将Integer对象转换为对应的long型值,因为Long是整数的64位表示,而Integer是32位表示......