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,570 points
36 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,640 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
• 31,740 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
234 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
105 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,640 points
55 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
67 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 9,375 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
2,186 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,672 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
628 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
• 31,740 points
73 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
• 31,740 points
258 views