简体   繁体   English

找不到Java Fileinputstream文件

[英]Java Fileinputstream File not found

"Dbconnection.java" "db.properties" the file can not find the file. “ dbconnection.java”“ db.properties”文件找不到该文件。 I show you how I get the following files. 我向您展示如何获取以下文件。

My next project directory. 我的下一个项目目录。

  • src src

    • DB D B

      • DbConnection.java DbConnection.java
  • db.properties db.properties

DbConnection.java DbConnection.java

 public class DBConnection {
    public static Connection getConnection() {
        Properties props = new Properties();
        FileInputStream fis = null;
        Connection con = null;
        try {

            fis = new FileInputStream("db.properties");
            props.load(fis);
            out.println(props.getProperty("DB_DRIVER_CLASS"));
            // load the Driver Class
            Class.forName(props.getProperty("DB_DRIVER_CLASS"));

            // create the connection now
            con = DriverManager.getConnection(props.getProperty("DB_URL"),
                    props.getProperty("DB_USERNAME"),
                    props.getProperty("DB_PASSWORD"));
        } catch (IOException | ClassNotFoundException | SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return con;
    }
}

fis = new FileInputStream("db.properties"); fis = new FileInputStream(“ db.properties”);

This line looks for the file under the "home" directory where you run the application. 该行在运行应用程序的“主”目录下查找文件。 Its equivalent to "./db.properties". 它等效于“ ./db.properties”。 If you don't know the root directory of the application, try add debug using this line new File(".").getAbsolutePath(); 如果您不知道应用程序的根目录,请尝试使用此行添加调试。new File(“。”)。getAbsolutePath(); then move your properties file there. 然后将您的属性文件移到那里。

In the above code please give the complete path of the file(db.properties). 在上面的代码中,请提供文件的完整路径(db.properties)。 That is, for example, if the file is placed in "E" drive "projectA" folder then the path is 也就是说,例如,如果文件位于“ E”驱动器“ projectA”文件夹中,则路径为

fis = new FileInputStream("E:/projectA/db.properties");// will work

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM