How to copy file in java

后端 未结 6 2012
后悔当初
后悔当初 2021-02-13 18:56

Im trying to copy a file in java and move it to a new folder. This is the code i HAve been using but I always get this error \"(Access is denied) in the specified directory\". I

6条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-13 20:00

    If you get this exception the access is really denied, i.e. you just do not have rights to write to the specified directory or file. So, first check it. Try for example to create the file in specified directory manually. Do you probably try to create file in somebody else' home directory? Or your java program is running as other user? What about foldertest2? Does it exist and writable? Try to copy your file there.

    And the final tip. When you manage to copy the file, I'd recommend you to use IOUtils.copy() (from jacarta commons). I use it a lot. It does almost exactly what you implemented but have to write of code one line only.

提交回复
热议问题