简体   繁体   中英

How to copy file in java

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". Is there a way i can fix this or a better way to copy the files? thanks

try{
          File f1 = new File(fpath);
          File f2 = new File("C:/users/peter/documents/foldertest2/hats");
          InputStream in = new FileInputStream(f1);

          //For Append the file.
          //OutputStream out = new FileOutputStream(f2,true);

          //For Overwrite the file.
          OutputStream out = new FileOutputStream(f2);

          byte[] buf = new byte[1024];
          int len;
          while ((len = in.read(buf)) > 0){
            out.write(buf, 0, len);
          }
          in.close();
          out.close();
          System.out.println("File copied.");
        }
        catch(FileNotFoundException ex){
          System.out.println(ex.getMessage() + " in the specified directory.");
          System.exit(0);
        }
        catch(IOException e){
          System.out.println(e.getMessage());      
        }

UPDATE: I checked the folder permissions and they are all open for all users and mine

Apache Commons IO is also another way to go, specifically FileUtils.copyFile(); it handles all the heavy lifting for you.

Use Java 7:

import static java.nio.file.StandardCopyOption.*;

Files.copy(source, target, REPLACE_EXISTING);

http://docs.oracle.com/javase/tutorial/essential/io/copy.html

Is there a way i can fix this or a better way to copy the files?

If you have the option, I would recommend you to go with Java version 7 , and use the Path.copyTo method.

Copy the file located by this path to a target location. [...]

Otherwise I would recommend at least using the NIO packages and FileChannels .

Edit ups messed up, second try:

You have to give the FileOutputStream a valid file name, just append the name of your file to the target path C:/users/peter/documents/foldertest2/hats/hat3 with only the folder name it will try to access the folder as if it was a file and fail.

If you get this exception the access is really denied, ie 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.

Hmm it looks like you are trying to run this on windows, should you not be using \\ in your path instead of / ?

As AlexR said check your permissions on the directory you are trying to write to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM