黑马程序员技术交流社区

标题: 求高手给我解决一个IO问题 [打印本页]

作者: 李杰    时间: 2012-8-27 11:05
标题: 求高手给我解决一个IO问题
本帖最后由 李杰 于 2012-8-27 11:08 编辑

{:soso_e101:}用FileReader读出的数据,通过转换流变为FileOutputStream输出去不行吗?
  1. public class PictureCopy
  2. {        
  3.         public static void main(String[] args)
  4.         {        
  5.                 <font color="Red">FileReader fi=null;
  6.                 FileOutputStream fo=null;</font>
  7.                 try
  8.                 {
  9.                         fi=new FileReader("I:\\11.txt");
  10.                         fo=new FileOutputStream("I:\\2.txt");
  11.                         char [] by=new char[1024];
  12.                         int len;
  13.                        OutputStreamWriter o=new OutputStreamWriter(fo);     //这样不行吗?,最后输出文件结果什么也没有  为什么
  14.                         while((len=fi.read(by))!=-1)
  15.                         {
  16.                                 o.write(by, 0, len);
  17.                         }
  18.                 } </font>
  19.                 catch (FileNotFoundException e)
  20.                 {
  21.                         // TODO Auto-generated catch block
  22.                         e.printStackTrace();
  23.                 } catch (IOException e) {
  24.                         // TODO Auto-generated catch block
  25.                         e.printStackTrace();
  26.                 }
  27.                 finally
  28.                 {
  29.                         if(fi!=null)
  30.                                 try {
  31.                                         fi.close();
  32.                                 } catch (IOException e) {
  33.                                         // TODO Auto-generated catch block
  34.                                         e.printStackTrace();
  35.                                 }
  36.                         if (fo!=null)
  37.                                 try {
  38.                                         fo.close();
  39.                                 } catch (IOException e) {
  40.                                         // TODO Auto-generated catch block
  41.                                         e.printStackTrace();
  42.                                 }
  43.                                 
  44.                         
  45.                 }
  46.                
  47.          
  48.         }

  49. }
复制代码

作者: 刘芮铭    时间: 2012-8-27 11:32
while((len=fi.read(by))!=-1)

                        {

                                o.write(by, 0, len);
                               o.flush(); //大侠,你这里没有刷新数据啊,数据还全部在流里面,没有刷进目标!
                        }

作者: 周兴华    时间: 2012-8-27 11:47
while((len=fi.read(by))!=-1)
{
o.write(by, 0, len);
o.flush;  //添加这一行就OK了
  }

作者: 李杰    时间: 2012-8-27 12:36
刘芮铭 发表于 2012-8-27 11:32
while((len=fi.read(by))!=-1)

                        {

谢了大哥
作者: 牛杨    时间: 2012-8-27 13:35
本帖最后由 牛杨 于 2012-8-27 13:45 编辑

用FileReader读出的数据,通过转换流变为FileOutputStream输出去不行吗?
一般情况下,用字符输入流读取数据,最好还用字符输出流写入数据
但是如果楼主非要使用字节输出流写入的话:
步骤
1、可以先把字符输入流FileReader读取到的字符数据(比如 存到byte数组by里面)。
2、然后把字符数组by构造成字符串
3、然后调用其getBytes()方法转变成字节数组
4、最后就可以使用字节输出流写入了。
示例: 使用楼主的代码演示:
   ………………
while((len=fi.read(by))!=-1)
{
     String str=new String(by,0,len); //先把字符数组构造成字符串str。
     fo.write ( str.getBytes() );//把str转换成字符字节数组写入字节输出流中。
}
   ………………
希望对楼主有用!




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2