MrSilent
MrSilent

Reputation: 574

File not found in project folder

I have a function that loads a config.properties file from the main folder of the project, however, it gives me a FileNotFoundException and I have no idea why. My method looks like:

public static Configfile configuration() {
        String server = null;
        int dbport = 0;
        String dbuser = null;
        String dbpass = null;

        Properties prop = new Properties();
        InputStream input = null;

        try {
            input = new FileInputStream("config.properties");
            prop.load(input);

            server = prop.getProperty("server");
            dbport = Integer.parseInt(prop.getProperty("dbport"));
            dbuser = prop.getProperty("dbuser");
            dbpass = prop.getProperty("dbpassword");
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return new Configfile(server, dbport, dbuser, dbpass);
    }

And my config.properties file is in:

E:\Android\NSomething5

What am I missing?

EDIT: Stacktrace:

12-04 17:00:07.619: W/System.err(21368): java.io.FileNotFoundException: /config.properties: open failed: ENOENT (No such file or directory)
12-04 17:00:07.619: W/System.err(21368):    at libcore.io.IoBridge.open(IoBridge.java:416)
12-04 17:00:07.619: W/System.err(21368):    at java.io.FileInputStream.<init>(FileInputStream.java:78)
12-04 17:00:07.619: W/System.err(21368):    at java.io.FileInputStream.<init>(FileInputStream.java:105)
12-04 17:00:07.619: W/System.err(21368):    at com.example.nsomething.utils.DBOperations.configuration(DBOperations.java:122)
12-04 17:00:07.619: W/System.err(21368):    at com.example.nsomething.utils.DBOperations.<init>(DBOperations.java:24)
12-04 17:00:07.619: W/System.err(21368):    at com.example.nsomething.Report.<init>(Report.java:20)
12-04 17:00:07.619: W/System.err(21368):    at java.lang.Class.newInstanceImpl(Native Method)
12-04 17:00:07.619: W/System.err(21368):    at java.lang.Class.newInstance(Class.java:1319)
12-04 17:00:07.619: W/System.err(21368):    at android.app.Instrumentation.newActivity(Instrumentation.java:1071)
12-04 17:00:07.619: W/System.err(21368):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
12-04 17:00:07.619: W/System.err(21368):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
12-04 17:00:07.619: W/System.err(21368):    at android.app.ActivityThread.access$700(ActivityThread.java:150)
12-04 17:00:07.619: W/System.err(21368):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
12-04 17:00:07.619: W/System.err(21368):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-04 17:00:07.619: W/System.err(21368):    at android.os.Looper.loop(Looper.java:137)
12-04 17:00:07.619: W/System.err(21368):    at android.app.ActivityThread.main(ActivityThread.java:5279)
12-04 17:00:07.619: W/System.err(21368):    at java.lang.reflect.Method.invokeNative(Native Method)
12-04 17:00:07.619: W/System.err(21368):    at java.lang.reflect.Method.invoke(Method.java:511)
12-04 17:00:07.619: W/System.err(21368):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
12-04 17:00:07.619: W/System.err(21368):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
12-04 17:00:07.619: W/System.err(21368):    at dalvik.system.NativeStart.main(Native Method)
12-04 17:00:07.619: W/System.err(21368): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
12-04 17:00:07.619: W/System.err(21368):    at libcore.io.Posix.open(Native Method)
12-04 17:00:07.619: W/System.err(21368):    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
12-04 17:00:07.619: W/System.err(21368):    at libcore.io.IoBridge.open(IoBridge.java:400)
12-04 17:00:07.619: W/System.err(21368):    ... 20 more

Upvotes: 0

Views: 1396

Answers (2)

Nazgul
Nazgul

Reputation: 1902

FileInputStream loads the file from either the absolute path provides or relative to the 'current working directory'. Now absolute path is not relevant to your example. So FileInputStream searches for your file 'config.properties' in the 'current working directory'. This directory is normally the one from which you started the program. If you are doing this on Android, place your config file in te assets folder and use the assets API functions to load the file as a stream. If this is a standalone java program then place the file on classpath and use the classloader.getResourceAsStream to load the file. Finally if you know the absolute path to the file use it directly.

Upvotes: 2

greenapps
greenapps

Reputation: 11214

E:\Android\NSomething5

That is no valid Android file system path. That is a Windows path. The app on your device has nothing to do with files on your pc.

Put your config file in the assets folder of your project. Then at runtime use assetsmanager to open an InputStream and read from it. For the rest youc can use the same code. Only opening InputStream is different.

Upvotes: 1

Related Questions