tags:

views:

60

answers:

2

Hi,

I'm trying to create a small application that will copy some .jar files into the latest jre.

Is there anyway of finding out which is this path? I've looking at the File class and I've found several methods that will create an empty file, but I didn't find anything that would help me copying a file into a given path.

Am I missing any important class?

Thanks

+1  A: 

Firstly there isn't a helper method for copying a file until Java 7 (not yet released). Secondly it is inadvisable to try copying into the JRE directory because you may not have sufficient permission. To find the location of the JRE use System.getProperty("java.home") To copy:

byte[] buffer = new byte[16384];
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);
while (true) {
int n = in.read(buffer);
if (n == -1)
break;
out.write(buffer, 0, n);
}
in.close();
out.close();
Mark Thornton
+2  A: 

To copy files you can use the java.nio.channels.FileChannel class from the nio library. package.

For example:

// Create channel for the source
FileChannel srcChannel = new FileInputStream("srcFileLocation").getChannel();

// Create channel for the destination
FileChannel dstChannel = new FileOutputStream("dstFileLocation").getChannel();

// Copy file contents from source to destination
dstChannel.transferFrom(srcChannel, 0, srcChannel.size());

// Close the channels
srcChannel.close();
dstChannel.close();
Aaron