java通过IO流复制文件

2018-07-06 01:24:44来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

package kimoji;

import java.io.*;

public class FileTest {

public static void main(String[] args) throws IOException {
out1("D:\\ppt\\Oracle_SQL基礎知識.ppt", "aaa.ppt");
out2("D:\\ppt\\Oracle_SQL基礎知識.ppt", "aaa1.ppt");
out3("D:\\ppt\\Oracle_SQL基礎知識.ppt", "aaa11.ppt");
out4("D:\\ppt\\Oracle_SQL基礎知識.ppt", "aaa111.ppt");

 


/*if (file.exists() && file.isDirectory()) {
String names[] = file.list();
for (String name : names) {
System.out.println(name);
}
System.out.println("000000000000000000000000000000000000000000000000000000000000000000000000");
String[] nameNei = file.list(new FilenameFilter() {

@Override
public boolean accept(File dir, String name) {
return name.endsWith(".java");
}
});
for (String string : nameNei) {
System.out.println(string);
}
}*/

 


}

/**
* 一次读取一个字节
* @param src1 :读取路径
* @param src2 :写入路径
* @throws IOException
*/
public static void out1(String src1,String src2) throws IOException{
FileInputStream fileInputStream = new FileInputStream(src1);
FileOutputStream fOutputStream = new FileOutputStream(src2);
int len = -1;
Long star = System.currentTimeMillis();
while((len = fileInputStream.read())!=-1){
fOutputStream.write(len);
}
long end = System.currentTimeMillis();
System.out.println("out1耗时为:"+(end-star)+"毫秒");
fileInputStream.close();
fOutputStream.close();
}

/**
* 一次读取一个字节数组
* @param str1:读取路径
* @param str2:写入路径
* @throws IOException
*/
public static void out2(String str1,String str2) throws IOException{
FileInputStream fis = new FileInputStream(str1);
FileOutputStream fos = new FileOutputStream(str2);
int len = -1;
byte temp [] = new byte[1024];
Long star = System.currentTimeMillis();
while((len = fis.read())!=-1){
fos.write(temp, 0, len);
}
long end = System.currentTimeMillis();
System.out.println("out2耗时为:"+(end-star)+"毫秒");
fis.close();
fos.close();
}
/**
* 带有缓冲区的一次读取一个字节
* @param src1 :读取路径
* @param src2 :写入路径
* @throws IOException
*/
public static void out3(String str1,String str2) throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(str1));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(str2));
int len = -1;
Long star = System.currentTimeMillis();
while((len=bis.read())!=-1){
bos.write(len);
}
Long end = System.currentTimeMillis();
System.out.println("out3共耗时"+(end-star)+"毫秒");
bis.close();
bos.close();
}
public static void out4(String str1,String str2) throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(str1));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(str2));
int len = -1;
byte [] temp = new byte[1024];
Long star = System.currentTimeMillis();
while((len = bis.read())!=-1){
bos.write(temp, 0, len);
}
Long end = System.currentTimeMillis();
System.out.println("out4共耗时"+(end-star)+"毫秒");
bis.close();
bos.close();
}
}

 

 

 用韵结果如下:

out1耗时为:6431毫秒
out2耗时为:6223毫秒
out3共耗时28毫秒
out4共耗时197毫秒

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:java.util.LinkedHashMap cannot be cast to xxx

下一篇:Java基础——对象和类