How to move or copy a file in Java

0 votes

Does Java provide any standard library that handles the common file operations like moving/copying files/folders?

Nov 2, 2018 in Java by 93.lynn
• 1,560 points
33 views

2 answers to this question.

0 votes

You can follow the below java.nio operations:

public static void fileCopy(File srcFile, File destFile) throws IOException {
    if(!destFile.exists()) {
        destFile.createNewFile();
    }

    FileChannel src= null;
    FileChannel dstn= null;
    try {
        src= new FileInputStream(srcFile).getChannel();
        dstn= new FileOutputStream(destFile).getChannel();

        // previous code: destination.transferFrom(source, 0, source.size());
        // to avoid infinite loops, should be:
        long count = 0;
        long size = src.size();              
        while((count += dstn.transferFrom(src, count, size-count))<size);
    }
    finally {
        if(src != null) {
            src.close();
        }
        if(dstn!= null) {
            dstn.close();
        }
    }
}


Hope this helps!!

answered Nov 2, 2018 by geek.erkami
• 2,320 points
0 votes

The new JAVA Specification Request 203 -NIO will have support for these common operations.

There are a few considerations however,

File.renameTo generally works only on the same file system volume. This is most likely equivalent to a "mv" command. You can use it if you wish, but for general copy and move support, you'll need to have a fallback.

When a rename doesn't work you will need to actually copy the file (deleting the original with File.delete if it's a "move" operation). 

To do this with the efficiently, use the FileChannel.transferTo or FileChannel.transferFrom methods. 

The implementation is platform specific, but in general, when copying from one file to another, implementations avoid transporting data back and forth between kernel and user space, yielding a big boost in efficiency.

answered Aug 20 by Sirajul
• 25,540 points

Related Questions In Java

0 votes
3 answers

How to check whether a file exists or not in Java?

Using nio we can check whether file ...READ MORE

answered Aug 14, 2018 in Java by Sushmita
• 6,880 points
190 views
0 votes
3 answers

How to read a Text File in Java?

You can use readAllLines and the join method to ...READ MORE

answered Jul 27, 2018 in Java by samarth295
• 2,190 points
88 views
0 votes
2 answers

How to read a text file in Java?

You can use Scanner class to read ...READ MORE

answered Aug 9, 2018 in Java by parth295
• 4,620 points
49 views
0 votes
2 answers

How to find out a single character appears in String or not in Java?

You can use string.indexOf('s'). If the 's' is present in string, ...READ MORE

answered Aug 7, 2018 in Java by Sushmita
• 6,880 points
46 views
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,380 points

edited Nov 6, 2018 by Omkar 8,166 views
0 votes
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,090 points
1,990 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,090 points
5,184 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,090 points
504 views
0 votes
2 answers

How to convert ArrayList<String> to String[] in Java

Try this:  String[] strArray = arrayList.toArray(new Stri ...READ MORE

answered Aug 21 in Java by Sirajul
• 25,540 points
68 views
0 votes
2 answers

Listing all the subclasses of a specific class in Java

This is not possible to do using ...READ MORE

answered Aug 21 in Java by Sirajul
• 25,540 points
184 views