码迷,mamicode.com
首页 > 其他好文 > 详细

IO:字节流转化字符流

时间:2015-08-05 17:43:39      阅读:85      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!