Read text file with Buffere and set default charset by using java.nio.file.Files in Java

Description

The following code shows how to read text file with Buffere and set default charset by using java.nio.file.Files.

Example


//from  w  w w  .j a v a  2  s .c  o m
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 Main {

  public static void main(String[] args) {
    Path path = null;
    try {
        path = Paths.get(new URI("file:///C:/home/docs/users.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();
    }

  }
}




















Home »
  Java Tutorial »
    I/O »




Binary File
Byte Array
CharSet
Checksum
Console
Create Copy Move Delete
Directory
Drive
Encode Decode
File Attribute
File Lock
File System
GZIP
Jar File
NIO Buffer
Path
Scanner
StreamTokenizer
Temporary File
Text File
Zip