package pageObjects;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;
public class ReadProperty {
public static void main(String[] args) throws Exception {
Properties prop = new Properties();
String fileName = "config.properties";
InputStream input = new FileInputStream(fileName);
prop.load(input);
System.out.println(prop.getProperty("UserName"));
System.out.println(prop.getProperty("Password"));
}
}
出现如下错误:
Exception in thread "main" java.io.FileNotFoundException: config.properties (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.(FileInputStream.java:138) at java.io.FileInputStream.(FileInputStream.java:93) at pageObjects.ReadProperty.main(ReadProperty.java:16)
最佳答案
程序无法打开与 config.properties 文件的连接,可能有两个原因:
- 该文件不存在。
- 其名称或路径不正确。
我想这不是第一个选项,因此您可以尝试使用文件的完整路径更改 String fileName = "config.properties";
。
如果该文件不在您执行程序的同一文件夹中,并且您没有指定完整路径,则它将找不到它。这样,无论您的工作目录如何,您都可以始终确保程序能够找到它。
关于java - 我试图访问 config.properties 文件但无法访问。谁能帮我?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33937543/