java 复制文件
来源:互联网 发布:淘宝情侣装专卖店 编辑:程序博客网 时间:2024/06/05 10:55
package sss;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
public class C1 {
/**
* 用文件通道的方式来进行文件复制
*
* @param s
* @param t
*/
public void fileChannelCopy(File s, File t) {
FileInputStream fi = null;
FileOutputStream fo = null;
FileChannel in = null;
FileChannel out = null;
try {
fi = new FileInputStream(s);
fo = new FileOutputStream(t);
in = fi.getChannel();// 得到对应的文件通道
out = fo.getChannel();// 得到对应的文件通道
in.transferTo(0, in.size(), out);// 连接两个通道,并且从in通道读取,然后写入out通道
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fi.close();
in.close();
fo.close();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 流复制
*
* @param s
* @param t
*/
public void copy(File s, File t) {
InputStream fis = null;
OutputStream fos = null;
try {
fis = new BufferedInputStream(new FileInputStream(s));
fos = new BufferedOutputStream(new FileOutputStream(t));
byte[] buf = new byte[2048];
int i;
while ((i = fis.read(buf)) != -1) {
fos.write(buf, 0, i);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fis.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
File s = new File("D:\\ORACLE执行计划和SQL调优教程.rar");
File t = new File("E:\\work\\111.rar");
File t1 = new File("E:\\work\\222.rar");
long start, end;
C1 c = new C1();
start = System.currentTimeMillis();
c.fileChannelCopy(s, t);
end = System.currentTimeMillis();
System.out.println("FileChannel复制,用时" + (end - start) + "ms");
start = System.currentTimeMillis();
c.copy(s, t1);
end = System.currentTimeMillis();
System.out.println("buffered复制,用时" + (end - start) + "ms");
}
}
- JAVA文件复制
- JAVA文件复制
- Java复制文件
- JAVA多线程复制文件
- Java实现文件复制
- Java实现文件复制
- 文件复制--FileStreamDemo.java
- 文件复制--BufferedStreamDemo1.java
- java复制文件
- java复制文件
- java 复制删除文件
- JAVA多线程文件复制
- java 复制文件
- 文件复制(java)
- java 复制文件/文件夹
- java文件复制
- java复制文件
- java复制文件、文件夹
- 翻译www.djangobook.com之第六章:Django管理系统admin
- Stack---python
- LRU缓存介绍与实现 (Java)
- 编译时间显示
- 想到,就要放手去做......
- java 复制文件
- c#多线程通信之委托(事件)
- java UDP Socket
- getElementBy系列
- Java世界的爱情诗
- oracle lock
- Biorhythms
- mingw 编译aac ffmpeg
- android:layout_gravity和android:gravity的区别