zoukankan      html  css  js  c++  java
  • IO:字节流转化字符流

    InputStreamReader类可以将InputStream字节流转化成Reader字符流:

     1 void inputStreamReader() {
     2         // InputStreamReader类可以将InputStream字节流转化成Reader字符流
     3         // 中文无乱码
     4         File file = new File("e:\test.txt");
     5         InputStream inputStream = null;
     6         InputStreamReader isr = null;
     7         char[] ch = new char[24];
     8         int len;
     9         StringBuilder sb = new StringBuilder();
    10         try {
    11             inputStream = new FileInputStream(file);
    12             isr = new InputStreamReader(inputStream);
    13             while ((len = isr.read(ch)) != -1) {
    14                 sb.append(ch);
    15             }
    16             System.out.println(sb);
    17             // 获取文件编码格式
    18             System.out.println(isr.getEncoding());
    19         } catch (FileNotFoundException e) {
    20             // TODO Auto-generated catch block
    21             e.printStackTrace();
    22         } catch (IOException e) {
    23             // TODO Auto-generated catch block
    24             e.printStackTrace();
    25         } finally {
    26             if (isr != null) {
    27                 try {
    28                     isr.close();
    29                 } catch (IOException e) {
    30                     // TODO Auto-generated catch block
    31                     e.printStackTrace();
    32                 }
    33             }
    34         }
    35     }
  • 相关阅读:
    Add Binary <leetcode>
    那些坑
    面试集锦
    随看随记
    View的事件处理流程
    android studio view.setId报错
    EditText的hint不显示
    EditText 焦点
    Android拍照的那些事
    微信支付提示签名错误
  • 原文地址:https://www.cnblogs.com/mada0/p/4705079.html
Copyright © 2011-2022 走看看