java文件复制速度,加快java中的复制速度

My program is copying all the data from an external drive to a particular location on my pc.

Here is my program :-

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

public class Copy

{

public static void main(String[] args)

{

String[] letters = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I"};

File[] drives = new File[letters.length];

int copy=0;int l;File files[]=null;boolean pluggedIn=false;

FileInputStream fis=null;

FileOutputStream fos=null;

boolean[] isDrive = new boolean[letters.length];

for (int i = 0; i < letters.length; ++i)

{

drives[i] = new File(letters[i] + ":/");

isDrive[i] = drives[i].canRead();

}

System.out.println("FindDrive: waiting for devices...");

while (true)

{

try

{

for (int i = 0; i < letters.length; ++i)

{

pluggedIn = drives[i].canRead();

if (pluggedIn != isDrive[i])

{

if (pluggedIn)

{

System.out.println("Drive " + letters[i] + " has been plugged in");

files = drives[i].getAbsoluteFile().listFiles();

File file;

int fread;

for (l = 0; l < files.length; l++)

{

if (files[l].isFile())

{

file = new File("G://copied//" + files[l].getName());

file.createNewFile();

fis = new FileInputStream(drives[i].getAbsolutePath() + files[l].getName());

fos = new FileOutputStream(file);

while (true)

{

fread = fis.read();

if (fread == -1)

{

break;

}

fos.write(fread);

}

}

else

{

func(files[l].getAbsoluteFile(), "G://copied");

}

if(l==files.length-1)

{

System.out.print("copy complete");

fos.close();

fis.close();

}

}

}

else

{

System.out.println("Drive " + letters[i] + " has been unplugged");

}

isDrive[i] = pluggedIn;

}

}

Thread.sleep(5000);

}

catch (FileNotFoundException e) { }

catch (IOException e) { }

catch (InterruptedException e) {}

}

}

public static void func(File dir, String path)

{

File file = new File(path + "//" + dir.getName());

file.mkdir();

File[] files = dir.listFiles();

FileInputStream fis;

FileOutputStream fos;

int fread;

File file1;

for (int i = 0; i < files.length; i++)

{

if (files[i].isFile())

{

file1 = new File(file.getAbsolutePath() + "//" + files[i].getName());

try

{

file1.createNewFile();

fis = new FileInputStream(files[i]);

fos = new FileOutputStream(file1);

while (true)

{

fread = fis.read();

if (fread == -1)

{

break;

}

fos.write(fread);

}

} catch (FileNotFoundException e) {} catch (IOException e) {}

}

else

{

func(files[i], file.getAbsolutePath());

}

}

}

}

Now it is taking too long to copy large files.

Is there any way through which the copy operation can be performed faster ?

Thanx in advance for any suggestion.

解决方案

You have to use a buffer. The copy logic should be something like:

byte[] buffer = new byte[4096];

int n;

while ((n = input.read(buffer) != -1)

{

output.write(buffer, 0, n);

}

output.close();

input.close();

This way, you copy a chunk of 4096 bytes at once, instead of byte per byte.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值