Close

Java IO & NIO - Files.newBufferedReader() Examples

Java IO & NIO Java Java API 


Class:

java.nio.file.Files

java.lang.Objectjava.lang.Objectjava.nio.file.Filesjava.nio.file.FilesLogicBig

Methods:

public static BufferedReader newBufferedReader(Path path,
                                               Charset cs)
                                        throws IOException

Opens a file for reading, returning a BufferedReader that may be used to read text from the file in an efficient manner.

Parameters:
path - the path to the file
cs - the charset to use for decoding
Returns:
a new buffered reader, with default buffer size, to read text from the file



public static BufferedReader newBufferedReader(Path path)
                                        throws IOException

Opens a file for reading, returning a BufferedReader to read text from the file in an efficient manner.

Parameters:
path - the path to the file
Returns:
a new buffered reader, with default buffer size, to read text from the file


Examples


package com.logicbig.example.files;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class NewBufferedReaderExample {

public static void main(String... args) throws IOException {
Path path = Files.write(Files.createTempFile("test", ".txt"),
"test content".getBytes());
System.out.println("Using newBufferedReader for path: " + path);
System.out.println("Reading lines: ");
try (BufferedReader bufferedReader = Files.newBufferedReader(path)) {
bufferedReader.lines().forEach(System.out::println);
}
}
}

Output

Using newBufferedReader for path: C:\Users\Joe\AppData\Local\Temp\test9978513373578826701.txt
Reading lines:
test content




See Also