一、标准输入、输出流
1.1概述
- System.in和System.out分别代表了系统标准的输入和输出设备。
- 默认输入设备时:键盘,输出设备是:显示器。
- System.in的类型是InputStream,字节输入流。
- System.out的类型是PrintStream,是OutputStream的子类FilterOutputStream的子类。
1.2重定向
-
System类的 setInt() 、setOut() 方式重新指定输入和输出的流。
-
setInt(InputStream is)
-
setOut(PrintStream os)
-
1.3例题
从键盘输入字符串,要求将读取到的整行字符串转成大写输出。然后继续进行输入操作,直至当输入“e”或者“exit”时,退出程序。
思路: System.in ---> 转换流 -->BufferedReader的readLine()
public static void main(String[] args) {
BufferedReader br = null;
try {
InputStreamReader isr = new InputStreamReader(System.in);
br = new BufferedReader(isr);
while (true) {
System.out.println("请输入字符串:");
String data = br.readLine();
if ("e".equalsIgnoreCase(data) || "exit".equalsIgnoreCase(data)) {
System.out.println("程序结束!");
break;
}
String upperCase = data.toUpperCase();
System.out.println(upperCase);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
二、打印流:PrintStrem、PrintWriter
2.1概述
- 实现将基本数据类型的数据格式转化为字符串输出。
- 提供了一系列重载的print()和println()。
三、数据流DataInputStream 和 DataOutputStream
3.1作用
- 为了方便操作Java语言的基本数据类型和String的数据,使用数据流。
- 将文件中存储的基本数据类型变量和字符串读取到内存中,保存在变量中。
- 读取不同类型的数据的顺序要与当初写入文件时,保存的数据顺序一致。