【问题标题】:Java - how to Read a properties file into array [closed]Java - 如何将属性文件读入数组[关闭]
【发布时间】:2015-07-01 05:27:51
【问题描述】:

所有 - 我是 java 的新手。所以需要一些帮助或代码 属性文件的位置 测试属性 100 200 300 400

我想把它读入一个数组,这样我得到的输入数据,我可以检查它是否在数组中。

我实际上可以硬编码 if id=100 or id=200 or id=300 {then do somethings} else { do something ordo nothing}。

我找到了答案:要在此处添加代码

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

public class read_properties_into_array {

    private static List<String> sensitivePropertiesList=new ArrayList<String>();
    public static void main(String[] args) {
        try {
            File file = new File("test.properties");
            FileInputStream fileInput = new FileInputStream(file);
            Properties properties = new Properties();
            properties.load(fileInput);
            fileInput.close();

            Enumeration enuKeys = properties.keys();
            while (enuKeys.hasMoreElements()) {
                String key = (String) enuKeys.nextElement();
                sensitivePropertiesList.add(new String(key));
                //String value = properties.getProperty(key);
                //System.out.println(key);
            }
            System.out.println("hi I am here");
            System.out.println("lenght of list:"+sensitivePropertiesList.size());

            for(int i=0;i<sensitivePropertiesList.size();i++)
            {
                System.out.println(sensitivePropertiesList.get(i));
            }
            System.out.println("Check if 100 it exists.");
            if (sensitivePropertiesList.contains("100"))
            {

                System.out.println(" 100 it exists.");
            }
            else 
            {
                System.out.println(" 100 Does not exist.");
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

如果使用eclipse,请在java项目级别添加test.properties文件。

Test.properties
100
200
300

【问题讨论】:

    标签: java properties properties-file


    【解决方案1】:

    虽然您的问题不清楚,但我认为您不需要属性类来读取数组。您将key=value 对放在属性文件中。

    您应该首先使用 java IO 读取文件,然后将所有值放入一个数组中,最后遍历该数组并检查您的值。

    在这里检查一些代码: https://stackoverflow.com/a/7705672/841221

    【讨论】:

      【解决方案2】:

      如果你不使用 Java Properties 文件,而是使用类似的东西

      test.properties

      100
      200
      300
      

      您可以将所有行读入List 并稍后处理该列表。

      Path inputFile = Paths.get("test.properties");
      Charset fileCharset = Charset.defaultCharset();
      List<String> allValues = Files.readAllLines(inputFile, fileCharset);
      
      // work on that list
      for (String value : allValues) {
          System.out.println(value);
      }
      

      【讨论】:

        猜你喜欢
        • 2013-12-11
        • 1970-01-01
        • 2010-09-22
        • 2016-01-04
        • 2019-09-03
        • 2012-05-14
        • 2013-02-23
        • 1970-01-01
        相关资源
        最近更新 更多