Scanner里的(System in)与(System.out)

使用scanner的方法获得用户从键盘输入的数据

Scanner类是JDK1.5中增加的一个类,用于扫描输入文本的实用程序。如果使用Scanner类,必须使用import语句导入Scanner类,位于java.util包中。

使用Scanner可以接收用户键盘输入的字符,

创建Scanner对象。

Scanner input = new Scanner(System.in);

String next()  获得一个字符串

int nextInt() 获得一个整型数值

double nextDouble() 获得一个双精度类型数值

boolean hasNext() 判断是否有输入数据,如果有输入数据,则返回true,否则返回false


1.标准输入

System.in作为InputStream类的对象实现标准输入,可以调用它的read方法来读取键盘数据。read方法见如下表格:

 

intread()
          从输入流中读取数据的下一个字节。
 intread(byte[] b)
          从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。
 intread(byte[] b, int off, int len)
          将输入流中最多 len 个数据字节读入 byte 数组。

第三个read方法参数说明:

b - 读入数据的缓冲区。
off - 数组  b 中将写入数据的初始偏移量。
len - 要读取的最大字节数。
    
备注:

如果输入流结束,返回-1。

发生I/O错时,会抛出IOException异常

Java通过系统类System实现标准输入/输出的功能,定义了3个流变量:in,out,和err.这3个流在Java中都定义为静态变量,可以直接通过System类进行调用。System.in表示标准输入,通常指从键盘输入数据;System.out表示标准输出,通常指把数据输出到控制台或者屏幕;System.err表示标准错误输出,通常指把数据输出到控制台或者屏幕。

1.简单标准输入 
System.in作为字节输入流类InputStream的对象实现标准输入,通过read()方法从键盘接受数据。 
int read() 
int read(byte b[]) 
int read(byte b[],int offset,int len)

import java.io.IOException;
public class StdInput{
    public static void main(String[] args) throws IOException 
    {
        System.out.println("input:");
        byte b[]=new byte[512];
        int count=System.in.read(b);
        System.out.println("Output");
        for(int i=0;i<count;i++)
        {
            System.out.print(b[i]+" ");
        }
        System.out.println();
        for(int i=0;i<count;i++)
        {
            System.out.print((byte)b[i]+" ");
        }
        System.out.println("count="+count);
    }
}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

结果 
input: 
abcd 
Output 
97 98 99 100 13 10 
97 98 99 100 13 10 count=6

分析:程序运行使,从键盘输入4个字符abcd并按Enter键,保存在缓冲区b中的元素个数count为6,Enter占用最后两个字节

2.Scanner类与标准输入结合 
在通常情况下需要从标准输入读取字符,整数,浮点数等具体类型的数据。System.in作为标准输入流,是InputStream类的一个对象,其read()方法的主要功能是读取字节和字节数组,不能直接得到需要的数据(如整型,浮点型)。此时,需要另外一个类java.util.Scanner的配合。可以利用Scanner类对标准输入流System.in的数据进行解析,得到需要的数据。


 

2.标准输出

System.out作为PrintStream打印流类的的对象实现标准输出,可以调用它的print、println或write方法来输出各种类型的数据。
print和println的参数完全一样,不同之处在于println输出后换行而print不换行。
write方法用来输出字节数组,在输出时不换行

在其祖先OutputStream类有如下方法

 voidclose()
          关闭此输出流并释放与此流有关的所有系统资源。
 voidflush()
          刷新此输出流并强制写出所有缓冲的输出字节。
 voidwrite(byte[] b)
          将 b.length 个字节从指定的 byte 数组写入此输出流。
 voidwrite(byte[] b, int off, int len)
          将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流。
abstract  voidwrite(int b)
          将指定的字节写入此输出流。

 

其中对于write(byte[] b, int off, int len)方法参数说明:

b - 数据。
off - 数据中的初始偏移量。
len - 要写入的字节数。

然而在PrintStream中重写了write方法

 void

write(byte[] buf, int off, int len)
          将 len 字节从指定的初始偏移量为 off 的 byte 数组写入此流。

其对于重写后write方法参数的说明:

buf - byte 数组
off - 相对于开始写入字节处的偏移量
len - 要写入的字节数
 

3.案例操作 

      案例说明:从myeclipse或者eclipse控制台输入数据,通过标准的输入读取,然后再通过标准的输出进行写出.

      1.代码实现

         

[java]  view plain  copy
  1. import java.io.IOException;  
  2. import java.io.InputStream;  
  3. import java.io.OutputStream;  
  4.   
  5. public class Demo {  
  6.   
  7.     public static void main(String[] args) {  
  8.   
  9.         InputStream is = System.in;// 标准的输入流对象 --读取操作  
  10.         OutputStream os = System.out;// 标准的输出流对象---写的操作  
  11.         try {  
  12.             byte[] buffer = new byte[10]; // 缓冲区 // 0 1 2 3 4 5 6 7 8 9  
  13.             int len = 0;// 读取之后的实际长度 //在UTF8编码下,回车\r 换行\n 也各占1个字节  
  14.             /* 
  15.              * read方法参数: b - 读入数据的缓冲区。 off - 数组 b 中将写入数据的初始偏移量。 len - 要读取的最大字节数。 
  16.              */  
  17.             while ((len = is.read(buffer, 04)) != -1) { //读取操作                                                  
  18.                 System.out.println("读取的实际长度--------------------------" + len);  
  19.                 os.write(buffer, 04); //写出的操作  
  20.                 System.out.println("--------------------------");  
  21.             }  
  22.         } catch (IOException e) {  
  23.             // TODO Auto-generated catch block  
  24.             e.printStackTrace();  
  25.         }  
  26.     }  
  27.   
  28. }  


备注说明:此程序处于一直堵塞的状态,所以没有进行输入流和输出流的关闭操作

2.操作及输出结果说明

 说明:

    1.在UTF-8编码下,回车 \r ,换行 \n 各占一个字节,当在控制台进行回车操作时会向输入流中写入这两个编码 

     2.程序在读取完数据后会留存buffer缓冲区中,火车换行替代了12 而34没有覆盖,所以也是说明最后为啥会有34存在的原因



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值