Quantcast
Viewing latest article 10
Browse Latest Browse All 36

Answer by OscarRyz for How do I create a Java string from the contents of a file?

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");

Viewing latest article 10
Browse Latest Browse All 36

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>