首页 > 技术文章 > 46 转换流

flypigggg 2021-04-10 21:10 原文

转换流

处理流之二:转换流的使用

转换流:属于字符流

InputStreamReader:将一个字节的输入流转换为字符的输入流

OutputStreamWriter:将一个字符的输出流转换为字节的输出流

作用:提供字节流和字符流之间的转换

 

编码: 字符数组,字符 -------> 字节,字节数组

解码:字节,字节数组--------->字符数组,字符

@Test
   public void  test1() throws IOException {

       FileInputStream fis = new FileInputStream("hello.txt");

       //InputStreamReader isr = new InputStreamReader(fis);//使用系统默认的字符集
       //参数2指明了字符集,具体使用哪个字符集,取决于文件保存时使用的字符集
       InputStreamReader isr = new InputStreamReader(fis,"UTF-8");

       char[] cbuf = new char[10];
       int len;
       while ((len = isr.read(cbuf)) != -1){
           String str = new String(cbuf,0,len)
           System.out.println(str);
      }

       isr.close();
  }

 

综合使用

    @Test
   public void test2(){
       InputStreamReader isr = null;
       OutputStreamWriter osw = null;
       try {
           //1.造文件,造流
           FileInputStream fis = new FileInputStream("hello.txt");
           FileOutputStream fos = new FileOutputStream("hello2.txt");

           isr = new InputStreamReader(fis,"utf-8");
           osw = new OutputStreamWriter(fos,"gbk");

           //2.读写过程
           char[] cbuf = new char[10];
           int len;
           while((len = isr.read(cbuf)) != -1){
               osw.write(cbuf);
          }
      } catch (IOException e) {
           e.printStackTrace();
      } finally {
           if(osw != null){
               //3.资源关闭
               try {
                   osw.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }if(isr != null){
               try {
                   isr.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }
      }

  }

 

标准输入、输出流

Sysyetm.in:标准的输入流,默认从键盘输入

System.out:标准的输出流,默认从控制台输出

 

System类的setIn(InputStream)/ setOut(PrintStream)方式重新制定输入输出的流

 

从键盘输入字符串,要求将读取到的整行字符串转换成大写输出,然后继续进行输入操作

直至输入”e“ 或 exit时,推出

 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 {
           try {
               br.close();
          } catch (IOException e) {
               e.printStackTrace();
          }
      }

  }

 

 

打印流

实现将基本数据类型的数据格式转化为字符串输出 打印流:PrintStreamPrintWriter 提供了一系列重载的print()和println()方法,用于多种数据类型的输出

PrintStreamPrintWriter的输出不会抛出IOException异常 PrintStreamPrintWriter有自动flush功能 PrintStream打印的所有字符都使用平台的默认字符编码转换为字节。在需要写入字符而不是写入字节的情况下,应该使用PrintWriter类。

System.out返回的是PrintStream的实例

 

 

数据流

DataInputStreamDataOutputStream

作用:用于读取或写出基本数据类型的变量

将内存中的字符串,基本数据类型的变量写出到文件中

    @Test
   public void test1() {

       DataOutputStream dos = null;
       try {
           dos = new DataOutputStream(new FileOutputStream("daata.txt"));

           dos.writeUTF("芜湖");
           dos.writeInt(23);
           dos.writeBoolean(true);
      } catch (IOException e) {
           e.printStackTrace();
      } finally {
           if (dos != null) {
               try {
                   dos.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }
      }

  }

 

将文件中存储的基本数据类型变量和字符串读取到内存中,保存到变量中

    @Test
   public void test2(){

       DataInputStream dis = null;
       String name = null;
       int age = 0;
       boolean isMale = false;
       try {
           dis = new DataInputStream(new FileInputStream("daata.txt"));

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

           System.out.println(name);
           System.out.println(age);
           System.out.println(isMale);
      } catch (IOException e) {
           e.printStackTrace();
      } finally {
           if (dis != null) {
               try {
                   dis.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }
      }

  }

 

 

推荐阅读