A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 臧盼 中级黑马   /  2012-12-25 18:46  /  1211 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

从字节字符的输入输出的角度看,数据是如何读写的,比如说dos=new DataoutputStream(new FileOutputStream(tempfile));
for(int i=0;i<10;i++)
dos.Writeint((int)(Math.random()*1000);在这个过程中本人认为最后写入文件的是以byte 为单位写入,那么DataoutputStream流是以什么单位写入?
并且最后的以bytes为单位写入似乎限制了前面的DateoutputStream流的写入速度,那么这种方式跟直接以FileoutPutStream写入的流有何区别?最后都是以byte为单位写入。都是一个二进制文件,我想起了是格式编码问题?不懂,请高手指教!

3 个回复

倒序浏览
我也不懂。。。。。。
回复 使用道具 举报
文件系统默认是GBK格式如:
  1. @Test
  2.         public void test3() {
  3.                 // InputStream is = System.in;
  4.                 InputStreamReader isr = null;
  5.                 OutputStreamWriter osw = null;
  6.                 try {
  7.                         isr = new InputStreamReader(new FileInputStream("d:/hello.java"),
  8.                                         "utf-8");
  9.                         osw = new OutputStreamWriter(new FileOutputStream(
  10.                                         "d:/hello_write.java"), "utf-8");
  11.                         int lenght = 0;
  12.                         char[] ch = new char[32];
  13.                         while ((lenght = isr.read(ch)) != -1) {
  14.                                 System.out.print(new String(ch, 0, lenght));
  15.                                 osw.write(ch, 0, lenght);
  16.                         }
  17.                 } catch (FileNotFoundException e) {
  18.                         // TODO Auto-generated catch block
  19.                         e.printStackTrace();
  20.                 } catch (IOException e) {
  21.                         // TODO Auto-generated catch block
  22.                         e.printStackTrace();
  23.                 } finally {
  24.                         if (isr != null) {
  25.                                 try {
  26.                                         isr.close();
  27.                                 } catch (IOException e) {
  28.                                         // TODO Auto-generated catch block
  29.                                         e.printStackTrace();
  30.                                 }
  31.                         }
  32.                         if (osw != null) {
  33.                                 try {
  34.                                         osw.close();
  35.                                 } catch (IOException e) {
  36.                                         // TODO Auto-generated catch block
  37.                                         e.printStackTrace();
  38.                                 }
  39.                         }
  40.                 }
  41.         }
复制代码
回复 使用道具 举报
设定好格式UTF-8输出也是UTF-8
这样就不会文件大小缺少一些情况
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马