当前位置:网站首页>标准输入输出流(System.in,System.out)
标准输入输出流(System.in,System.out)
2022-07-30 05:41:00 【想买CT5的小曹】
1.标准输入流(System.in)
首先这个标准输入流的默认输入设备是键盘,数据源是键盘,下面看一个简单的程序。
package fuxi;
import java.io.IOException;
import java.io.InputStream;
public class StandardInputOutput {
public static void main(String[] args) throws IOException {
InputStream in = System.in;
int read = in.read();
System.out.println((char) read);
}
}
这一段程序是通过键盘录入读取,但是读取的只有一个。

要是想读取多个可以设置一个while循环,对录入的进行判断是否是-1.
package fuxi;
import java.io.IOException;
import java.io.InputStream;
public class StandardInputOutput {
public static void main(String[] args) throws IOException {
InputStream in = System.in;
int read;
while ((read = in.read()) != -1)
//一开始是打印int类型的,用char进行强制转换
System.out.print((char) read);
}
}

在这里可以看见的是录入的字母或者数字都可以正常打印,但是录入要是中文就是不行,出现乱码,因为这个InputStream是字节流
,为了解决这个问题你可以用转换流进行转换。
package fuxi;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class StandardInputOutput {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();
System.out.println(input);
}
}

在这里可以看见用转换流使用成功了,这里可以输入字母数字中文也可以输入不会乱码,原因在于BufferedReader,他继承于Reader,这是字符输入流。

2.标准输出流(System.out)
package fuxi;
import java.io.*;
public class StandardInputOutput {
public static void main(String[] args) throws IOException {
PrintStream out = System.out;
out.print(true);
out.print(99);
out.print("我爱你啊你爱我蜜雪冰城甜蜜蜜");
out.println("------");
out.println(99);
out.close();
}
}

这是可以看到是换行和没有换行的区别
print就是不换行而println就换行了。
边栏推荐
猜你喜欢

Dcat Admin 安装

秒杀项目的总结及面试常见问题

3 minutes to tell you how to become a hacker | Zero foundation to hacker introductory guide, you only need to master these five skills
文件上传漏洞的绕过

DVWA安装教程(懂你的不懂·详细)
![[PASECA2019]honey_shop](/img/8f/7161a63dab10dc02fef1fea075401a.png)
[PASECA2019]honey_shop

uni-app:关于自定义组件、easycom规范、uni_modules等问题

【小程序项目开发-- 京东商城】uni-app之分类导航区域

强国杯初赛WP

函数解剖——深挖getchar()与putchar()
随机推荐
Solution to TypeError The view function did not return a valid response. The function either returned None
npm run serve starts error npm ERR Missing script "serve"
强国杯初赛WP
mysql间隙锁失效?
cJSON开源项目详细解剖
最新版redis6.3.2下载安装
3分钟告诉你如何成为一名黑客|零基础到黑客入门指南,你只需要掌握这五点能力
CTF之misc-其他类型隐写
jsx的实现
Art-template 中文文档[详细篇]
攻防世界easy_web
3 minutes to tell you how to become a hacker | Zero foundation to hacker introductory guide, you only need to master these five skills
使用Nodejs搭建Web Server(入门教程)
初识C语言
P3 元宝序列化笔记
人生的第一篇博客(初识代码)
C语言:快速排序三种方法(递归)
信息安全必备神器之kali
自定义异常类的使用
uni-app installs components using npm commands