标准输入输出流(输入流)
标准输入流
- System类中有两个静态的成员变量
图文:
- public static final InputStream in:标准输入流。通常该留对应于键盘输入或由主机环境或用户指定的另一个输入源
- public static final PrintStream out :标准输出流。通常该留对应于显示输出或由主机环境或用户指定的另一个输出目标
- 自己实现键盘录入数据
BufferedReader by=new BufferedRead(new InputStreamReader(System.in));
以图文和代码内容形式讲解
BufferedReader by=new BufferedRead(new InputStreamReader(System.in));
由来:
package Demo;import java.io.*;public class Demo { public static void main(String[] args) throws IOException { //1 InputStream in = System.in;//public static final InputStream in 静态/常量 //按字节可以读取字符串 int len; byte[] bytes = new byte[1024]; while((len=in.read(bytes))!=-1){ System.out.print(new String(bytes,0,len)); } //2 //但是如果按一个字符读取那就得需要转换!! InputStreamReader isr = new InputStreamReader(in); int s; while((s=isr.read())!=-1){ System.out.print((char)s); } //3 //如果是字符缓冲输入流 BufferedReader br = new BufferedReader(isr); int sr; while((sr=br.read())!=-1){ System.out.print((char)sr); } //4 把上述我们可以合为一体 BufferedReader by=new BufferedReader(new InputStreamReader(System.in)); int oo; System.out.println("请输入数据:"); while((oo=by.read())!=-1){ System.out.print((char)oo); } } }
图文解释:
输入的内容:
哈哈哈,当然写起来我们是感觉很麻烦于是Java给我们提供了Scanner类
Scanner sc=new Scanner(System.in);