首页 > 其他 > 详细

IO:字节流转化字符流

时间:2015-08-05 17:41:26      阅读:226      评论:0      收藏:0      [点我收藏+]

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     }

 

IO:字节流转化字符流

原文:http://www.cnblogs.com/mada0/p/4705079.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!