s-day09--文件复制的分析与实现
This commit is contained in:
@@ -0,0 +1,30 @@
|
|||||||
|
package com.inmind.outputstream01;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/*
|
||||||
|
需求:将工程中的a.jpg复制到b.jpg中
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class Test08 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
//复制:对一个文件一边读取字节,一边写出字节到新文件中
|
||||||
|
FileInputStream fis = new FileInputStream("a.jpg");
|
||||||
|
FileOutputStream fos = new FileOutputStream("b.jpg");
|
||||||
|
|
||||||
|
long start = System.currentTimeMillis();
|
||||||
|
//一边读一边写(一次读写一个字节)
|
||||||
|
//int c = fis.read();
|
||||||
|
int c;//定义一个变量来接收,我们读取的字节数据
|
||||||
|
while ((c = fis.read())!=-1){
|
||||||
|
fos.write(c);
|
||||||
|
}
|
||||||
|
long end = System.currentTimeMillis();
|
||||||
|
fos.close();
|
||||||
|
fis.close();
|
||||||
|
System.out.println("复制结束,消耗的时间:"+(end - start));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package com.inmind.outputstream01;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/*
|
||||||
|
需求:将工程中的a.jpg复制到b.jpg中
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class Test09 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
//复制:对一个文件一边读取字节,一边写出字节到新文件中
|
||||||
|
FileInputStream fis = new FileInputStream("a.jpg");
|
||||||
|
FileOutputStream fos = new FileOutputStream("b.jpg");
|
||||||
|
|
||||||
|
long start = System.currentTimeMillis();
|
||||||
|
//一边读一边写(一次读写一个字节数组)
|
||||||
|
byte[] bytes = new byte[1024];
|
||||||
|
int len;
|
||||||
|
//int len = fis.read(bytes);
|
||||||
|
while ((len = fis.read(bytes) )!= -1){
|
||||||
|
fos.write(bytes,0,len);
|
||||||
|
}
|
||||||
|
long end = System.currentTimeMillis();
|
||||||
|
fos.close();
|
||||||
|
fis.close();
|
||||||
|
System.out.println("复制结束,消耗的时间:"+(end - start));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user