Code Example
Reading a file by using BufferReader, You would done in few line of code, Here is the working example code.
import java.io.BufferedReader;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class SampleTest {
public static void main(String[] args) {
Path path = null;
try {
path = Paths.get(new URI("file:///C:/java/example/test.txt"));
} catch (URISyntaxException e) {
System.out.println("Bad URI");
}
try (BufferedReader inputReader = Files.newBufferedReader(path, Charset.defaultCharset())) {
String inputLine;
while ((inputLine = inputReader.readLine()) != null) {
System.out.println(inputLine);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Leave A Comment