Answer by Henry for How do I create a Java string from the contents of a file?
Be aware when using fileInputStream.available() the returned integer does not have to represent the actual file size, but rather the guessed amount of bytes the system should be able to read from the...
View ArticleAnswer by user590444 for How do I create a Java string from the contents of a...
import java.nio.file.Files;....... String readFile(String filename) { File f = new File(filename); try { byte[] bytes = Files.readAllBytes(f.toPath()); return new String(bytes,"UTF-8"); } catch...
View ArticleAnswer by wau for How do I create a Java string from the contents of a file?
A flexible solution using IOUtils from Apache commons-io in combination with StringWriter:Reader input = new FileReader();StringWriter output = new StringWriter();try { IOUtils.copy(input, output);}...
View ArticleAnswer by barjak for How do I create a Java string from the contents of a file?
This one uses the method RandomAccessFile.readFully, it seems to be available from JDK 1.0 !public static String readFileContent(String filename, Charset charset) throws IOException { RandomAccessFile...
View ArticleAnswer by Home in Time for How do I create a Java string from the contents of...
If it's a text file why not use apache commons-io? It has the following methodpublic static String readFileToString(File file) throws IOExceptionIf you want the lines as a list usepublic static...
View ArticleAnswer by Pablo Grisafi for How do I create a Java string from the contents...
A very lean solution based on Scanner:Scanner scanner = new Scanner( new File("poem.txt") );String text = scanner.useDelimiter("\\A").next();scanner.close(); // Put this call in a finally blockOr, if...
View ArticleAnswer by Peter Lawrey for How do I create a Java string from the contents of...
To read a File as binary and convert at the endpublic static String readFileAsString(String filePath) throws IOException { DataInputStream dis = new DataInputStream(new FileInputStream(filePath)); try...
View ArticleAnswer by finnw for How do I create a Java string from the contents of a file?
Guava has a method similar to the one from Commons IOUtils that Willi aus Rohr mentioned:import com.google.common.base.Charsets;import com.google.common.io.Files;// ...String text = Files.toString(new...
View ArticleAnswer by Scott S. McCoy for How do I create a Java string from the contents...
public static String slurp (final File file)throws IOException { StringBuilder result = new StringBuilder(); BufferedReader reader = new BufferedReader(new FileReader(file)); try { char[] buf = new...
View ArticleAnswer by Dan Dyer for How do I create a Java string from the contents of a...
There is a variation on the same theme that uses a for loop, instead of a while loop, to limit the scope of the line variable. Whether it's "better" is a matter of personal taste.for(String line =...
View ArticleAnswer by Jon Skeet for How do I create a Java string from the contents of a...
That code will normalize line breaks, which may or may not be what you really want to do.Here's an alternative which doesn't do that, and which is (IMO) simpler to understand than the NIO code...
View ArticleAnswer by Dónal for How do I create a Java string from the contents of a file?
If you're looking for an alternative that doesn't involve a third-party library (e.g. Commons I/O), you can use the Scanner class:private String readFile(String pathname) throws IOException { File file...
View ArticleAnswer by erickson for How do I create a Java string from the contents of a...
Read all text from a fileJava 11 added the readString() method to read small files as a String, preserving line terminators:String content = Files.readString(path, encoding);For versions between Java 7...
View ArticleAnswer by Claudiu for How do I create a Java string from the contents of a file?
Java attempts to be extremely general and flexible in all it does. As a result, something which is relatively simple in a scripting language (your code would be replaced with "open(file).read()" in...
View ArticleAnswer by DaWilli for How do I create a Java string from the contents of a file?
If you're willing to use an external library, check out Apache Commons IO (200KB JAR). It contains an org.apache.commons.io.FileUtils.readFileToString() method that allows you to read an entire File...
View ArticleHow do I create a Java string from the contents of a file?
I've been using the idiom below for some time now. And it seems to be the most wide-spread, at least on the sites I've visited.Is there a better/different way to read a file into a string in...
View Article