IO stream-character output stream, byte array stream (7)

IO stream-character output stream, byte array stream (7)

  1. Character output stream

    In the java IO stream, a stream object PrintWirter for character output is specially provided. This object has an automatic refresh buffer character output stream, which is characterized by writing characters by line, and you can automatically wrap by using the println(); method. .

    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    
    public class LineNumberDemo3 {
          
          
        public static void main(String[] args) {
          
          
            BufferedReader br=null;
            PrintWriter pw=null;
            try {
          
          
                //让我们明白一个道理:输入流和输出流不一定是成对出现的,你输入流可以是处理流,输出流可以是节点流
                //输入流是就考虑输入流的过程,至于这个内容你要怎么输出到程序的外部,你自己来决定
                br=new BufferedReader(new InputStreamReader(new FileInputStream("d:/sxt.txt")));
                pw=new PrintWriter("d:/sxt4.txt");//PrintWriter直接是一个字符输出流,,且自带flush()刷新功能
                String temp="";
                int i=1;
                while ((temp=br.readLine())!=null){
          
          
                    pw.println(i+","+temp);
                    i++;
                }
    
            }catch (Exception e){
          
          
                e.printStackTrace();
            }finally {
          
          
                try {
          
          
                    if (br !=null){
          
          
                        br.close();
                    }
                    if (pw!=null){
          
          
                        pw.close();
                    }
    
                }catch (Exception e){
          
          
                    e.printStackTrace();
                }
            }
        }
    }
    
    
  2. Byte array stream

    ByteArrayInputStream and ByteArrayOutputStream are often used when converting between streams and arrays!

    • Byte array input stream

      To put it bluntly, FileInputStream treats files as data sources. ByteArrayInputStream regards the "byte array object" in memory as the data source.

      import java.io.ByteArrayInputStream;
      
      public class ByteArrayInputDemo {
              
              
          public static void main(String[] args) {
              
              
              byte[] arr="abcdefg".getBytes();
              ByteArrayInputStream bis=null;
              StringBuilder sb=new StringBuilder();
              try {
              
              
                  //该构造方法的参数是一个字节数组,这个字节数组就是数据源
                  bis=new ByteArrayInputStream(arr);
                  int temp=0;
                  while ((temp=bis.read())!=-1){
              
              
                      sb.append((char)temp);
                  }
                  System.out.println(sb.toString());
      
              }finally {
              
              
                  try {
              
              
      
                  }catch (Exception e){
              
              
                      e.printStackTrace();
                  }
              }
          }
      }
      
      
    • Byte array output stream

      The ByteArrayOutputStream stream object is to write the data in the stream to the byte array.

      import java.io.ByteArrayOutputStream;
      
      public class ByteArrayOutputDemo {
              
              
          public static void main(String[] args) {
              
              
              ByteArrayOutputStream bos=null;
              try {
              
              
                  StringBuilder sb=new StringBuilder();
                  bos=new ByteArrayOutputStream();
                  bos.write('a');
                  bos.write('b');
                  bos.write('c');
                  byte[] arr=bos.toByteArray();//toByteArray()就是把字节数组输出流转换成字节数组的一个方法
                  for (int i=0;i<arr.length;i++){
              
              
                      sb.append((char) arr[i]);
                  }
                  System.out.println(sb.toString());
              }finally {
              
              
                  try {
              
              
                      if (bos!=null){
              
              
                          bos.close();
                      }
      
                  }catch (Exception e){
              
              
                      e.printStackTrace();
                  }
              }
          }
      }
      

Guess you like

Origin blog.csdn.net/Xun_independent/article/details/114897419