当前位置:网站首页>标准输入输出流(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就换行了。
边栏推荐
猜你喜欢
随机推荐
文件上传漏洞的绕过
别找了,你要的C语言“数组”在这里
node包的导入与导出
运维工程师面试经历
PHP-fpm
记一次流量分析实战——安恒科技(八月ctf)
cJSON开源项目详细解剖
mysql处理insert冲突的解决方案
C语言(1)
零基础C语言“函数”教程,有手就行
多进程实现并发服务器
CTF之misc-其他类型隐写
通信中间件 Fast DDS 基础概念简述与通信示例
MySQL 安装报错的解决方法
多线程之间的5中通信方式
Solution to TypeError The view function did not return a valid response. The function either returned None
FastAPI 快速入门
简述SSRF
js方法 reduce 用法
Communication middleware Fast DDS basic concepts and communication examples





![[Mini Program Project Development--Jingdong Mall] Classification Navigation Area of uni-app](/img/cb/b0b79444dc90980cd2220ff9e68549.png)


