Also if your file happens to be inside a jar, you can also use this:
public String fromFileInJar(String path) { try ( Scanner scanner = new Scanner(getClass().getResourceAsStream(path))) { return scanner.useDelimiter("\\A").next(); }}
The path should start with /
for instance if your jar is
my.jar/com/some/thing/a.txt
Then you want to invoke it like this:
String myTxt = fromFileInJar("/com/com/thing/a.txt");