一、标准输入流
Scanner 是作为一个扫描器,它能够以标准格式以及扫描器语言环境的格式的指定文件、流中读取数据。
常用构造方法:
Scanner(File source):构造一个新的 Scanner,它生成的值是从指定文件扫描的
Scanner(InputStream source):构造一个新的 Scanner,它生成的值是从指定的输入流扫描的
Scanner(Readable source):构造一个新的 Scanner,它生成的值是从指定源扫描的
Scanner(String source):构造一个新的 Scanner,它生成的值是从指定字符串扫描的
而默认情况下是从键盘输入的数据中扫描,即 System.in 这其实 是 System类中的一个输入流。
Demo:
1 @Test
2 public void test01(){
3 Scanner input = new Scanner(System.in);
4 System.out.print("请输入一个整数:"); //从键盘输入
5 int num = input.nextInt();
6 System.out.println("num = " + num);
7 input.close();
8 }
9
10 @Test
11 public void test02() throws FileNotFoundException{
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 @Test
23 public void test03() throws FileNotFoundException{
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 @Test
35 public void test04() throws FileNotFoundException{
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 @Test
48 public void test05() throws FileNotFoundException{
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 @Test
2 public void test01(){
3 PrintStream out = System.out;
4 System.out.println(out);
5 }
6 @Test
7 public void test02() throws FileNotFoundException{
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 import java.io.FileDescriptor;
2 import java.io.FileInputStream;
3 import java.util.Scanner;
4
5 public class TestSystemIn {
6 public static void main(String[] args) throws Exception{
7 //重定向从文件输入
8 System.setIn(new FileInputStream("java\info.txt"));
9 Scanner input = new Scanner(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(new FileInputStream(FileDescriptor.in));
18 }
19 }
20
21
22 import java.io.FileDescriptor;
23 import java.io.FileOutputStream;
24 import java.io.PrintStream;
25
26 public class TestSystemOut {
27 public static void main(String[] args) throws Exception{
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(new FileOutputStream(FileDescriptor.out)));
34 System.out.println("java");
35 }
36 }