Java Scanner(File source, String charsetName) Constructor

Syntax

Scanner(File source, String charsetName) constructor from Scanner has the following syntax.

public Scanner(File source,   String charsetName)  throws FileNotFoundException

Example

In the following code shows how to use Scanner.Scanner(File source, String charsetName) constructor.


import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
/*from   w  w  w.j  ava 2s  .c  o m*/
public class Main {

   public static void main(String[] args) throws Exception {
      Scanner scanner = new Scanner(new File("c:/text.txt"),StandardCharsets.UTF_8.name());
      System.out.println(scanner.nextLine());

      scanner.close();
   }
}