Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Copying files. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Copying files." Watch "Copying files." New topic
Author

Copying files.

Chris Swanson
Greenhorn

Joined: Nov 30, 2009
Posts: 15
I am trying to copy over some files from one file to another, but I keep getting access denied errors as an exception. Right now I am using this segment of code. How can I get around this? I have a database which stores the location of these files, and I need to do a hard copy of many files to another.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Your code looks correct.

Error messages such as "access denied" indicate that the operating system for some reason doesn't allow your program to write a file in a specific location. Check if you have the rights to write to the directory where you are trying to copy the file, if there's enough free disk space, if there isn't already an existing file with the same name as your output file that's opened by another process (so that you can't overwrite it at that moment), etc.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Jesper Young wrote:Your code looks correct.

I would make one change though - put the closing in finally blocks. By using nested try blocks you can skip any null checks, and it will also make it safer in case in.close() would throw an exception. The code:
The inner-most try-finally statement will make sure that whatever happens, out is closed. The try-finally around that will make sure that whatever happens, in is closed.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Chris Swanson
Greenhorn

Joined: Nov 30, 2009
Posts: 15
This issue has come up again, and is the main barrier preventing me from completing my program.

How do people normally store images when they need to be copied? Should I store the location of the file within the database and find ways around the operating system. If so, can I get any suggestions on how to accomplish this?

Otherwise, should I store the image in the database itself? I know this is difficult, and from what I have read not recommended.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Copying files.
 
Similar Threads
how can I append two files
problem in copying the directory
how to copy files at runtime
how to read files in a directory on a local network
using byte array to copy contents of one file to another