I'm working on a project that involves taking millions (literally) of .txt files in one folder, then copying and moving them to 3 different folders - named 2012, 2013, and 2014.. In each filename, the first 13 characters can be ignored, but the 14th states the year the file was made (either 2, 3, or 4, for 2012-2014).
So far I have been able to successfully move one file at a time between folders, but cannot do it for multiple files and cannot figure out how to move them based on the character integer value.
I have attached my code so far. I have read many other posts (particularly Move / Copy File Operations in Java) but cannot seem to find what I'm stuck on. Any and all help would be greatly appreciated. Thank you.
Here is the UPDATED Copy class.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class CopyAndMove {
public static void main(String[] args) {
try {
Path afile = Paths.get("C:\\Users\\Documents\\testblankfiles\\
"xxxxxxxxxxxxx2.txt");
Path bfile = Paths.get("C:\\Users\\Documents\\testblankfilesdest\\
2012\\"xxxxxxxxxxxxx2.txt");
Files.copy(afile, bfile);
/**_IF YOU WANT_ to delete the original file.*/
//afile.delete();
System.out.println("File is copied successful!");
}
catch(IOException e) {
e.printStackTrace();
}
}
}
And the other class Move:
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class MoveFileExample {
public static void main(String[] args){
try {
Path afile = Paths.get("C:\\Users\\\\Documents\\testblankfiles
\\xxxxxxxxxxxxx2.txt");
Files.move(afile,
Paths.get("C:\\Users\\Documents\\testblankfilesdest\\2012",
afile.getFileName().toString()));
}
catch(Exception e){
e.printStackTrace();
}
}
}
EDIT/UPDATE:
After the help I have received from comments and answers, I have been able to successfully move a single file. However, I need to do this for millions of files. I have written pseudo-code for a For Each Loop with If-Then Statements:
Files directory = all files in "C:\\Users\\Documents\\TestBlankFiles\\";
File FileName{Is this even necessary} = current FileName; (want to recursively[?]
iterate[?] through each FileName in
\\TestBlankFiles, copying and moving each
file based on 14th character in FileName)
Directory<Files> ListOfFiles = new Directory<Files>; //I KNOW THIS IS NOT RIGHT
for (File FileName: "C:\\Users\\Documents\\TestBlankFiles\\") {
if (14th character in FileName == 2) {
MOVE FileName to "C:\\Users\Documents\\TestBlankFilesDest\\2012";
}
if (14th char in FileName == 3) {
MOVE FileName to "C:\\Users\Documents\\TestBlankFilesDest\\2013"'
}
if (14th char in FileName == 4) {
MOVE FileName to "C:\\Users\Documents\\TestBlankFilesDest\\2014";
}
}
The answers to which you've linked are out of date. As of Java 1.7 (which came out in 2011), you should be copying files this way:
Path afile = Paths.get("C:\\Users\\Documents\\testblankfiles\\xxxxxxxxxxxxx2.txt");
Path bfile = Paths.get("C:\\Users\\Documents\\testblankfilesdest\\2012\\xxxxxxxxxxxxx2.txt");
Files.copy(afile, bfile);
And you should be moving files this way:
Path afile = Paths.get("C:\\folderA\\Afile.txt");
Files.move(afile,
Paths.get("C:\\folderB", afile.getFileName().toString()));
Notice that Files.move doesn't return a value because, in proper object-oriented fashion, it throws an IOException if the move fails, unlike the ancient File.renameTo method.
JB Nizet is correct about determining your destination folders: Use the charAt
(or substring
) method on the result of Path.getFileName().toString().
To go through all the files in a directory, use a DirectoryStream:
try (DirectoryStream<Path> dir = Files.newDirectoryStream(
Paths.get("C:\\Users\\Documents\\TestBlankFiles"))) {
for (Path file : dir) {
String name = file.getFileName().toString();
if (!Files.isDirectory(file) && name.length() >= 14) {
char yearDigit = name.charAt(13);
Path destDir = Paths.get(
"C:\\Users\\Documents\\TestBlankFiles\\201" + yearDigit);
Files.createDirectories(destDir);
Files.move(file, destDir.resolve(name));
}
}
}