my dog learned polymorphism
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Copying files." Watch "Copying files." New topic

Copying files.

Chris Swanson

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

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 8 API documentation
Rob Spoor

Joined: Oct 27, 2005
Posts: 20269

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.

How To Ask Questions How To Answer Questions
Chris Swanson

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.
I agree. Here's the link:
subject: Copying files.
It's not a secret anymore!