zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Java 基础 (IO标准的输入,输出流,打印流,数据流)

2023-09-27 14:26:01 时间

标准的输入,输出流

* System.in 和 System.out 分别代表了系统标准的输入和输出设备
* 默认输入设备是: 键盘,输出设备是: 显示器
* System.in 的类型是 InputStream
* System.out 的类型是 PrintStream,其是 OutputStream 的子类 
  FilterOutputStream 的子类
* 重定向: 通过 System 类的 setIn,setOut 方法对默认设备进行改变。
 > public static void setIn(InputStream in)
 > public static void setOut(PrintStream out)

otherSreamTest.java

package com.klvchen.java;

import org.junit.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class otherSreamTest {

    public static void main(String[] args){
       InputStreamReader isr = null;
       BufferedReader br = null;
       try {
           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();
               }
           }
       }
   }
}

打印流

实现将基本数据类型的数据格式转化为字符串输出
打印流: PrintStream 和 PrintWriter
> 提供了一系列重载的 print() 和 println() 方法,用于多种数据类型的输出 
> PrintStream 和 PrintWriter 的输出不会抛出 IOException异常
> PrintStream 和 PrintWriter 有自动flush功能
> PrintStream 打印的所有字符都使用平台的默认字符编码转换为字节。在需要写入字符而不是写入字节的情况下,应该使用 PrintWriter类。
> System.out 返回的是 PrintStream 的实例

otherSreamTest.java

package com.klvchen.java;

import org.junit.Test;

import java.io.*;

public class otherSreamTest {
 @Test
    public void test2(){
       PrintStream ps = null;
       try {
           FileOutputStream fos = new FileOutputStream(new File("G:\\tmp\\text.txt"));
           //创建打印输出流,设置为自动刷新模式(吸入换行字符或字节'\n'时会刷新输出缓冲区)
           ps = new PrintStream(fos, true);
           if (ps != null) { //把标准输出流(控制台输出)改成文件
               System.setOut(ps);
           }
           for (int i = 0; i < 155; 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();
           }

       }
   }

}

数据流

* 为了方便地操作 Java 语言的基本数据类型和 String 的数据,可以使用数据流。
* 数据流有两个类:(用于读取和写出基本数据类型、String类的数据)
  > DataInputStream 和 DataOutputStream
  > 分别 “套接” 在 InputStream 和 OutputStream 子类的流上
* DatalnputStream中的方法
  boolean readBoolean()    byte readByte()
  char readChar()          float readFloat()
  double readDouble()      short readShort()
  long readLong()          int readInt()
  String readUTF()         void readFully(byte[] b)
* DataoutputStream 中的方法
  > 将上述的方法的 read 改为相应的 write 即可。

otherSreamTest.java

package com.klvchen.java;

import org.junit.Test;

import java.io.*;

public class otherSreamTest {
   @Test
    public void test3() throws IOException {
       DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));

       dos.writeUTF("陈陈陈");
       dos.flush();
       dos.writeInt(23);
       dos.flush();
       dos.writeBoolean(true);
       dos.flush();

       dos.close();

   }

   @Test
    public void test4() throws IOException {
       DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));

       String name = dis.readUTF();
       int age = dis.readInt();
       boolean isMale = dis.readBoolean();

       System.out.println("name = " + name);
       System.out.println("age = " + age);
       System.out.println("isMale = " + isMale);

       dis.close();

   }


}