Results 1 to 10 of 10
  1. #1
    Join Date
    May 2012
    Posts
    15
    Rep Power
    0

    Default Trouble copying Image file from JAR directory

    Here's the code I'm using:

    Java Code:
     public void copyFile()throws IOException{
        	  File inputFile = new File("Default.png");
        	   File outputFile = new File("copy.png");
        	   FileReader in = new FileReader(inputFile);
        	   FileWriter out = new FileWriter(outputFile);
        	   int c;
    
        	   while ((c = in.read()) != -1)
        	     out.write(c);
    
        	   in.close();
        	   out.close();
        }
    Unfortunately, it isn't doing what its supposed to. I have an image file called "Default.png" in the directory where the JAR file is. I have another method confirming that it is able to find this file:

    Java Code:
    java.net.URL imageURL = getClass().getResource("Default.png");
    ImageIcon picture = new ImageIcon(imageURL);
    The method is supposed to make a copy of the file "Default.png" called "copy.png". The original snippet of code I got the method top method from actually had hard coded filepaths, e.g. "c://documents," but I need my program to be able to simply look for the file in whatever directory the JAR file is in - the default directory. I thought I'd beable to do that the same way I as I did in another method used to read Text files - by simply putting the name of the file. Not sure why this isn't working. Help is much appreciated. Hopefully I've explained the problem well enough.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    11,377
    Blog Entries
    7
    Rep Power
    17

    Default Re: Trouble copying Image file from JAR directory

    Is that image stored in your .jar file? If so, it isn't a file, it is a jar entry; you can get an input stream from that entry by using the getResourceAsStream( ... ) method; don't use Readers, they handle characters (including their encoding) not bytes.

    kind regards,

    Jos
    When people rob a bank they get a penalty; when banks rob people they get a bonus.

  3. #3
    Join Date
    May 2012
    Posts
    15
    Rep Power
    0

    Default Re: Trouble copying Image file from JAR directory

    Sorry, the file isn't actually in the .jar file, but the directory that the .jar file is in.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,604
    Rep Power
    5

    Default Re: Trouble copying Image file from JAR directory

    As Tolls pointed out, don't use Reader/Writer. And to drive the point home, see the API
    FileWriter (Java Platform SE 6)
    And pay special attention to the following line:
    FileWriter is meant for writing streams of characters. For writing streams of raw bytes, consider using a FileOutputStream.
    A similar statement can be found on the FileReader API page.

  5. #5
    Join Date
    May 2012
    Posts
    15
    Rep Power
    0

    Default Re: Trouble copying Image file from JAR directory

    Swell, thanks! Here's the final code:

    Java Code:
     public void copyFile(String filenameoriginal, String filenamecopy)throws IOException{
        	   File destFile = new File(filenamecopy);
        	   if (!destFile.exists()) {
        	       destFile.createNewFile();
        	   }
    
        	   FileChannel fileSource = new FileInputStream(filenameoriginal).getChannel();
        	   FileChannel destination = new FileOutputStream(filenamecopy).getChannel();
        	   destination.transferFrom(fileSource, 0, fileSource.size());
    
        	   System.out.println("Success.");
        	   if (fileSource != null) {
        	       fileSource.close();
        	   }
        	   if (destination != null) {
        	       destination.close();
        	   }
        }

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    10,438
    Rep Power
    16

    Default Re: Trouble copying Image file from JAR directory

    Quote Originally Posted by doWhile View Post
    As Tolls pointed out, don't use Reader/Writer.
    I did?
    ;)
    Please do not ask for code as refusal often offends.

  7. #7
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,604
    Rep Power
    5

    Default Re: Trouble copying Image file from JAR directory

    Quote Originally Posted by Tolls View Post
    I did?
    ;)
    Now that was a full on brain fart...I mean JosAH

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    11,377
    Blog Entries
    7
    Rep Power
    17

    Default Re: Trouble copying Image file from JAR directory

    Quote Originally Posted by doWhile View Post
    Now that was a full on brain fart...I mean JosAH
    I did? ;-) I don't remember anything and I'll deny any accusations! I didn't do nothing! I was asleep! I mean, where am I? Who are you folks? Don't you stare at me that way! Help! I'm being oppressed!

    kind regards,

    Jos (innocent as always)
    When people rob a bank they get a penalty; when banks rob people they get a bonus.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Moderator
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    9,918
    Rep Power
    16

    Default Re: Trouble copying Image file from JAR directory

    Quote Originally Posted by doWhile View Post
    Now that was a full on brain fart...I mean JosAH
    Jos? He hasn't posted anything in this thread.

    db (exit, stage right)
    Why do they call it rush hour when nothing moves? - Robin Williams

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    11,377
    Blog Entries
    7
    Rep Power
    17

    Default Re: Trouble copying Image file from JAR directory

    Quote Originally Posted by DarrylBurke View Post
    Jos? He hasn't posted anything in this thread.

    db (exit, stage right)
    See? See? I told you so. case closed.

    kind regards,

    Jos (runs ---------------------------------------------------------> that a'way)
    When people rob a bank they get a penalty; when banks rob people they get a bonus.

Similar Threads

  1. Replies: 0
    Last Post: 11-12-2011, 01:26 PM
  2. Trouble copying an array
    By xXRedneckXx in forum New To Java
    Replies: 10
    Last Post: 02-05-2011, 05:36 PM
  3. Trouble drawing image from file picked by JFileChooser
    By I = new NewToJava() in forum New To Java
    Replies: 6
    Last Post: 12-26-2010, 03:54 PM
  4. add to a zip file without copying it.
    By yurabita in forum New To Java
    Replies: 5
    Last Post: 08-21-2010, 03:16 AM
  5. problems copying a file
    By atom86 in forum Advanced Java
    Replies: 16
    Last Post: 10-01-2009, 02:40 PM

Posting Permissions

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