The readString() method of File Class in Java is used to read contents to the specified file. Return Value: This method returns the content of the file in String format. Note: File. readString() method was introduced in Java 11 and this method is used to read a file's content into String.
The FileUtils. readFileToString() is an excellent way to read a whole file into a String in a single statement.
readString() method, which can read all characters from a file into a string. It takes a path to the file and overloaded to accept the charset used for decoding from bytes to characters.
The readAllLines() method of the Files class allows reading the whole content of the file and stores each line in an array as strings.
Yes, you can do this in one line (though for robust IOException
handling you wouldn't want to).
String content = new Scanner(new File("filename")).useDelimiter("\\Z").next();
System.out.println(content);
This uses a java.util.Scanner
, telling it to delimit the input with \Z
, which is the end of the string anchor. This ultimately makes the input have one actual token, which is the entire file, so it can be read with one call to next()
.
There is a constructor that takes a File
and a String charSetName
(among many other overloads). These two constructor may throw FileNotFoundException
, but like all Scanner
methods, no IOException
can be thrown beyond these constructors.
You can query the Scanner
itself through the ioException()
method if an IOException
occurred or not. You may also want to explicitly close()
the Scanner
after you read the content, so perhaps storing the Scanner
reference in a local variable is best.
For completeness, these are some really good options if you have these very reputable and highly useful third party libraries:
com.google.common.io.Files
contains many useful methods. The pertinent ones here are:
String toString(File, Charset)
String
List<String> readLines(File, Charset)
List<String>
, one entry per lineorg.apache.commons.io.IOUtils
also offer similar functionality:
String toString(InputStream, String encoding)
InputStream
as a String
List readLines(InputStream, String encoding)
List
of String
, one entry per lineFrom Java 7 (API Description) onwards you can do:
new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);
Where filePath is a String representing the file you want to load.
You can use apache commons IO..
FileInputStream fisTargetFile = new FileInputStream(new File("test.txt"));
String targetFileStr = IOUtils.toString(fisTargetFile, "UTF-8");
This should work for you:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public static void main(String[] args) throws IOException {
String content = new String(Files.readAllBytes(Paths.get("abc.java")));
}
Using Apache Commons IO.
import org.apache.commons.io.FileUtils;
//...
String contents = FileUtils.readFileToString(new File("/path/to/the/file"), "UTF-8")
You can see de javadoc for the method for details.
Don't write your own util class to do this - I would recommend using Guava, which is full of all kinds of goodness. In this case you'd want either the Files
class (if you're really just reading a file) or CharStreams for more general purpose reading. It has methods to read the data into a list of strings (readLines
) or totally (toString
).
It has similar useful methods for binary data too. And then there's the rest of the library...
I agree it's annoying that there's nothing similar in the standard libraries. Heck, just being able to supply a CharSet
to a FileReader
would make life a little simpler...
Another alternative approach is:
How do I create a Java string from the contents of a file?
Other option is to use utilities provided open source libraries
http://commons.apache.org/io/api-1.4/index.html?org/apache/commons/io/IOUtils.html
Why java doesn't provide such a common util API ?
a) to keep the APIs generic so that encoding, buffering etc is handled by the programmer.
b) make programmers do some work and write/share opensource util libraries :D ;-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With