Java Scanner read text file by encoding
import java.io.File; import java.util.Scanner; public class Main { public static void main(String[] args) throws Exception{ String path = "Main.java"; Scanner scanner = new Scanner( new File(path) ); String result = scanner.useDelimiter("\\A").next(); System.out.println(result);/*from w w w . j a v a2 s. c om*/ scanner.close(); /* Scanner scanner = new Scanner( new File(path), "US-ASCII" ); String result = scanner.useDelimiter("\\A").next(); scanner.close(); Scanner scanner = new Scanner( new File(path), "ISO-8859-1" ); String result = scanner.useDelimiter("\\A").next(); scanner.close(); Scanner scanner = new Scanner( new File(path), "UTF-8" ); String result = scanner.useDelimiter("\\A").next(); scanner.close(); Scanner scanner = new Scanner( new File(path), "UTF-16BE" ); String result = scanner.useDelimiter("\\A").next(); scanner.close(); Scanner scanner = new Scanner( new File(path), "UTF-16LE" ); String result = scanner.useDelimiter("\\A").next(); scanner.close(); Scanner scanner = new Scanner( new File(path), "UTF-16" ); String result = scanner.useDelimiter("\\A").next(); scanner.close(); */ } }