java标准流类_Java 之 标准输入流 与 标准输出流

一、标准输入流

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 类中,发现里面有三个常量字段。

4c640a971927360bd5abf064deaeef41.png   “标准”输入流 System.in

6200003cf781ae1944237ab305dc65e5.png   “标准”输出流 System.out

9bc030519654f1eb57254522e48b2fc5.png   “标准”错误输出流  System.err

可以看到这三个字段都用 final修饰的,是常量,并被赋值为 null,但是打印的时候,它们并不是 null。

接着发现了这三个方法:(重定向)

0aca5279025528279bef712fd756c339.png

303e6877ec08bdcb092e5a9b070cdfd2.png

e00308ba9131155d69de3c3a2ea39747.png

16bb1b86ba30c18fd9663e4965af5470.png

8e0aff96a7c9a7335f8410335de2c09a.png

通过上面的这些方法可以看到,上面的三个字段,在Java层面是常量对象,但是底层调用本地方法(调用C等底层语言)进行修改了。

输出默认到控制台,其他控制台是个文件,查看 System类中的 initializeSystemClass() 方法

1f041102b97381189216115097c8f447.png

这个方法里面有上面这三个方法,分别是把它们指向控制台。

而对于输出流 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 }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值