How to speed up the speed of copy in Java?

Hi,all:

I am working at a project. In this project, I write a finch to copy any format files. It works. However, I found the speed of the copy is very slow. It seems to take nearly 1 minus to copy 1MB in Java. It is so bad for the project. And the project is due alrealy, but I still cannot find a better way to speed up the copy. Could anybody be so kind to tell me how to deal with this situation? Can I import some assembler language or C code to the java code? Or other batter way?

My copy function is as below:

public void copyBytes(String source,String destin) {

try {

File inputFile = new File(source);

File outputFile = new File(destin);

FileInputStream in = new FileInputStream(inputFile);

FileOutputStream out=new FileOutputStream(outputFile);

int c;

while ((c = in.read()) != -1)

out.write(c);

in.close();

out.close();

}

catch (IOException e)

{

//System.out.println(e.getMessage());

}

}

Thanks a ton.

Paul Chen

[1080 byte] By [paulchenzhong] at [2007-9-26 9:06:00]
# 1

Buffered streams should speed it up; change

>FileInputStream in = new FileInputStream(inputFile);

>FileOutputStream out=new FileOutputStream(outputFile);

to

FileInputStream fin = new FileInputStream(inputFile);

FileOutputStream fout=new FileOutputStream(outputFile);BufferedOutputStream out = new BufferedOutputStream(fout);

BufferedInputStream int = new BufferedInputStream(fin);

(you could create the file streams directly from the strings, by the way)

jsalonen at 2007-7-1 20:11:44 > top of Java-index,Core,Core APIs...
# 2

Dear jsalonen:

It work perfectly. I cannot impress my thank in a property way. It takes 100MB in 4 minus. It is very fast than my original code. By the way, I test the same less than 1 minus by using Microsoft windows copy tool, so can we still have the other way to speed up more?

Thanks for a million ton.

Paul Chen

paulchenzhong at 2007-7-1 20:11:44 > top of Java-index,Core,Core APIs...
# 3

I've said this before, but I will say it again beacause I wasn't propely (could you also tell the right spelling of that word) answered. What's the point in streams with byte by byte reading, and the multible buffering, when you have BufferedStream, BufferedReader etc. system joint. What's wrong with

repeat blockread(file, data, 32000,dataread); blockwrite(file2, data, dataread); until dataread=0;

I wrote that in five seconds, no matter that the last time I wrote pascal was 2 years ago, when I was 15. Why this complicated streams system, which are chained to something more complex than a woman.

mikaelkuisma at 2007-7-1 20:11:44 > top of Java-index,Core,Core APIs...
# 4

The point is that you can hook up the streams to other types of streams and to readers. You can create different types of streams and readers, just like Sun has done in the standard library. That way, you can write Java objects to byte[] arrays, read Unicode strings from TCP/IP sockets, write graphics to ZIP files, etc. You can't do those things in Pascal as easily as you can in Java!!!

schapel at 2007-7-1 20:11:44 > top of Java-index,Core,Core APIs...
# 5

The default buffer size for the streams is 512 bytes. Increasing that by creating the streams with the constructors that take the size as a second parameter might give better performance.

Reading and writing byte arrays, preferrably of the same size as the buffer, instead of individual bytes could also make some difference (or not since the buffered streams do that for you already) but the code would become somewhat more complex if it's to be implemented in a reliable way.

> Dear jsalonen:

> It work perfectly. I cannot impress my thank in a

> property way. It takes 100MB in 4 minus. It is very

> fast than my original code. By the way, I test the

> same less than 1 minus by using Microsoft windows copy

> tool, so can we still have the other way to speed up

> more?

> Thanks for a million ton.

>

> Paul Chen

jsalonen at 2007-7-1 20:11:44 > top of Java-index,Core,Core APIs...
# 6
Hi, all:Thanks all.I try to make the buffer bigger, performance is good.Thanks again.Paul Chen
paulchenzhong at 2007-7-1 20:11:44 > top of Java-index,Core,Core APIs...