Java Path File Read nio readFile(Path directory, String... parts)

Here you can find the source of readFile(Path directory, String... parts)

Description

Reads the file under the directory/parts[0]/parts[1]/.../parts[n] using ISO_8859_1.

License

Open Source License

Parameter

Parameter Description
directory root directory for resolve
parts parts of the path relative to directory

Exception

Parameter Description
IOException in case file can not be read

Return

the List of lines of the file

Declaration

public static List<String> readFile(Path directory, String... parts) throws IOException 

Method Source Code


//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import java.nio.file.Files;
import java.nio.file.Path;

import java.util.List;

public class Main {
    /**// w w  w  . ja v a2  s.  c o m
     * Reads the file under the <code>directory/parts[0]/parts[1]/.../parts[n]</code>
     * using ISO_8859_1.
     *
     * @param directory root directory for resolve
     * @param parts parts of the path relative to directory
     * @return the List<String> of lines of the file
     * @throws IOException in case file can not be read
     */
    public static List<String> readFile(Path directory, String... parts) throws IOException {
        return readFile(resolve(directory, parts));
    }

    /**
     * Reads the <code>file</code> using ISO_8859_1.
     *
     * @param file file to read
     * @return the List<String></String> of lines of the file
     * @throws IOException in case file can not be read
     */
    public static List<String> readFile(Path file) throws IOException {
        return Files.readAllLines(file, StandardCharsets.ISO_8859_1);
    }

    /**
     * Resolves the Path to the file or directory under the <code>
     * directory/parts[0]/parts[1]/.../parts[n]</code>.
     *
     * @param directory root directory for resolve
     * @param parts parts of the path relative to directory
     * @return resolved Path
     */
    public static Path resolve(Path directory, String... parts) {
        Path current = directory;
        for (String part : parts) {
            current = current.resolve(part);
        }
        return current;
    }
}

Related

  1. readAndConsume(String filePath, Consumer consumer)
  2. readAsString(Path path)
  3. readAsString(String path)
  4. reader(String path)
  5. readers(String path)
  6. readFile(Path file)
  7. readFile(Path path)
  8. readFile(Path path)
  9. readFile(String path)