How to copy file from one location to another location?
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);
}
Copy file from one folder to another in Java
You can use standard java.nio.file.Files.copy(Path source, Path target, CopyOption... options)
How to copy file from directory to another Directory in Java
Use Apache Commons
FileUtilsFileUtils.copyDirectory(source, desc);
How to copy file into another directory in Java 8?
Files.copy
needs the name of the target file.
Path targetFilePath = Paths.get("D:/folder/Movie.class");
This is indeed requires a bit more than the conventional "if the target is a directory, copy the file into it." On the otherhand a quite useful requirement: an InputStream no longer has a name.
How to copy files from one folder to another using Java?
You can use the Java.io.File class.
It has a method that checks if a fill exists.
Example:
//create files
File original =new File("C:\\test\\testfile.txt");
File destination =new File("D:\\test\\file.txt");
//check if file exists.
for(int x=0;destination.exists()==true;x++){
//if file exists then add 1 to file name and check if exists again.
destination=new File("D\\test\\file"+x+".txt");
}
//copy file.
Files.copy(origional, destination, StandardCopyOption.REPLACE_EXISTING);
Copy entire directory contents to another directory?
FileUtils.copyDirectory()
Copies a whole directory
to a new location preserving the file
dates. This method copies the
specified directory and all its child
directories and files to the specified
destination. The destination is the
new location and name of the
directory.The destination directory is created
if it does not exist. If the
destination directory did exist, then
this method merges the source with the
destination, with the source taking
precedence.
To do so, here's the example code
String source = "C:/your/source";
File srcDir = new File(source);
String destination = "C:/your/destination";
File destDir = new File(destination);
try {
FileUtils.copyDirectory(srcDir, destDir);
} catch (IOException e) {
e.printStackTrace();
}
Need to copy all files from one folder to another using java
Apache commons-io library has a FileUtils.copyDirectory()
method. It copies all files from source dir and its subfolders to dest dir, creating dest dir if necessary.
FileUtils docs
If you are using Gradle, add this to your dependencies section to add this library to your project:
implementation 'commons-io:commons-io:2.11.0'
Copy files from one directory to another and append new files with timestamp instead of overwriting in Java
//construct the src and dest file structure
File srcFile = new File(src, file);
File destFile = new File(dest, file);
while (destFile.exists()) {
destFile = new File(dest, file + '-' + Instant.now());
}
In one case the destination file got named test-file.txt-2018-03-14T11:05:21.103706Z
. The time given is in UTC. In any case you will end up with a name of file that doesn’t already exist (if the loop terminates, but I have a hard time seeing the scenario where it doesn’t).
You may want to append the timestamp only to plain files and reuse existing folders (directories), I don’t know your requirements here. And you may want to append the timestamp before the extension if there is one (to get test-file-2018-03-14T11:05:21.103706Z.txt
instead). I trust you to make the necessary modifications.
Related Topics
Explicitly Calling a Default Method in Java
How to Compare Two Dates Without the Time Portion
Java Random Always Returns the Same Number When I Set the Seed
What Does Java Option -Xmx Stand For
Deploying Spring 5.X on Tomcat 10.X
How to Check If a Single Character Appears in a String
How to Execute System Commands (Linux/Bsd) Using Java
Android 6.0 Open Failed: Eacces (Permission Denied)
How to Call a Static Method in Jsp/El
Delete Directories Recursively in Java
"Integer Number Too Large" Error Message for 600851475143
How to Restart a Java Application
How to Use Regular Expressions to Parse HTML in Java
How to Implement a Map with Multiple Keys