标准输入、输出流
- System.in和System.out分别代表了系统标准的输入和输出设备
- 默认输入设备是:键盘,输出设备是:显示器
- System的两个全局常量
System.in的类型是InputStream(字节流基类)System.out的类型是PrintStream,其是OutputStream的子类FilterOutputStream 的子类
- 重定向:通过System类的setIn,setOut方法对默认设备进行改变。
public static void setIn(InputStream in)public static void setOut(PrintStream out)
/*从键盘输入字符串,要求将读取到的整行字符串转成大写输出。然后继续进行输入操作,直至当输入“e”或者“exit”时,退出程序。*/public static void main(String[] args) {BufferedReader br = null;try {//System.in输入的为字节流,需要通过转换流转换为字符流InputStreamReader isr = new InputStreamReader(System.in);//通过BufferedReader的readLine()可以读取整行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();}}}}
打印流
实现将基本数据类型的数据格式转化为字符串输出
打印流:PrintStream和PrintWriter
- 提供了一系列重载的print()和println()方法,用于多种数据类型的输出
- PrintStream和PrintWriter的输出不会抛出IOException异常
- PrintStream和PrintWriter有自动flush功能
- PrintStream 打印的所有字符都使用平台的默认字符编码转换为字节。 在需要写入字符而不是写入字节的情况下,应该使用 PrintWriter 类。
- System.out返回的是PrintStream的实例
```java
public void test() {
PrintStream ps = null;try {FileOutputStream fos = new FileOutputStream(new File("D:\\IO\\text.txt"));// 创建打印输出流,设置为自动刷新模式(写入换行符或字节 '\n' 时都会刷新输出缓冲区)ps = new PrintStream(fos, true);if (ps != null) {// 把标准输出流(控制台输出)重定向输出到文件System.setOut(ps);}
for (int i = 0; i <= 255; i++) { // 输出ASCII字符System.out.print((char) i);if (i % 50 == 0) { // 每50个数据一行System.out.println(); // 换行}}} catch (FileNotFoundException e) {e.printStackTrace();} finally {if (ps != null) {ps.close();}}}
---<a name="e0vkz"></a># 数据流> 为了方便地操作Java语言的基本数据类型和String的数据,可以使用数据流。<a name="ulqk2"></a>### 数据流的两个类`DataInputStream`:(用于读取基本数据类型、String类的数据)<br />`DataOutputStream`::(用于写出基本数据类型、String类的数据)<br />分别“套接”在 InputStream 和 OutputStream 子类的流上<a name="W2JLY"></a>### DataInputStream中的方法`boolean readBoolean() `<br />`byte readByte()`<br />`char readChar()`<br />`float readFloat()`<br />`double readDouble()`<br />`short readShort()`<br />`long readLong() `<br />`int readInt() `<br />`String readUTF() `<br />`void readFully(byte[] b) `<a name="SHN4E"></a>### DataOutputStream中的方法将上述的方法的read改为相应的write即可。```javapublic void test3() throws IOException {DataOutputStream dos = null;try { // 创建连接到指定文件的数据输出流对象dos = new DataOutputStream(new FileOutputStream("destData.dat"));dos.writeUTF("我爱北京天安门"); // 写UTF字符串dos.flush();//刷新操作,将内存中的数据写入文件dos.writeBoolean(false); // 写入布尔值dos.writeLong(1234567890L); // 写入长整数System.out.println("写文件成功!");} catch (IOException e) {e.printStackTrace();} finally { // 关闭流对象try {if (dos != null) {// 关闭过滤流时,会自动关闭它包装的底层节点流dos.close();}} catch (IOException e) {e.printStackTrace();}}}
/*将文件中存储的基本数据类型变量和字符串读取到内存中,保存在变量中。注意点:读取不同类型的数据的顺序要与当初写入文件时,保存的数据的顺序一致!*/public void test4() {DataInputStream dis = null;try {dis = new DataInputStream(new FileInputStream("destData.dat"));String info = dis.readUTF();boolean flag = dis.readBoolean();long time = dis.readLong();System.out.println(info);System.out.println(flag);System.out.println(time);} catch (Exception e) {e.printStackTrace();} finally {if (dis != null) {try {dis.close();} catch (IOException e) {e.printStackTrace();}}}}
