What I'm wanting to do is create a String readFile(Path filePath, Charset encoding)
method, that returns the entire contents of a given file.
I'm wanting the equivalent of Guava's Files.toString(file, encoding)
, but without calling the Path.getFile()
method (as this method only works with the default filesystem provider, and in this particular case I'm wanting to write tests that use JimFS).
I've written the following, but I'm wondering if it can be improved:
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import java.nio.file.Files;
⋮
public String readFile(Path path, Charset encoding) throws IOException {
Preconditions.checkState(Files.exists(path), "File does not exist: %s", path);
Preconditions.checkState(Files.isRegularFile(path), "File is not regular file: %s", path);
return Joiner.on(System.lineSeparator()).join(Files.readAllLines(path, encoding));
}
Is there a better way to read the file in (such as without using Files.readAllLines()
which results in a slightly awkward List<String>
). I'd previously joined this result together by streaming the lines of the file, and then immediately collecting them with Collectors.joining(System.lineSeparator())
, but since the project has a dependency on Guava for other purposes, it seems preferable to use the Joiner
available.
I'm happy to restrict this to Java 8 and onwards if needed, and as previously mentioned I already have a dependency on Guava, but I'd rather not add too many dependencies if possible.