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,550 points
19 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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

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,840 points
111 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
49 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,600 points
39 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,840 points
30 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,360 points

edited Nov 6, 2018 by Omkar 5,344 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,050 points
1,414 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,050 points
3,876 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,050 points
272 views
0 votes
1 answer

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

You can try using this: List<String> list = ...READ MORE

answered May 21, 2018 in Java by geek.erkami
• 2,320 points
54 views
0 votes
1 answer

Listing all the subclasses of a specific class in Java

There is a simple library available for ...READ MORE

answered Jun 7, 2018 in Java by geek.erkami
• 2,320 points
99 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.