javaandroidfileinputstreamfileinputstream

Is there a way to use getResourceAsStream method to read a json file present in data directory path?


I have a file present in data directory path like this: /data/user/0/com.example.myapplication/files/data/en-US/ProjectStrings.json.

This is what I am using to get the input stream:

InputStream ins = ProjectManager.class.getResourceAsStream("/data/user/0/com.example.myapplication/files/data/en-US/").

Here ins is coming as null.

Maybe the path that I am giving to getResourceAsStream is not correct. Please suggest what should the relative path that I should provide to this api to read ProjectStrings.json file.


Solution

  • No, Class.getResourceAsStream(…) will only work for resources present on the application‘s classpath.

    To get a stream for a file at an arbitrary filesystem location, use a FileInputStream:

    try (var fis = new FileInputStream(pathToYourFile)) { … }