//文件读写,从一个地方读取文件,然后把读取的文件写入另一个文件中.
public void testBufferInputStream(){
long startTime = System.currentTimeMillis();
File inFile = new File("D://11.wmv"); // 读文件;
FileInputStream ips = null;
BufferedInputStream bis = null;
File outFile = new File("D://11Copy2.wmv");
FileOutputStream ops = null;
BufferedOutputStream bos = null;
if (!inFile.exists()) {
System.out.println(inFile.getName() + " is not exists!");
return;
}
try {
if (!outFile.exists()) {
outFile.createNewFile();
System.out.println("out File is Create!");
}
ips = new FileInputStream(inFile);
ops = new FileOutputStream(outFile);
/* //方法1:以byte读取
int len = 0;
while ((len = ips.read()) != -1) {
ops.write(len);
}*/
//方法2:以byte[]数组进行读取(速度比byte快得多,不过BufferedInputStream一样快)
int len = ips.available(); //获取文件的大小
System.out.println("the length len "+len);
byte[] data = new byte[len];
//使用缓冲
bis = new BufferedInputStream(ips);
bis.read(data);
bos = new BufferedOutputStream(ops);
bos.write(data);
//不使用BufferedInputStream缓冲
//ips.read(data);
//ops.write(data);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
//ips.close();
//ops.close();
bis.close();
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
long endTime = System.currentTimeMillis();
long spentTime = endTime - startTime;
System.out.println("spentTime="+spentTime);
}