Copy files within JAR to disk
Hi. I'm a novice trying to learn some Java. Currently working on a small application. A feature in my application is that it holds some files (resources) and copies it to the hard disk.
Running it from Eclipse, it works 100% fine (!!). Unfortunately, when I export my app to a JAR and run it, files from within my application that need to be copied to the hard disk, aren't getting copied...
Here's some code that handles the copying from jar to hard disk:
I'm pretty sure the problem lies in the this.getClass().getResource() part. But not sure on what to fix. Have been googling for quite a while now, but just can't seem to find it out on my own.
File srcFile = new File(this.getClass().getResource("/srcFile.txt").toURI());
File tgtFile = new File("C:\\tgtFile.txt");
FileChannel sourceChannel = new FileInputStream(myFile).getChannel();
FileChannel targetChannel = new FileOutputStream(tgtFile).getChannel();
sourceChannel.transferTo(0, sourceChannel.size(), targetChannel);
There's probably more efficient code for copying files, but I only need a 'quick fix' for now. Can someone please help me?