yegor256
yegor256

Reputation: 105193

How to read file (addressed by URL) into string, in java?

This is the code:

URL url = new URL("jar:file:/path/my.jar!/META-INF/file.txt");
File file = File.createTempFile("foo", ".txt");
file.deleteOnExit();
FileUtils.copyURLToFile(url, file);
String txt = FileUtils.readFileToString(file);

Can I do the same with less lines of code?

Upvotes: 4

Views: 8320

Answers (1)

Bala R
Bala R

Reputation: 109017

If you have apache IOUtils

    URL url = new URL("jar:file:/path/my.jar!/META-INF/file.txt");
    String myString = IOUtils.toString(url);

Upvotes: 11

Related Questions