一、標准輸入流
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 }