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

BufferedReaderopenBufferedReader(String pathOrUrl)
open Buffered Reader
InputStream inputStream;
if (isRemote(pathOrUrl)) {
    URL url = new URL(pathOrUrl);
    inputStream = openConnection(url).getInputStream();
} else {
    inputStream = new FileInputStream(pathOrUrl);
return new BufferedReader(new InputStreamReader(inputStream, Charset.defaultCharset()));
...
ByteChannelopenForReadingAndWriting(String filePath)
Opens a file for writing.
Set<OpenOption> opts = new HashSet<>();
opts.add(StandardOpenOption.CREATE);
opts.add(StandardOpenOption.WRITE);
opts.add(StandardOpenOption.READ);
Path path = Paths.get(filePath);
return Files.newByteChannel(path, opts);
ReaderopenResourceReader(Class resourceOrigin, String resourcePath)
Creates a reader for reading the specified class loader resource.
InputStream in = resourceOrigin.getResourceAsStream(resourcePath);
if (in == null) {
    throw new FileNotFoundException("File not found: " + resourcePath);
try {
    Reader reader = new InputStreamReader(in, CHARSET);
    in = null;
    return reader;
...
Stringread(String aPathString)
read
StringBuilder sb = new StringBuilder();
Path path = Paths.get(aPathString);
try (BufferedReader reader = Files.newBufferedReader(path, Charset.defaultCharset())) {
    String line = reader.readLine();
    while (line != null) {
        sb.append(line);
        sb.append(System.lineSeparator());
        line = reader.readLine();
...
Objectread(String filePath)
read
Path path = Paths.get(filePath);
try (ObjectInputStream input = new ObjectInputStream(Files.newInputStream(path))) {
    return input.readObject();
Stringread(String filePath)
read
return read(new File(filePath));
Stringread(String path)
read
String content = "";
List<String> lines = Files.readAllLines(Paths.get(path), StandardCharsets.UTF_8);
for (String line : lines) {
    content += line + "\n";
return content;
ListreadAllLines(Path path)
read All Lines
return Files.readAllLines(path, StandardCharsets.UTF_8);
ListreadAllLines(String path)
Read file at path.
List<String> lines = new ArrayList<String>();
CharsetEncoder encoder = Charset.forName(StandardCharsets.ISO_8859_1.toString()).newEncoder();
boolean ignoreBOM = true; 
BufferedReader br = null;
String currentLine;
int lineNumber = 1;
try {
    br = new BufferedReader(new InputStreamReader(new FileInputStream(path), StandardCharsets.UTF_8));
...
ListreadAllLinesOrExit(Path path)
read All Lines Or Exit
try {
    return Files.readAllLines(path, Charset.forName("UTF-8"));
} catch (IOException e) {
    System.err.println("Failed to read [" + path + "]: " + e.getMessage());
    System.exit(0);
return null;