Situation:
I am doing an automation where I have to download only CSV files from a set of files. And now i want to move only CSV files from one folder t
You can simply move the file this way.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class MyFilteredFileList {
public static void main(String a[]){
try {
Files.move (Paths.get("C:\\Users\\sh370472\\Downloads\\file.txt"),
Paths.get("E:\\Query\\file.txt"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Just add pathname.deleteOnExit(); on accept method
@Override
public boolean accept(File pathname) {
boolean source = pathname.getName().toLowerCase().endsWith(".csv");
if (source) {
pathname.deleteOnExit();
return true;
}
return false;
}
whole code :
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
/**
* Created by Lenovo on 02/12/2018.
*/
public class FileMove {
public static void main(String a[])
{
try {
File source = new File("C:\\Users\\sh370472\\Downloads");
File dest = new File("E:\\Query\\");
FileUtils.copyDirectory(source, dest, new FileFilter() {
@Override
public boolean accept(File pathname) {
boolean source = pathname.getName().toLowerCase().endsWith(".csv");
if (source) {
pathname.deleteOnExit();
return true;
}
return false;
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}
Copy this class and run :) Try setting the correct source and destination path if it doesn't work.
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public class MoveFile {
private boolean moveFile(String source, String destination) throws IOException {
Path srcPath = Paths.get(source);
Path destPath = Paths.get(destination);
System.out.println("Moving processed file to archive");
Files.move(srcPath, destPath, StandardCopyOption.REPLACE_EXISTING);
return true;
}
private File[] getTheFilesWithExtension(String filePath, String ext) {
File fileDir = new File(filePath);
File[] files = fileDir.listFiles((dir, name) -> name.toLowerCase().endsWith(ext));
return files;
}
public static void main(String... args){
{
MoveFile moveFile = new MoveFile();
String sourcePath = "C:\\\\Users\\\\sh370472\\\\Downloads";
String destPath = "E:\\Query\\";
String extension = ".csv";
try {
File[] files = moveFile.getTheFilesWithExtension(sourcePath, extension);
for (File file : files){
moveFile.moveFile(file.getPath(),destPath+file.getName());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Note: Its for Java 8 and above , for lower java version, you can update the lambda expression in getTheFilesWithExtension with the below code
private File[] getTheFilesWithExtension(String filePath, String ext) {
File fileDir = new File(filePath);
File[] files = fileDir.listFiles(
new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(ext);
}
}
);
return files;
}