Results 1 to 3 of 3
  1. #1
    RaF
    RaF is offline Member
    Join Date
    Aug 2010
    Posts
    1
    Rep Power
    0

    Default 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:

    Java Code:
    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);
    sourceChannel.close();
    targetChannel.close();
    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.

    There's probably more efficient code for copying files, but I only need a 'quick fix' for now. Can someone please help me?
    Last edited by RaF; 08-31-2010 at 03:16 PM.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default

    Hi,

    Try using getResourceAsStream() method instead.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    If the "file" is in a jar file, can FileChannel read it?
    You may have to change your I/O classes to read an entry from a jar file.

Similar Threads

  1. Copy files between servers
    By mcoto800 in forum Java Servlet
    Replies: 1
    Last Post: 02-05-2010, 01:05 AM
  2. Getting disk label
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-05-2008, 09:07 AM
  3. copy image/imageicon into a file on disk
    By archanajathan in forum Advanced Java
    Replies: 2
    Last Post: 11-22-2007, 06:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •