一、标准输入流
Scanner 是作为一个扫描器,它能够以标准格式以及扫描器语言环境的格式的指定文件、流中读取数据。
常用构造方法:
Scanner(File source):构造一个新的 Scanner,它生成的值是从指定文件扫描的
Scanner(InputStream source):构造一个新的 Scanner,它生成的值是从指定的输入流扫描的
Scanner(Readable source):构造一个新的 Scanner,它生成的值是从指定源扫描的
Scanner(String source):构造一个新的 Scanner,它生成的值是从指定字符串扫描的
而默认情况下是从键盘输入的数据中扫描,即 System.in 这其实 是 System类中的一个输入流。
Demo:
1 @Test2 public voidtest01(){3 Scanner input = newScanner(System.in);4 System.out.print("请输入一个整数:"); //从键盘输入5 int num =input.nextInt();6 System.out.println("num = " +num);7 input.close();8 }9
10 @Test11 public void test02() throwsFileNotFoundException{12 Scanner input = new Scanner(new FileInputStream("1.txt"));//InputStream
13
14 while(input.hasNextLine()){15 String line =input.nextLine();16 System.out.println(line);17 }18
19 input.close();20 }21
22 @Test23 public void test03() throwsFileNotFoundException{24 Scanner input = new Scanner(new File("1.txt")); //InputStream
25
26 while(input.hasNextLine()){27 String line =input.nextLine();28 System.out.println(line);29 }30
31 input.close();32 }33
34 @Test35 public void test04() throwsFileNotFoundException{36 Scanner input = new Scanner("1.txt"); //InputStream
37
38 while(input.hasNextLine()){39 String line =input.nextLine();40 System.out.println(line);41 }42
43 input.close();44 }45
46
47 @Test48 public void test05() throwsFileNotFoundException{49 Scanner input = new Scanner(new File("d:/1.txt"),"GBK");//使用InputStream,并指定字符集
50
51 while(input.hasNextLine()){52 String line =input.nextLine();53 System.out.println(line);54 }55
56 input.close();57 }
二、标准输出流
进入到 System 类中,发现里面有三个常量字段。
“标准”输入流 System.in
“标准”输出流 System.out
“标准”错误输出流 System.err
可以看到这三个字段都用 final修饰的,是常量,并被赋值为 null,但是打印的时候,它们并不是 null。
接着发现了这三个方法:(重定向)
通过上面的这些方法可以看到,上面的三个字段,在Java层面是常量对象,但是底层调用本地方法(调用C等底层语言)进行修改了。
输出默认到控制台,其他控制台是个文件,查看 System类中的 initializeSystemClass() 方法
这个方法里面有上面这三个方法,分别是把它们指向控制台。
而对于输出流 Out,我们还以为给它指定一个新的目标。
Demo:
1 @Test2 public voidtest01(){3 PrintStream out =System.out;4 System.out.println(out);5 }6 @Test7 public void test02() throwsFileNotFoundException{8 System.setOut(new PrintStream("1.txt")); //指定输出流,重定向9
10 System.out.println("aaaa");11 System.out.println("bbb");12 System.out.println("ccc");13 System.out.println("ddd");14 }
Demo : 重定向 System.in 和 System.out
1 importjava.io.FileDescriptor;2 importjava.io.FileInputStream;3 importjava.util.Scanner;4
5 public classTestSystemIn {6 public static void main(String[] args) throwsException{7 //重定向从文件输入
8 System.setIn(new FileInputStream("java\\info.txt"));9 Scanner input = newScanner(System.in);10 while(input.hasNext()){11 String str =input.nextLine();12 System.out.println(str);13 }14 input.close();15
16 //重定向回键盘输入
17 System.setIn(newFileInputStream(FileDescriptor.in));18 }19 }20
21
22 importjava.io.FileDescriptor;23 importjava.io.FileOutputStream;24 importjava.io.PrintStream;25
26 public classTestSystemOut {27 public static void main(String[] args) throwsException{28 System.out.println("hello");29 //重定向输出到文件
30 System.setOut(new PrintStream("java\\print.txt"));31 System.out.println("world");32 //重定向回控制台
33 System.setOut(new PrintStream(newFileOutputStream(FileDescriptor.out)));34 System.out.println("java");35 }36 }