io流中read方法使用不当导致运行异常的一点

public class CopyMp3test {

public static void main(String[] args) throws IOException {
FileInputStream fis= new FileInputStream("d:\0.mp3");
FileOutputStream fos= new FileOutputStream("d:\2.mp3");
byte[] by= new byte[1024];
int len=0;
while((len=fis.read())!=-1)
{
fos.write(by, 0, len);
}
fis.close();
fos.close();
}

}

加下划线的地方容易出错,read方法使用没有指定byte数组,在fos输出流调用write方法时写的数据实际上是空的数组,由于len在此情况下是read方法返回的数据的字节数,也就是说一个循环会写一个1024的数据,一个MP3文件假如大小为4M,那会导致循环4*1024*1024次,就需要写入一个空的1024的数组的数据,在此估计为1kb,导致2.MP3为4G;