java 方法 示例_Java属性stringPropertyNames()方法与示例

java 方法 示例

属性类stringPropertyNames()方法 (Properties Class stringPropertyNames() method)

  • stringPropertyNames() method is available in java.util package.

    java.util包中提供了stringPropertyNames()方法

  • stringPropertyNames() method is used to get all the key exists in this Property list and here the key and its desired value are string along with unique key exists in the default property list to be viewed in a Set.

    stringPropertyNames()方法用于获取此属性列表中存在的所有键,并且此处的键及其所需值是字符串,并且默认属性列表中存在唯一键以在Set中进行查看。

  • stringPropertyNames() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    stringPropertyNames()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • stringPropertyNames() method does not throw an exception at the time of returning property names.

    返回属性名称时, stringPropertyNames()方法不会引发异常。

Syntax:

句法:

    public Set stringPropertyNames();

Parameter(s):

参数:

  • It does not accept any parameter.

    它不接受任何参数。

Return value:

返回值:

The return type of the method is Set, it returns set of keys exist in this property list along with the key exists in the default list.

方法的返回类型为Set ,它返回存在于此属性列表中的键集以及存在于默认列表中的键。

Example:

例:

// Java program to demonstrate the example 
// of Set stringPropertyNames()
// method of Properties

import java.io.*;
import java.util.*;

public class StringPropertyNamesOfProperties {
 public static void main(String arg[]) throws Exception {
  // Instantiate Properties object
  Properties prop = new Properties();

  prop.put("10", "C");
  prop.put("20", "C++");
  prop.put("30", "JAVA");
  prop.put("40", "PHP");
  prop.put("50", "SFDC");

  // Get all the properties of
  // this object in a set
  Set set = prop.stringPropertyNames();

  // Display Set View
  System.out.println("prop.stringPropertyNames(): " + set);
 }
}

Output

输出量

prop.stringPropertyNames(): [50, 40, 30, 20, 10]


翻译自: https://www.includehelp.com/java/properties-stringpropertynames-method-with-example.aspx

java 方法 示例

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,我们可以使用java.util.Properties来读取.properties文件,并将其转换为Map对象。 首先,我们需要创建一个Properties对象,并通过load()方法加载.properties文件。然后,我们可以通过getProperty(key)方法获取每个键对应的值,并将其存储在Map对象中。 下面是一个示例代码: ```java import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.Map; import java.util.Properties; public class ReadPropertiesToMapExample { public static void main(String[] args) { // 创建一个Map对象,用于存储.properties文件的键值对 Map<String, String> propertiesMap = new HashMap<>(); // 读取.properties文件并将其转换为Map try (InputStream input = new FileInputStream("example.properties")) { Properties properties = new Properties(); properties.load(input); // 将.properties文件中的键值对存储在Map对象中 for (String key : properties.stringPropertyNames()) { String value = properties.getProperty(key); propertiesMap.put(key, value); } } catch (IOException e) { e.printStackTrace(); } // 输出Map对象的内容 for (Map.Entry<String, String> entry : propertiesMap.entrySet()) { System.out.println(entry.getKey() + "=" + entry.getValue()); } } } ``` 以上代码中,我们通过创建一个Map对象propertiesMap来存储.properties文件的键值对。然后,我们使用Properties类的load()方法来加载.properties文件。接下来,我们使用getProperty(key)方法获取每个键对应的值,并将其存储在propertiesMap中。最后,我们通过遍历Map对象来输出其内容。 需要注意的是,示例代码中的"example.properties"为需要读取的.properties文件路径,请根据实际情况进行替换。 希望以上回答能够解决你的问题,如果还有其他疑问,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值