Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: copying file



Hi,

If you are going to use the buffered streams, you may want to use the
constructor with a buffer size which matches the size of your byte array.
e.g. BufferedInputStream(InputStream in, int size)

For fast copying of files it is best to use the NIO (new I/O) API introduced
in Java 1.4., the underlying support code is more optimized on most OS when
compared to the existing IO API methods.

http://java.sun.com/j2se/1.4.2/docs/api/java/nio/channels/FileChannel.html

Here's a method I've used on multiple platforms with success:

public static void copyFile(File src, File dst) throws IOException {
if (dst.isDirectory())
dst = new File(dst, src.getName());

dst.createNewFile();
FileChannel dstChannel = new FileOutputStream(dst).getChannel();
FileChannel srcChannel = new FileInputStream(src).getChannel();
dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
dstChannel.close();
srcChannel.close();
}

However, I'm unsure how Apple Java deals with forked files as I don't think
I've ever tried to copy one with Java! Ideally, it would be hidden from the
user by the underlying implementation; worse case scenario it is like the
BSD commands which are unaware of their existence.

I would think that file attributes such as type & creator codes should be
taken care of with a byte level copy but, if you need to poke at them, the
com.apple.eio.FileManager class provides getter and setter methods.

Phil.
_______________________________________________
java-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/java-dev
Do not post admin requests to the list. They will be ignored.


References: 
 >Re: copying file (From: Daniel Child <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.