java标准流类_Java IO流-标准输入输出流

2017-11-05 19:13:21

标准输入输出流:System类中的两个成员变量。

标准输入流(public static final InputStreamin):“标准”输入流。此流已打开并准备提供输入数据。通常,此流对应于键盘输入或者由主机环境或用户指定的另一个输入源。

InputStream is = System.in;

标准输出流(public static finalPrintStreamout):“标准”输出流。此流已打开并准备接受输出数据。通常,此流对应于显示器输出或者由主机环境或用户指定的另一个输出目标。

PrintStream ps = System.out;

System.out

标准输出流本质是PrintStream类,下面来看一下PrintStream类的一些方法。

*常用方法

a9569416911fde218ef3bc5a978fbb4e.png

2fa2136a17f6423f04ae6f649654cd7c.png

a399e36c03d060217cc5b47ef32a029a.png

public static void main(String[] args) {

System.out.println("hello world.");

//获取标准输出流对象

PrintStream ps = System.out;

ps.println("hello world");

}

使用字符缓冲流进行包装(很少用,因为不方便):

public static void main(String[] args) throws IOException {

BufferedWriter bw= new BufferedWriter(new OutputStreamWriter(System.out));

bw.write("hello");

bw.newLine();

bw.write("world");

bw.newLine();

bw.write("!");

bw.flush();

bw.close();

}

System.in

标准输入流本质是InputStream类。

键盘录入的方法:

A:main方法接收参数

B:Scanner类

Scanner sc = new Scanner(System.in)

int x = sc.nextInt()

C:通过字符缓冲流包装标准输入流,BufferedReader类

BufferedReader br = new BufferedReader(new InputStreamReader(System.in))

public static void main(String[] args) throws IOException {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

// 读到末尾返回null

String line = br.readLine();

int i = Integer.parseInt(line);

System.out.println(i);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值