标准的输入输出流 了解
package com.atguigu.java;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 其他流的使用
* 1. 标准的输入、输出流
* 2. 打印流
* 3. 数据流
*
* @author liangqichen
* @create 2021-10-17 22:27
*/
public class OtherStream {
/*
1. 标准的输入、输出流
1.1
System.in 标准的输入流,默认从键盘输入
System.out 标准的输出路,默认从控制台输出
1.2
System类的setIn(InputStream) setOut(PrintStream) 方式重新指定输入和输出的流。
1.3练习
从键盘输入字符串,要求将读取到的整行字符串转成大写输出。然后继续讲进行输入操作,值至当输入 e 或者 exit时,退出程序
方法一 : 使用Scanner,调用了next方法,返回一个字符串
方法二 : 使用System.in实现 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 {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}}
package com.atguigu.exer;
// MyInput.java: Contain the methods for reading int, double, float, boolean, short, byte and
// string values from the keyboard
import java.io.*;
public class MyInput {
// Read a string from the keyboard
public static String readString() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// Declare and initialize the string
String string = "";
// Get the string from the keyboard
try {
string = br.readLine();
} catch (IOException ex) {
System.out.println(ex);
}
// Return the string obtained from the keyboard
return string;
}
// Read an int value from the keyboard
public static int readInt() {
return Integer.parseInt(readString());
}
// Read a double value from the keyboard
public static double readDouble() {
return Double.parseDouble(readString());
}
// Read a byte value from the keyboard
public static double readByte() {
return Byte.parseByte(readString());
}
// Read a short value from the keyboard
public static double readShort() {
return Short.parseShort(readString());
}
// Read a long value from the keyboard
public static double readLong() {
return Long.parseLong(readString());
}
// Read a float value from the keyboard
public static double readFloat() {
return Float.parseFloat(readString());
}
}