黑马程序员技术交流社区
标题:
怎么样的拷贝文件方式才是高效的
[打印本页]
作者:
张绍成
时间:
2011-12-23 21:12
标题:
怎么样的拷贝文件方式才是高效的
本帖最后由 tianshan20081 于 2011-12-25 15:31 编辑
怎么样的拷贝文件方式才是高效的;
我这样写高效吗?
代码:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Timestamp;
//利用 FileInputStream 来实现输入,利用FileOutputStream 来实现输出,用以实现文件的复制
public class FileCopy {
public static void main(String[] args) throws Exception {
FileInputStream fis = null ;
FileOutputStream fos = null ;
try {
//创建字节输入流
fis = new FileInputStream("d:\\JDK.6.Documentation.chm");
//创建字节输出流
fos = new FileOutputStream("d:\\download\\java help.chm");
byte[] bbuf = new byte[1024];
int len = 0 ;
Timestamp start = new Timestamp(System.currentTimeMillis());
System.out.println(start);
//循环重输入流中读取数据
while((len=fis.read(bbuf))!=-1){
//每读取一次就将数据写入一次,读了多少就写多少
fos.write(bbuf, 0, len);
}
Timestamp end = new Timestamp(System.currentTimeMillis());
System.out.println(end);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
// 利用 finally 关闭输入输出流。finally 是程序必须执行的部分
if(fis!=null){
fis.close();
}
if(fos!=null){
fos.close();
}
}
}
}
复制代码
作者:
袁泽宇
时间:
2011-12-23 21:38
本帖最后由 袁泽宇 于 2011-12-23 21:40 编辑
第一种方法,是采用普通的IO,Buffered流,速度一般;
另一种是采用Channel流的transfer方法,速度超快。
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
public class TestTransfer {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("c:\\abc.txt");
FileOutputStream fos = new FileOutputStream("c:\\123.txt");
FileChannel fc1 = fis.getChannel();
FileChannel fc2 = fos.getChannel();
fc2.transferFrom(fc1, 0, fc1.size());
fc1.close();
fc2.close();
fis.close();
fos.close();
}
}
复制代码
作者:
周吉明
时间:
2011-12-24 18:39
public static void fileCopy( File in, File out ) throws IOException
{
FileChannel inChannel = new FileInputStream( in ).getChannel();
FileChannel outChannel = new FileOutputStream( out ).getChannel();
try
{
inChannel.transferTo(0, inChannel.size(), outChannel);
// original -- apparently has trouble copying large files on Windows
// magic number for Windows, 64Mb - 32Kb)
int maxCount = (64 * 1024 * 1024) - (32 * 1024);
long size = inChannel.size();
long position = 0;
while ( position < size )
{
position += inChannel.transferTo( position, maxCount, outChannel );
}
}
finally
{
if ( inChannel != null )
{
inChannel.close();
}
if ( outChannel != null )
{
outChannel.close();
}
}
}
复制代码
作者:
黄湘怡
时间:
2011-12-24 21:21
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
public class DirDemo {
private static File getDir() throws IOException {
System.out.println("请输入路径:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
File f = new File(line);
return f;
}
}
public class Test3 {
public static void main(String[] args) throws IOException {
File file = DirUtil.getDir();
File dest = DirUtil.getDir();
fileCopy(file,dest);
}
public static void fileCopy(File src, File dest) throws IOException {
File newDest = new File(dest, src.getName());
newDest.mkdir();
File[] fi = src.listFiles();
for (File f : fi) {
if (f.isDirectory()) {
fileCopy(f, newDest);
} else {
FileInputStream fs = new FileInputStream(f);
FileOutputStream fo = new FileOutputStream(new File(newDest, f.getName()));
byte[] b = new byte[1024];
int len;
while ((len = fs.read(b)) != -1) {
fo.write(b, 0, len);
}
fs.close();
fo.close();
}
}
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2