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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© @coffee 中级黑马   /  2015-1-17 18:07  /  1298 人查看  /  10 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文



import java.io.*;
class MyBufferedInputStream
{
private InputStream in;
private int pos = 0,count = 0;
private byte[] buf = new byte[1024];
MyBufferedInputStream(InputStream in)
{
  this.in = in;
}
public int myRead() throws IOException
{
  if(count == 0)
  {
   count = in.read(buf);
   if(count < 0)
    return -1;
   pos = 0;
   byte b = buf[pos];
   count--;
   pos++;
   return b&255;
  }
  else if (count > 0)
  {
   byte b = buf[pos];
   count--;
   pos++;
   return b&255;
  }
  return -1;
}
public void myClose() throws IOException
{
  in.close();
}
}
class MyBufferedInputStreamDemo
{
public static void main(String[] args)
{
  long start = System.currentTimeMillis();
  MyBufferedInputStream bis = null;
  BufferedOutputStream bos = null;
  try
  {
   bis = new MyBufferedInputStream (new FileInputStream("mp3.exe"));//错点
   bos = new BufferedOutputStream (new FileOutputStream("mp32.exe"));
   byte[] buf = new byte[1024];
   int ch =0;
   while((ch = bis.myRead(buf))!=-1)
   {
    bos.write(buf,0,ch);
   }
  }
  catch (IOException e)
  {
   throw new RuntimeException("复制失败");
  }
  finally
  {
   try
   {
    if(bis != null)
     bis.myClose();
   }
   catch (IOException e)
   {
    throw new RuntimeException("读取失败");
   }
   try
   {
    if(bos != null)
     bos.close();
   }
   catch (IOException e)
   {
    throw new RuntimeException("读取失败");
   }
  }
  long end = System.currentTimeMillis();
  System.out.println((end-start)+"毫秒");
}
}

/*

E:\java\day19>javac MyBufferedInputStreamDemo.java
MyBufferedInputStreamDemo.java:74: 错误: 无法将类 MyBufferedInputStream中的方法
myRead应用到给定类型;
                        while((ch = bis.myRead(buf))!=-1)
                                       ^
  需要: 没有参数
  找到: byte[]
  原因: 实际参数列表和形式参数列表长度不同
1 个错误
*/

10 个回复

倒序浏览
MyBufferedInputStream中的方法myRead()是无参的方法 你往里传参数buf当然不行 你得写个带参数的方法
回复 使用道具 举报
Gonnaloveu 发表于 2015-1-17 19:06
MyBufferedInputStream中的方法myRead()是无参的方法 你往里传参数buf当然不行 你得写个带参数的方法 ...

你说的对! :handshake
回复 使用道具 举报
怎么可以这么长啊 ,以后是不是还要更长啊
回复 使用道具 举报
你错误的原因是不知道用代码粘贴模块
回复 使用道具 举报 1 0
myRead()是无参的方法
回复 使用道具 举报
:handshake
回复 使用道具 举报
好长...能用封装吗
回复 使用道具 举报
yukuoyuan 发表于 2015-1-17 21:23
怎么可以这么长啊 ,以后是不是还要更长啊

既然喜欢这行 ,就不要怕吃苦。
回复 使用道具 举报
wangcongwu 发表于 2015-1-17 21:32
你错误的原因是不知道用代码粘贴模块

想 先找找手感!!
回复 使用道具 举报
myRead()方法无参数,你传进一个buf...
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马