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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© STRUGGLE 中级黑马   /  2014-1-7 11:43  /  1217 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 STRUGGLE 于 2014-1-8 20:28 编辑

看了毕老师的学习视频,请教各位大神代码中红框的遍历能否用缓存数组来做呢?

PS: byte[] buffer = new byte[1024] ;  如果可以该怎样遍历呢,可以提高效率!。。。

QQ截图20140107113634.jpg (144.83 KB, 下载次数: 7)

QQ截图20140107113634.jpg

评分

参与人数 1技术分 +1 收起 理由
FFF + 1 神马都是浮云

查看全部评分

6 个回复

正序浏览
周志彬 发表于 2014-1-7 11:52
因为你已经使用了流技术  所以我也不知道有没有提高效率的方法了
byte[] buffer = new byte[1024] ; 是可以 ...

对对,就是这种遍历方式。谢谢!
回复 使用道具 举报
利用缓冲技术可以提高流的效率 BufferedReader BufferedWriter
回复 使用道具 举报 1 0
import java.io.*;
class Demo
{
        public static void main(String[] args)
        {
                BufferedReader bufr = null;

                BufferedWriter bufw = null ;

                try
                {
                        bufr = new BufferedReader(new FileReader("d:\\itheima.txt"));
               
                        bufw = new BufferedWriter(new FileWriter("e:\\itheima.txt"));

                        int[] buf = new int[1024];

                        for (int ch = 0;(ch=bufr.read())!=-1; )
                        {
                                bufw.write(ch);
                        }
                }
                catch (IOException e)
                {
                        throw new RuntimeException("读写流创建失败");
                }
                finally
                {
                        try
                        {
                                if(bufr!=null)
                                        bufr.close();
                        }
                        catch (IOException e)
                        {
                                throw new RuntimeException("读取关闭失败");
                        }
                        try
                        {
                                if(bufw!=null)
                                        bufw.close();
                        }
                        catch (IOException e)
                        {
                                throw new RuntimeException("写入关闭失败");
                        }
                }
        }
}
回复 使用道具 举报
import java.io.*; class Demo  {         public static void main(String[] args)          {                 BufferedReader bufr = null;                  BufferedWriter bufw = null ;                  try                 {                         bufr = new BufferedReader(new FileReader("d:\\itheima.txt"));                                          bufw = new BufferedWriter(new FileWriter("e:\\itheima.txt"));                           int[] buf = new int[1024];                          for (int ch = 0;(ch=bufr.read())!=-1; )                         {                                 bufw.write(ch);                         }                 }                 catch (IOException e)                 {                         throw new RuntimeException("读写流创建失败");                 }                 finally                 {                         try                         {                                 if(bufr!=null)                                         bufr.close();                         }                         catch (IOException e)                         {                                 throw new RuntimeException("读取关闭失败");                         }                         try                         {                                 if(bufw!=null)                                         bufw.close();                         }                         catch (IOException e)                         {                                 throw new RuntimeException("写入关闭失败");                         }                 }         } }
回复 使用道具 举报
呵呵 可以用for循环提高效率 如果文件不大的话 建议用available()  
回复 使用道具 举报
因为你已经使用了流技术  所以我也不知道有没有提高效率的方法了
byte[] buffer = new byte[1024] ; 是可以的
while((buffer=br.readline())!=null)
{
       bw.write(buffer,0,buffer.length);
}

这样就可以了  

评分

参与人数 1技术分 +1 收起 理由
FFF + 1 赞一个!

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马