I cannot seem to get Java to detect my text file. It keeps giving me a FileNotFoundException()
I have been able to get it to find PNG and TTF files before, but I am not sure if there is something special I need to do to get it to find text files in a resource folder.
I am using Eclipse 2018-19 (4.9.0) with OpenJDK 11.
How can I make my program manage to find and utilize this text file?
MCV Example:
public static main(String[] args) {
generateHumanName();
}
/**
* Generates a male "human-ish" name from a text file of syllables.
* @return String The male human name.
*/
private static String generateHumanName() {
try {
FileReader("/text/male_human_name_syllables.txt/");
} catch (IOException e) {
e.printStackTrace();
}
return null; // never called
}
Exception Received:
java.io.FileNotFoundException: ../res/text/male_human_name_syllables.txt (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at java.io.FileReader.<init>(FileReader.java:58)
at mcve.MCVE.generateHumanName(MCVE.java:21)
at mcve.MCVE.main(MCVE.java:12)
My File Path:
You are putting this file in a resource folder (res
): this is different than browsing for a file on the local file system (see below):
You need to use MCV.class.getResourceAsStream("/text/male_human_name_syllables.txt")
:
try (InputStream is = MCVE.class.getResourceAsStream("/text/male_human_name_syllables.txt")) {
if (null == is) {
throw new FileNotFoundException("/text/male_human_name_syllables.txt");
}
try (BufferedReader in = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
// do stuff here
}
}
I won't delve too much on getResourceAsStream
, the javadoc will do better explanation, but:
null
if the resource does not exists. You may need to move the test outside the try with resources.bin
by default)./text
), it will be relative to the class (and package) on which you invoke the getResourceAsStream
method.Alternatively, if you want to read the file at some random location, you should pass it (eg: configure Eclipse to execute your program with some args) to your program and read it:
public static main(String[] args) {
if (args.length < 1) throw new IllegalArgumentException("missing path");
generateHumanName(args[0]);
}
/**
* Generates a male "human-ish" name from a text file of syllables.
* @return String The male human name.
*/
private static String generateHumanName(String path) {
try (FileReader reader = new FileReader(path)) {
} catch (IOException e) {
e.printStackTrace();
}
return null; // never called
}
Otherwise, you must move the text
folder to the root of your project (where the res
folder is), refresh your project, and use text/male_human_name_syllables.txt
(because that is an absolute path).
The res/text/male_human_name_syllables.txt
would probably work (because it executes from the root of the project).