Java Utililty Methods Path File Read nio

List of utility methods to do Path File Read nio

Description

The list of methods to do Path File Read nio are organized into topic(s).

Method

voidreadAndConsume(String filePath, Consumer consumer)
read And Consume
Stream<String> lines = null;
lines = Files.lines(Paths.get(filePath));
lines.forEach(consumer);
StringreadAsString(Path path)
read As String
InputStream in = null;
try {
    in = Files.newInputStream(path);
    return new String(read(in));
} finally {
    if (in != null) {
        in.close();
StringreadAsString(String path)
read As String
try (BufferedReader reader = new BufferedReader(
        new InputStreamReader(new FileInputStream(path), StandardCharsets.UTF_8))) {
    String line = null;
    StringBuilder sb = new StringBuilder(4096);
    while ((line = reader.readLine()) != null) {
        sb.append(line).append('\n');
    return sb.toString();
...
Stringreader(String path)
reader
InputStream in = fileIn(path);
String val = getString(in).toString();
close(in);
return val;
String[]readers(String path)
readers
FileReader fileReader = new FileReader(path);
BufferedReader fr = new BufferedReader(fileReader);
String line = null;
List<String> list = new ArrayList<>();
while (null != (line = fr.readLine())) {
    list.add(line);
close(fileReader);
...
ListreadFile(Path directory, String... parts)
Reads the file under the directory/parts[0]/parts[1]/.../parts[n] using ISO_8859_1.
return readFile(resolve(directory, parts));
StringreadFile(Path file)
read File
StringBuilder strBuilder = new StringBuilder();
for (String line : Files.readAllLines(file, StandardCharsets.UTF_8)) {
    strBuilder.append(line).append('\n');
return strBuilder.toString();
StringreadFile(Path path)
read File
String pathToFile = path.toString(), line = "";
try (BufferedReader br = new BufferedReader(new FileReader(pathToFile))) {
    while (true) {
        try {
            line = line.concat(br.readLine());
        } catch (IOException ex) {
            ex.printStackTrace(System.err);
        } catch (NullPointerException ex) {
...
byte[]readFile(Path path)
Fully reads a file into a byte array.
try (final InputStream in = Files.newInputStream(path)) {
    return readStream(in);
StringreadFile(String path)
Reads the contents of a file into a string.
return read(new File(path));