杰瑞科技汇

java遍历properties

Java遍历Properties文件的几种方法

在Java中,遍历Properties文件内容有几种常见方法,以下是几种主要的实现方式:

java遍历properties-图1
(图片来源网络,侵删)

使用Enumeration遍历

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
public class PropertiesTraversal {
    public static void main(String[] args) {
        Properties properties = new Properties();
        try {
            // 加载properties文件
            properties.load(new FileInputStream("config.properties"));
            // 使用Enumeration遍历
            Enumeration<?> enumeration = properties.propertyNames();
            while (enumeration.hasMoreElements()) {
                String key = (String) enumeration.nextElement();
                String value = properties.getProperty(key);
                System.out.println(key + " = " + value);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

使用forEach方法(Java 8+)

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class PropertiesTraversal {
    public static void main(String[] args) {
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("config.properties"));
            // 使用forEach遍历
            properties.forEach((key, value) -> System.out.println(key + " = " + value));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

使用entrySet()遍历

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;
public class PropertiesTraversal {
    public static void main(String[] args) {
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("config.properties"));
            // 使用entrySet遍历
            for (Map.Entry<Object, Object> entry : properties.entrySet()) {
                System.out.println(entry.getKey() + " = " + entry.getValue());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

使用keySet()遍历

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class PropertiesTraversal {
    public static void main(String[] args) {
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("config.properties"));
            // 使用keySet遍历
            for (String key : properties.stringPropertyNames()) {
                System.out.println(key + " = " + properties.getProperty(key));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

注意事项

  1. 确保properties文件路径正确,或者使用绝对路径
  2. 处理可能出现的IOException
  3. 如果properties文件使用非UTF-8编码,需要指定字符集:
    properties.load(new InputStreamReader(new FileInputStream("config.properties"), "UTF-8"));
  4. 在Java 8+中,推荐使用forEach方法,代码更简洁

完整示例(带资源管理)

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
public class PropertiesTraversal {
    public static void main(String[] args) {
        try (FileInputStream fis = new FileInputStream("config.properties")) {
            Properties properties = new Properties();
            properties.load(fis);
            // 使用Enumeration遍历
            Enumeration<?> enumeration = properties.propertyNames();
            while (enumeration.hasMoreElements()) {
                String key = (String) enumeration.nextElement();
                String value = properties.getProperty(key);
                System.out.println(key + " = " + value);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

方法都可以有效地遍历Properties文件内容,选择哪种方法主要取决于你的Java版本和个人偏好。

java遍历properties-图2
(图片来源网络,侵删)
分享:
扫描分享到社交APP
上一篇
下一篇