I want to copy a file from one location to another location in Java. What is the best way to do this?
Here is what I have so far:
import java.io.File; import java.io.FilenameFilter; import java.util.ArrayList; import java.util.List; public class TestArrayList { public static void main(String[] args) { File f = new File( "D:\\CBSE_Demo\\Demo_original\\fscommand\\contentplayer\\config"); List<String>temp=new ArrayList<String>(); temp.add(0, "N33"); temp.add(1, "N1417"); temp.add(2, "N331"); File[] matchingFiles = null; for(final String temp1: temp){ matchingFiles = f.listFiles(new FilenameFilter() { public boolean accept(File dir, String name) { return name.startsWith(temp1); } }); System.out.println("size>>--"+matchingFiles.length); } } }
This does not copy the file, what is the best way to do this?
The Linux cp command is used for copying files and directories to another location. To copy a file, specify “cp” followed by the name of a file to copy. Then, state the location at which the new file should appear. The new file does not need to have the same name as the one you are copying.
To copy multiple files with the “cp” command, navigate the terminal to the directory where files are saved and then run the “cp” command with the file names you want to copy and the destination path.
You can use this (or any variant):
Files.copy(src, dst, StandardCopyOption.REPLACE_EXISTING);
Also, I'd recommend using File.separator
or /
instead of \\
to make it compliant across multiple OS, question/answer on this available here.
Since you're not sure how to temporarily store files, take a look at ArrayList
:
List<File> files = new ArrayList(); files.add(foundFile);
To move a List
of files into a single directory:
List<File> files = ...; String path = "C:/destination/"; for(File file : files) { Files.copy(file.toPath(), (new File(path + file.getName())).toPath(), StandardCopyOption.REPLACE_EXISTING); }
Using Stream
private static void copyFileUsingStream(File source, File dest) throws IOException { InputStream is = null; OutputStream os = null; try { is = new FileInputStream(source); os = new FileOutputStream(dest); byte[] buffer = new byte[1024]; int length; while ((length = is.read(buffer)) > 0) { os.write(buffer, 0, length); } } finally { is.close(); os.close(); } }
Using Channel
private static void copyFileUsingChannel(File source, File dest) throws IOException { FileChannel sourceChannel = null; FileChannel destChannel = null; try { sourceChannel = new FileInputStream(source).getChannel(); destChannel = new FileOutputStream(dest).getChannel(); destChannel.transferFrom(sourceChannel, 0, sourceChannel.size()); }finally{ sourceChannel.close(); destChannel.close(); } }
Using Apache Commons IO lib:
private static void copyFileUsingApacheCommonsIO(File source, File dest) throws IOException { FileUtils.copyFile(source, dest); }
Using Java SE 7 Files class:
private static void copyFileUsingJava7Files(File source, File dest) throws IOException { Files.copy(source.toPath(), dest.toPath()); }
Or try Googles Guava :
https://github.com/google/guava
docs: https://guava.dev/releases/snapshot-jre/api/docs/com/google/common/io/Files.html
Compare time:
File source = new File("/Users/sidikov/tmp/source.avi"); File dest = new File("/Users/sidikov/tmp/dest.avi"); //copy file conventional way using Stream long start = System.nanoTime(); copyFileUsingStream(source, dest); System.out.println("Time taken by Stream Copy = "+(System.nanoTime()-start)); //copy files using java.nio FileChannel source = new File("/Users/sidikov/tmp/sourceChannel.avi"); dest = new File("/Users/sidikov/tmp/destChannel.avi"); start = System.nanoTime(); copyFileUsingChannel(source, dest); System.out.println("Time taken by Channel Copy = "+(System.nanoTime()-start)); //copy files using apache commons io source = new File("/Users/sidikov/tmp/sourceApache.avi"); dest = new File("/Users/sidikov/tmp/destApache.avi"); start = System.nanoTime(); copyFileUsingApacheCommonsIO(source, dest); System.out.println("Time taken by Apache Commons IO Copy = "+(System.nanoTime()-start)); //using Java 7 Files class source = new File("/Users/sidikov/tmp/sourceJava7.avi"); dest = new File("/Users/sidikov/tmp/destJava7.avi"); start = System.nanoTime(); copyFileUsingJava7Files(source, dest); System.out.println("Time taken by Java7 Files Copy = "+(System.nanoTime()-start));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With