1. 程式人生 > >JAVA之用緩沖流讀寫文件

JAVA之用緩沖流讀寫文件

java stream span () 字節 pan str edi pri

public class CopyDemo {
public static void main(String[] args) throws Exception{
long time1 = System.currentTimeMillis();
copy4(new File("d:\\ccc.mp4"),new File("e:\\ccc.mp4"));
long time2 = System.currentTimeMillis();
System.out.println(time2-time1);
}
//1.字節流讀寫單個字節 太慢了

public static void copy1(File src,File desc) throws Exception{
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(desc);
int len = 0;
while ((len=fis.read())!=-1){
fos.write(len);
}
fos.close();
fis.close();

}
//2.字節流讀寫字節數組 550
public static void copy2(File src,File desc) throws Exception{
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(desc);
int len = 0;
byte[] b = new byte[1024*10];
while ((len=fis.read(b))!=-1){
fos.write(b,0,len);

}
fos.close();
fis.close();
}
//3.字節流緩沖區 讀寫單個字節 4252
public static void copy3(File src,File desc) throws Exception{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc));
int len = 0;
while ((len=bis.read())!=-1){
bos.write(len);
}
bis.close();
bos.close();
}
//4.字節流緩沖區讀寫字節數組 585
public static void copy4(File src,File desc) throws Exception{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc));
int len = 0;
byte[] b = new byte[1024*10];
while ((len=bis.read(b))!=-1){
bos.write(b,0,len);
}
bos.close();
bis.close();
}

JAVA之用緩沖流讀寫文件