Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

url

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?

like image 952
yegor256 Avatar asked Dec 22 '22 16:12

yegor256


1 Answers

If you have apache IOUtils

    URL url = new URL("jar:file:/path/my.jar!/META-INF/file.txt");
    String myString = IOUtils.toString(url);
like image 83
Bala R Avatar answered Feb 09 '23 00:02

Bala R