在毕老师的视频中讲到字节流为了提高效率可以自定义字节流缓冲区
我把四种缓冲区拷贝文件的方法测试发现自定义的方法运行时间最长,
效率最低,这是为什么啊?
/*
拷贝mp3
*/
第一种:
//通过字节流缓冲区直接读取
import java.io.*;
class CopyMp3Demo3
{
public static void main(String[] args) throws IOException
{
long start=System.currentTimeMillis();
copy();
long end=System.currentTimeMillis();
System.out.println(end-start+"毫秒"); }
//通过字节流的缓冲区完成复制。
public static void copy() throws IOException
{
FileInputStream fis=new FileInputStream("d:\\abc");
FileOutputStream fos=new FileOutputStream("d:\\fgh");
BufferedInputStream bufis=new BufferedInputStream(fis);
BufferedOutputStream bufos=new BufferedOutputStream(fos);
int by=0;
while((by=bufis.read())!=-1)
{
bufos.write(by);
System.out.print((char)by);
}
bufos.close();
bufis.close();
}
}
*/
第二种:
/*
//通过字节流缓冲区读取数组
import java.io.*;
//import java.lang.*;
class CopyMp3Demo3
{
public static void main(String[] args)throws IOException
{
long start=System.currentTimeMillis();
copy();
long end=System.currentTimeMillis();
System.out.println(end-start+"毫秒");
}
public static void copy() throws IOException
{
BufferedInputStream bufis=new BufferedInputStream(new FileInputStream("E:\\1.mp3"));
BufferedOutputStream bufos=new BufferedOutputStream(new FileOutputStream("E:\\2.mp3"));
byte[] buf=new byte[1024];
int len=0;
while((len=bufis.read(buf))!=-1)
{
bufos.write(buf,0,len);
}
bufis.close();
bufos.close();
}
}
*/
第三种:
//通过字节流缓冲区读取数组,包括处理异常
import java.io.*;
class CopyMp3Demo3
{
public static void main(String[] args) throws IOException
{
long start=System.currentTimeMillis();
copy();
long end=System.currentTimeMillis();
System.out.println(end-start+"毫秒");
}
public static void copy() throws IOException
{
BufferedInputStream bufis=null;
BufferedOutputStream bufos=null;
try
{
bufis=new BufferedInputStream(new FileInputStream("E:\\1.mp3"));
bufos=new BufferedOutputStream(new FileOutputStream("E:\\2.mp3"));
byte[] buf=new byte[1024];
int len=0;
while((len=bufis.read(buf))!=-1)
{
bufos.write(buf,0,len);
}
}
catch (IOException e)
{
throw new RuntimeException("读写失败");
}
finally
{
try
{
if(bufis!=null)
bufis.close();
}
catch (IOException e)
{
}
try
{
if(bufos!=null)
bufos.close();
}
catch (IOException e)
{
}
}
}
}
第四种:
//自定义缓冲区拷贝Mp3
import java.io.*;
class MyBufferedInputStream
{
private InputStream in;
private byte[] buf=new byte[1024];
private int pos=0,count=0;
MyBufferedInputStream(InputStream in)
{
this.in=in;
}
//一次读取一个字节,从缓冲区(字节数组)获取
public int myRead() throws IOException
{
//通过in对象读取硬盘数据,并存储在buf中
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 CopyMp3Demo3
{
public static void main(String[] args) throws IOException
{
long start=System.currentTimeMillis();
copy();
long end=System.currentTimeMillis();
System.out.println(end-start+"毫秒");
}
//通过字节流的缓冲区完成复制。
public static void copy() throws IOException
{
MyBufferedInputStream bufis=new MyBufferedInputStream(new FileInputStream("E:\\1.mp3"));
BufferedOutputStream bufos=new BufferedOutputStream(new FileOutputStream("E:\\2.mp3"));
int by=0;
while((by=bufis.myRead())!=-1)
{
bufos.write(by);
}
bufos.close();
bufis.myClose();
}
}
*/
并且自定义缓冲区的原理不也是读取数组吗?和直接读取数组有什么区别?
|
|