Using newBufferedReader() Method to read files through a buffer via the UTF-8 charset - Java File Path IO

Java examples for File Path IO:File Operation

Introduction

The following code snippet reads the wiki.txt file using the UTF-8 charset:

Demo Code

import java.io.BufferedReader;
import java.io.IOException;
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 wiki_path = Paths.get("C:/folder1/wiki", "wiki.txt");
    Charset charset = Charset.forName("UTF-8");
    try (BufferedReader reader = Files.newBufferedReader(wiki_path, charset)) {
      String line = null;//  w  w w  .  jav a  2  s.  c o  m
      while ((line = reader.readLine()) != null) {
        System.out.println(line);
      }
    } catch (IOException e) {
      System.err.println(e);
    }
  }
}

Result


Related Tutorials