可以使用Properties类的load()方法将文件中的内容读入到Properties对象中。代码示例如下:

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.io.File;

public class FileToProperties {
    public static void main(String[] args) {
        File file = new File("config.properties");
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream(file));
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(properties.getProperty("name"));
    }
}

在上述代码中,首先创建一个File对象,表示要读取的配置文件,然后创建一个Properties对象。接着,使用Properties类的load()方法将文件中的内容读入到Properties对象中。最后,可以通过getProperty()方法获取配置项的值

java file对象如何转换成Properties对象

原文地址: https://www.cveoy.top/t/topic/fGnC 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录