java - 我试图访问 config.properties 文件但无法访问。谁能帮我?

标签 java selenium-webdriver

  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 文件的连接,可能有两个原因:

  1. 该文件不存在。
  2. 其名称或路径不正确。

我想这不是第一个选项,因此您可以尝试使用文件的完整路径更改 String fileName = "config.properties";

如果该文件不在您执行程序的同一文件夹中,并且您没有指定完整路径,则它将找不到它。这样,无论您的工作目录如何,您都可以始终确保程序能够找到它。

关于java - 我试图访问 config.properties 文件但无法访问。谁能帮我?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33937543/

相关文章:

java - 通过java代码获取虚拟内存页面大小

java - php/apache 会支持多线程吗?

java - 在不重启 Android Studio 的情况下重启 Gradle

java - 如何顺序读取文件但重复指针的任何位置

python - 如何在 Chrome 浏览器通过 Python selenium 自动更新时使用特定版本的 ChromeDriver

java - 计算机锁定时自动关闭显示器

java - Selenium 不等待页面加载

wpf - 如何将 Selenium WebDriver 嵌入为 WPF 控件?

java - 不弹出另存为对话框

java - 页面加载的默认 HtmlUnitDriver 超时是多少?