Results 1 to 10 of 10
- 06-06-2012, 09:18 PM #1
Member
- Join Date
- May 2012
- Posts
- 15
- Rep Power
- 0
Trouble copying Image file from JAR directory
Here's the code I'm using:
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: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(); }
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.Java Code:java.net.URL imageURL = getClass().getResource("Default.png"); ImageIcon picture = new ImageIcon(imageURL);
- 06-06-2012, 09:49 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,377
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-06-2012, 11:10 PM #3
Member
- Join Date
- May 2012
- Posts
- 15
- Rep Power
- 0
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.
- 06-06-2012, 11:24 PM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,604
- Rep Power
- 5
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:
A similar statement can be found on the FileReader API page.FileWriter is meant for writing streams of characters. For writing streams of raw bytes, consider using a FileOutputStream.
- 06-07-2012, 01:57 AM #5
Member
- Join Date
- May 2012
- Posts
- 15
- Rep Power
- 0
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(); } }
- 06-07-2012, 09:56 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
- 06-07-2012, 07:11 PM #7
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,604
- Rep Power
- 5
- 06-07-2012, 07:59 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,377
- Blog Entries
- 7
- Rep Power
- 17
Re: Trouble copying Image file from JAR directory
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-07-2012, 09:13 PM #9
- 06-07-2012, 09:36 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,377
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
copying file from one folder to another maintaining the file structure in java
By rahggupt in forum New To JavaReplies: 0Last Post: 11-12-2011, 01:26 PM -
Trouble copying an array
By xXRedneckXx in forum New To JavaReplies: 10Last Post: 02-05-2011, 05:36 PM -
Trouble drawing image from file picked by JFileChooser
By I = new NewToJava() in forum New To JavaReplies: 6Last Post: 12-26-2010, 03:54 PM -
add to a zip file without copying it.
By yurabita in forum New To JavaReplies: 5Last Post: 08-21-2010, 03:16 AM -
problems copying a file
By atom86 in forum Advanced JavaReplies: 16Last Post: 10-01-2009, 02:40 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks