Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Copying files.

 
Chris Swanson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chris Swanson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic