JavaBase-IO流-标准输入,输出流、打印流、数据流

标准输入,输出流

  • System.in和System.out分别代表了系统标准的输入和输出设备

  • 默认输入设备是:键盘,输出设备是显示器

  • System.in的类型是InputStream

  • System.out的类型是PrintStream,PrintStream是OutputStream的子类,FilterOutputStream的子类

  • 重定向:通过System类的setIn,setOut方法对默认设备进行改变。

    • public static void setIn(InputStream in)
    • public static void setOut(PrintStream out)
  • 使用练习

    • /**
       * 1.标准的输入流
       *  System.in:标准的输入流,默认从键盘输入
       *  System.out:标准的输出流,默认从控制台输出
       *
       *  从键盘输入字符串,要求将读取到的整行字符串转成大写输出。然后继续进行输入操作,直至当
       *  输入“e”或者“exit”时,退出程序。
       *  方法一 使用Scanner实现,调用next()返回一个字符串
       *  方法二 使用System.in实现
       */
      public static void main(String[] args) {
          BufferedReader br = null;
          BufferedWriter bw = null;
      
          try {
              br = new BufferedReader(new InputStreamReader(System.in));
              bw = new BufferedWriter(new OutputStreamWriter(System.out));
              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 {
          }
      }
      

打印流

  • 实现将基本数据类型的数据格式转化为字符串输出

  • 打印流:PrintStream和PrintWriter

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

    • @Test
      public void test1(){
          PrintStream ps = null;
          try {
              ps = new PrintStream(new FileOutputStream(new File("my.txt")),true);
              System.setOut(ps);
              for(int i = 0;i<=255;i++){
                  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子类的流上
  • DataInputStream中的方法

    • boolean readBoolean()
    • byte readByte()
    • char readChar()
    • float readFloat()
    • double readDouble()
    • short readShort()
    • long readLong()
    • int readInt()
    • String readUTF()
    • void readFully(byte[] b)
  • DataOutputStream中的方法

    • void writeBoolean(boolean b)
    • void writeByte(int v)
    • void writeChar(int v)
    • void writeFloat(float v)
    • void writeDouble(double v)
    • void writeShort(int v)
    • void writeLong(long v)
    • void writeInt(int v)
    • void wirteUTF(String v)
  • DataOutputStream和DataInputStream的使用

    • @Test
      public void test3(){
          DataOutputStream dos = null;
          try {
              dos = new DataOutputStream(new FileOutputStream("dos.txt"));
              dos.writeBoolean(true);
              dos.flush();
              dos.writeUTF("打发斯蒂芬");
              dos.flush();
              dos.writeInt(1);
              dos.flush();
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          } finally {
              if(dos != null){
                  try {
                      dos.close();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          }
      }
      
      @Test
      public void test4(){
          DataInputStream dis = null;
          try {
              dis = new DataInputStream(new FileInputStream("dos.txt"));
              System.out.println(dis.readBoolean());
              System.out.println(dis.readUTF());
              System.out.println(dis.readInt());
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          } finally {
              if(dis != null){
                  try {
                      dis.close();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          }
      }
      

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值