java怎么读取properties文件内容

41次阅读
没有评论

共计 755 个字符,预计需要花费 2 分钟才能阅读完成。

在 Java 中,可以使用 java.util.Properties 类来读取 .properties 文件的内容。以下是一个示例代码:

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class ReadPropertiesFile {public static void main(String[] args) {Properties properties = new Properties();
try {
// 通过文件输入流加载 properties 文件
FileInputStream file = new FileInputStream("config.properties");
// 加载 properties 文件内容到 Properties 对象
properties.load(file);
// 关闭文件输入流
file.close();
// 读取 properties 文件中的属性值
String username = properties.getProperty("username");
String password = properties.getProperty("password");
System.out.println("Username:" + username);
System.out.println("Password:" + password);
} catch (IOException e) {e.printStackTrace();
}
}
}

请将上述代码中的 config.properties 替换为你要读取的 .properties 文件的路径。然后,你可以通过 getProperty 方法来读取文件中的属性值。

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-16发表,共计755字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)