0

I am encountering a weird FileNotFoundException with FileInputStream while working in Android Studio. Using this post, I ran some checks, but still cannot find the issue.

This is my code:

package com.mypackage;

import java.io.FileInputStream;
import java.sql.*;
import java.util.*;
import java.util.logging.Logger;
import java.io.File;

public class DatabaseDriver {

    public DatabaseDriver(){
        Properties properties = new Properties();

        // get credentials from application.properties file
        FileInputStream in = new FileInputStream("application.properties");
        properties.load(in);

        String url = properties.getProperty("url");
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
    }

} 

With some of the tips from the above post, I ran the following code in a debugging attempt:

public class DatabaseDriver {
    public DatabaseDriver(){

        File file = new File("application.properties");
        System.out.println(new File(".").getAbsolutePath());
        System.out.println(new File("application.properties").getAbsolutePath());
        System.out.println(file.exists());
        System.out.println(file.canRead());
        System.out.println(file.isDirectory());
    }
}

And the output when I create a DatabaseDriver instance in a main:

C:\Users\erin\AndroidStudioProjects\RecCenter\.
C:\Users\erin\AndroidStudioProjects\RecCenter\application.properties
true
true
false

That all seems fine, so I'm a little lost.

Erin
  • 1
  • 3
  • You need to use `'\\'` for `'\'`. I.e. `"C:\\Users\\me\\AndroidStudioProjects\\RecCenter\\"`. You might also use `'/'` for `'\'`, which is the better variant, cause it's OS independent. I.e. `"C:/Users/me/AndroidStudioProjects/RecCenter/"`. – paladin Mar 25 '22 at 09:48
  • 2
    I suspect that the problem is that the current directory is different when you running the two versions of the class. You don't show how you are doing that. You also don't show us the stacktrace for the exception in the first version ... or the output from the 2nd version. (And I would not that the first version that you showed us *won't compile* ... because the `FileInputStream` constructor is defined as throwing a checked exception that the code is neither catching or propagating.) – Stephen C Mar 25 '22 at 10:04
  • How would the current directory be different? I thought my couple of test functions showed that it was indeed where my application.properties file was. – Erin Mar 25 '22 at 10:24
  • 1
    What happens if you add the` new FileInputStream("application.properties")` lines directly after the `System.out.println(file.isDirectory());` line? – k314159 Mar 25 '22 at 11:01
  • *"I thought my couple of test functions showed that it was indeed where my application.properties file was."* - Maybe it does, maybe it doesn't. It depends on how the code is actually run, and we can't tell that from what you have shown us. For instance, if one version is being run in a webapp, and the other from a simple command, then it is *likely* that they will be running in different current directories. The only way to be sure is to combine it all into one class, run it and look at all of the output ... including the stacktrace – Stephen C Mar 25 '22 at 12:00
  • The problem is that lots of people get confused by how relative paths actually work. They say "it is definitely there" ... when actually they (or their program) is looking for the file in a different directory to what they expect. Another thing you could try is repeating your experiments using the correct **absolute** path for the properties file. – Stephen C Mar 25 '22 at 12:02
  • @StephenC @k314159 I changed the definition for the DatabaseDriver constructor to: `public DatabaseDriver() throws Exception` and it works perfectly! Thanks for your responses. Scrapping most of what I had anyway now since it was badly written/not scalable and adding HikariCP connection pooling. – Erin Mar 25 '22 at 22:29

0 Answers0