1 import java.io.*;
2
3 /**
4 * Created by wojia on 2017/7/6.
5 */
6 public class FileReaderDemo {
7 public static void main(String args[]) {
8 //先创建srcfile
9 File srcFile = new File( "E:/test/test.txt" );
10 char[] cbuffer = new char[1024];
11 int length = -1;
12 StringBuffer sb = new StringBuffer( 1024 );
13 try(
14 /**由于ansi编码问题
15 * 直接使用FileInputStream , print时出现乱码现象
16 * 又 FIleInputStream的 没有包含 charset 设置的 constructor
17 * 因此,使用InputStreamReader 的带charset的 构造器
18 * 至于这些IO 的 class什么关系 暂时没学到 有点乱
19 *Class FileInputStream:
20 * java.lang.Object
21 * java.io.InputStream
22 * java.io.FileInputStream >>>
23 *
24 * Class FileReader:
25 * java.lang.Object
26 * java.io.Reader
27 * java.io.InputStreamReader >>>
28 * java.io.FileReader
29 * */
30 Reader in = new InputStreamReader( new FileInputStream( srcFile ),"GBK" )
31
32 )
33 {
34 /**但读入的不为空*/
35 while((length = in.read( cbuffer )) != -1){
36 sb.append( cbuffer,0,length );
37 System.out.print(sb);
38
39
40 }
41
42 }catch(Exception e){
43 e.printStackTrace();
44 }
45
46
47 }
48 }