Saturday, August 27, 2011
Copying files in Java
This is something I've never really done much of, but when I've seen snippets of code which do this I've always thought how ugly and cumbersome it looks:
File srcFile = new File(sourceFilePath);
File destFile = new File(destinationFilePath);
FileInputStream in = new FileInputStream(srcFile);
FileOutputStream out = new FileOutputStream(destFile);
byte[] buf = new byte[2048];
int len;
while ((len = in.read(buf)) > 0)
{
try
{
out.write(buf, 0, len);
}
catch (Exception e)
{
}
}
out.flush();
in.close();
out.close();
etc...
But there is now, at last, an easier way:
import java.nio.channels.FileChannel;
...
File srcFile = new File(sourceFilePath);
File destFile = new File(destinationFilePath);
FileInputStream in = new FileInputStream(srcFile);
FileOutputStream out = new FileOutputStream(destFile);
FileChannel srcFileChannel = in.getChannel();
FileChannel destFileChannel = out.getChannel();
destFileChannel.transferFrom(srcFileChannel, 0, srcFileChannel.size());
srcFileChannel.close();
destFileChannel.close();
...
Sunday, August 7, 2011
Building Android with Ant
To create a build using an ant build.xml script, you'll need a local.properties file. This point to your sdk installation directory.
You can create this with the android tool like so:
android update project --path .
Execute from your project's directory. The full stop is important!
Remember you'll need to add keystore and proguard if you want to sign/obfustcate etc:
key.store=/path_to_keystore/keystore
key.alias=key_store_alias
proguard.dir=/path_to_proguard/lib (seems to use lib dir rather than bin)
(See here for more about signing and obfuscation)
You can create this with the android tool like so:
android update project --path .
Execute from your project's directory. The full stop is important!
Remember you'll need to add keystore and proguard if you want to sign/obfustcate etc:
key.store=/path_to_keystore/keystore
key.alias=key_store_alias
proguard.dir=/path_to_proguard/lib (seems to use lib dir rather than bin)
(See here for more about signing and obfuscation)
Subscribe to:
Posts (Atom)