简体   繁体   中英

How to load files using relative path in NetBeans

import java.io.*;
import java.util.Properties;

public class NewClass {
    public static void main(String args[]) throws IOException  {
        Properties p = new Properties();
        p.load(new FileInputStream("DBDriverInfo.properties"));
        String url=p.getProperty("url");
        String user=p.getProperty("username");
        String pass=p.getProperty("password");
        System.out.println(url+"\n"+user+"\n"+pass);
    }
}

Though the file DBDriverInfo.properties file is in the same directory, the following exception is raised.

Exception in thread "main" java.io.FileNotFoundException: DBDriverInfo.properties (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:97)
    at NewClass.main(NewClass.java:7)

Relative paths work fine when compiled using javac in a command line interface. But the exception raises in NetBeans.

在Netbeans中,您需要将该文件放在项目文件夹中而不是src / package文件夹中。

You should specify a full path the file or put the file to the project dir. The project dir is the current dir when run the project.

Make sure your DBDriverInfo.properties is on the CLASSPATH. According to your code, place your properties file to netbeans's default package.

The default directory for the File class is the one from where you started your main class execution. In case of these IDEs the default directory will be your project home directory.

To better know your default directory execute these two lines from your IDE. And then place your file there.

File f = new File("DBDriverInfo.properties");
System.out.println(f.getAbsolutePath());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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