File not moving from folder to another - java

I am trying to find the latest 10 files from a folder and then move it to another folder, I am able to find the latest 10 files but the files won't move to another folder, renameTo is always returning false and canWrite returns true.
What could be the possible reasons and solutions?
class MoveFiles
{
int numOfForms;
String source;
String destination;
static String finalDest;
String stateName;
MoveFiles(int numOfForms,String source,String destination, String stateName)
{
System.out.println("Move Files Constructor");
this.numOfForms=numOfForms;
this.source=source;
this.destination=destination;
this.stateName=stateName;
}
public void mFile()
{
System.out.println("Starting moving files");
finalDest = "C:\\PDF\\"+destination+"\\"+stateName;
File f = new File(finalDest);
System.out.println("Can Write ? : "+f.canWrite());
System.out.println(f.mkdirs());
for ( int i=0;i<10;i++)
{
File moveit = MoveFiles.lastFileModified(source);
System.out.println(moveit.renameTo(f));
}
}
public static File lastFileModified(String dir) {
File fl = new File(dir);
File[] files = fl.listFiles(new FileFilter() {
public boolean accept(File file) {
return file.isFile();
}
});
long lastMod = Long.MIN_VALUE;
File choice = null;
for (File file : files) {
if (file.lastModified() > lastMod) {
choice = file;
lastMod = file.lastModified();
}
}
return choice;
}
}

Related

Sort files in a directory by timstamp string in file name after second -

I'm trying to sort the files in a directory by unix time-stamp. Following is the files name in a directory.
20151102-2148040042-1446522484838-Game21500052-x.realtime
20121102-2148010042-1446522484138-Game21500052-x.realtime
I need to get the files after sorting unix timestamp after second - . How I can do that in java?
I am able to do that in python like below
def extract_timestamp(filename):
timestamp = filename.split('-')[2]
return timestamp
directory = '/home/ubuntu/assdd/'
# Get all files from the path
log_files = os.listdir(directory)
# Sort files by timestamp
log_files.sort(key=extract_timestamp)
# Get full path of each file
files = [os.path.join(directory,data_file) for data_file in log_files]
A very direct approach:
List<String> fileNames = ... ;
List<String> sortedFileNames = fileNames.stream()
.sorted(Comparator.comparingLong(s -> Long.parseLong(s.split("-")[2])))
.collect(Collectors.toList());
The sorted call deserves some explanation:
It creates a Comparator that compares Long values (Comparator<Long>). It gets the Long value by splitting the fileName String into parts separated by -. It then takes the third element of the resulting split, which contains a number. It then converts this number to a long.
You can sort all files with a custom comparator, here you can parse the timestamp and compare file's timestamp. The simplest way is:
private String getTimestamp(File file) {
return file.getName().substring(20, 33);
}
public List<File> sortFilesInDir(String yourpath) {
File folder = new File(yourpath);
File[] listOfFiles = folder.listFiles();
if (listOfFiles == null) {
return null;
}
List<File> files = Arrays.asList(listOfFiles);
Collections.sort(files, new Comparator<File>() {
#Override
public int compare(File o1, File o2) {
return getTimestamp(o1).compareTo(getTimestamp(o2));
}
});
return files;
}
If you have a lot of files and you don't want to parse timestamp every time, you can save each file to a record like:
private static class FileRecord {
private File file;
private String timestamp;
public FileRecord(File file) {
this.file = file;
this.timestamp = file.getName().substring(20, 33);
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
}
private List<FileRecord> convertFilesToRecords(File[] files) {
List<FileRecord> records = new ArrayList<>();
for (File file : files) {
records.add(new FileRecord(file));
}
return records;
}
public List<File> sortFilesInDir(String yourpath) {
File folder = new File(yourpath);
List<FileRecord> records = convertFilesToRecords(folder.listFiles());
Collections.sort(records, new Comparator<FileRecord>() {
#Override
public int compare(FileRecord o1, FileRecord o2) {
return o1.getTimestamp().compareTo(o2.getTimestamp());
}
});
List<File> files = new ArrayList<>();
for (FileRecord record : records) {
files.add(record.getFile());
}
return files;
}

How to Fetch a file name from a folder with current Date and Time using Java Code

Am new to java automation and i have a scenario where i need to navigate to a particular folder, the folder has a list of files i need to filter by date and fetch a filename that got generated recently i.e. if the current time is 5:30pm i need to find the file name that got created between 5:28pm to current time (5:30pm)
This has to be achieved using Java code, File names in the folder
DOF_US_DELL_1.0_20160930_0516.CSV
DOF_US_DELL_1.0_20160930_0756.CSV
DOF_US_DELL_1.0_20161003_0346.CSV
DOF_US_DELL_1.0_20161003_0536.CSV
DOF_US_DELL_1.0_20161004_0747.CSV
DOF_US_DELL_1.0_20161005_0527.CSV
Here is a piece of code that helps me to fetch the list of files in a directory
File dir = new File("C:\\FolderName");
FilenameFilter filter = new FilenameFilter() {
public boolean accept
(File dir, String name) {
return name.startsWith("DOF");
}
};
String[] children = dir.list(filter);
if (children == null) {
System.out.println("Either dir does not exist or is not a directory");
}
else
{
System.out.println("# of the files in the folder is: "+children.length);
if(children.length>0)
{
for (int i=0; i<children.length; i++)
{
String filename = children[i];
System.out.println(filename);
}
}
else
{
System.err.println("# of the files in the folder is: "+children.length);
fail("# of the files in the folder is: "+children.length);
}
}
Thanks in Advance.
Try this. Here I am sorting files on the basis of their last updated time.
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith("cred");
}
};
List<FileOrder> list = new ArrayList<FileOrder>();
File dir = new File("/tmp/");
for (File file : dir.listFiles(filter)) {
list.add(new FileOrder(file.getName(), file.lastModified()));
}
Collections.sort(list);
System.out.println(list);
System.out.println("Last updated file : " + (list != null ? list.get(0) : null));
Above code is part of main function.
Below code is separate class.
public class FileOrder implements Comparable<FileOrder> {
private String fileName;
private Long updationTIme = 0l;
#Override
public String toString() {
return "FileOrder [fileName=" + fileName + ", updationTIme=" + updationTIme + "]";
}
public FileOrder(String fileName, Long updationDate) {
super();
this.fileName = fileName;
this.updationTIme = updationDate;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public Long getUpdationDate() {
return updationTIme;
}
public void setUpdationDate(Long updationDate) {
this.updationTIme = updationDate;
}
#Override
public int compareTo(FileOrder o) {
return o.getUpdationDate().compareTo(this.getUpdationDate());
}
}

How to Convert List of Folders and SubFolder into JSOn FOrmat

i want to populate Folder Name With Sub Folder name on KendoDrop Down . so i want to Convert Folder Directory in JSOn Format How can i Do That ?
public class FolderPath {
public static void main(String[] args) throws IOException {
File currentDir = new File("Folder URL "); // current directory
displayDirectoryContents(currentDir);
}
public static void displayDirectoryContents(File dir) {
StringBuilder sb1 = new StringBuilder("[");
try {
File[] files = dir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
sb1 = sb1.append("{\"JSONKEY\":\"" + file.getCanonicalPath() + "\"},");
String str = file.getCanonicalPath();
displayDirectoryContents(file);
} else {
}
}
sb1.deleteCharAt(sb1.length() - 1);
sb1 = sb1.append("]");
System.out.println("s2==>" + sb1);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Here i am Not Getting Full Directroy into JSOn Please Help
You are creating a StringBuilder object on each iteration. That's why your concatenation does not work.
Consider the contents of you C:\test is composed of 3 directories:
c:\test
|
+--css
| +--less
+--js
The code below, returns:
[{"JSONKEY":"C:\test\css"},
{"JSONKEY":"C:\test\css\less"},
{"JSONKEY":"C:\test\js"}]
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
public class FolderPath {
private static FileFilter onlyDirectories = new FileFilter() {
#Override
public boolean accept(File file) {
return file.isDirectory();
}
};
public static void main(String[] args) {
File currentDir = new File("C:\\test"); // current directory
displayDirectoryContents(currentDir);
}
public static void displayDirectoryContents(File dir) {
StringBuilder sb1 = new StringBuilder("[");
doDisplayDirectoryContents(dir, sb1);
if (sb1.length() > 1) {
sb1.deleteCharAt(sb1.length() - 1);
}
sb1.append("]");
System.out.println(sb1);
}
private static void doDisplayDirectoryContents(File dir, StringBuilder sb1) {
File[] files = dir.listFiles(onlyDirectories);
if (files != null) {
for (File file : files) {
try {
sb1.append("{\"JSONKEY\":\"" + file.getCanonicalPath() + "\"},");
doDisplayDirectoryContents(file, sb1);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public List<Object> getDirectoryContents(String path) throws IOException {
File directory = new File(path);
File[] files;
enter code here FileFilter fileFilter = file -> file.isDirectory() || file.isFile();
files = directory.listFiles(fileFilter);
List<Object> directoryContent = new ArrayList<>();
if(files != null) {
for (int i = 0; i < files.length; i++) {
File filename = files[i];
String folderPath[] =filename.toString().split("/");
if(files[i].isDirectory()) {
Folder folder = new Folder();
folder.setName(folderPath[folderPath.length - 1]);
folder.setType("folder");
folder.setChildren(mapper.readTree(mapper.writeValueAsString(getDirectoryContents(path + "/" + folder.getName()))));
directoryContent.add(folder);
}
else{
Files file = new Files();
file.setName(folderPath[folderPath.length - 1]);
file.setType("file");
directoryContent.add(file);
}
}
}
return directoryContent;
}
public class Files {
private String name;
private String type = "file";
}
public class Folder {
private String name;
private String type = "folder";
private JsonNode children;
}

delele all files with an extension in java

So I found some code earlier that looks like it would work but it doesn't call to delete the files just to list them. What do I need to add so that it deletes the files?
import java.io.File;
import java.util.regex.Pattern;
public class cleardir {
static String userprofile = System.getenv("USERPROFILE");
private static void walkDir(final File dir, final Pattern pattern) {
final File[] files = dir.listFiles();
if (files != null) {
for (final File file : files) {
if (file.isDirectory()) {
walkDir(file, pattern);
} else if (pattern.matcher(file.getName()).matches()) {
System.out.println("file to delete: " + file.getAbsolutePath());
} } } }
public static void main(String[] args) {
walkDir(new File(userprofile+"/Downloads/Software_Tokens"),
Pattern.compile(".*\\.sdtid"));
}
}
Once you have the path to the file, delete him:
File physicalFile = new File(path); // This is one of your file objects inside your for loop, since you already have them just delete them.
try {
physicalFile.delete(); //Returns true if the file was deleted or false otherwise.
//You might want to know this just in case you need to do some additional operations based on the outcome of the deletion.
} catch(SecurityException securityException) {
//TODO Handle.
//If you haven't got enough rights to access the file, this exception is thrown.
}
To delete a file you can call the delete function
file.delete();
You can invoke the delete() method on an instance of File. Be sure to check the returncode to make sure your file was actually deleted.
Use file.delete(); to delete a file.
You need to learn Java basics properly before attempting to write programs. Good resource: http://docs.oracle.com/javase/tutorial/index.html
Call File.delete() for each file you want to delete. So your code would be:
import java.io.File;
import java.util.regex.Pattern;
public class cleardir {
static String userprofile = System.getenv("USERPROFILE");
private static void walkDir(final File dir, final Pattern pattern) {
final File[] files = dir.listFiles();
if (files != null) {
for (final File file : files) {
if (file.isDirectory()) {
walkDir(file, pattern);
} else if (pattern.matcher(file.getName()).matches()) {
System.out.println("file to delete: " + file.getAbsolutePath());
boolean deleteSuccess=file.delete();
if(!deleteSuccess)System.err.println("[warning]: "+file.getAbsolutePath()+" was not deleted...");
}
}
}
}
public static void main(String[] args) {
walkDir(new File(userprofile+"/Downloads/Software_Tokens"),
Pattern.compile(".*\\.sdtid"));
}
}
final File folder = new File("C:/Temp");
FileFilter ff = new FileFilter() {
#Override
public boolean accept(File pathname) {
String ext = FilenameUtils.getExtension(pathname.getName());
return ext.equalsIgnoreCase("EXT"); //Your extension
}
};
final File[] files = folder.listFiles(ff);
for (final File file : files) {
file.delete();
}
public class cleardir {
static String userprofile = System.getenv("USERPROFILE");
private static final String FILE_DIR = userprofile+"\\Downloads\\Software_Tokens";
private static final String FILE_TEXT_EXT = ".sdtid";
public static void run(String args[]) {
new cleardir().deleteFile(FILE_DIR,FILE_TEXT_EXT);
}
public void deleteFile(String folder, String ext){
GenericExtFilter filter = new GenericExtFilter(ext);
File dir = new File(folder);
if (dir.exists()) {
//list out all the file name with .txt extension
String[] list = dir.list(filter);
if (list.length == 0) return;
File fileDelete;
for (String file : list){
String temp = new StringBuffer(FILE_DIR)
.append(File.separator)
.append(file).toString();
fileDelete = new File(temp);
boolean isdeleted = fileDelete.delete();
System.out.println("file : " + temp + " is deleted : " + isdeleted);
}
}
}
//inner class, generic extension filter
public class GenericExtFilter implements FilenameFilter {
private String ext;
public GenericExtFilter(String ext) {
this.ext = ext;
}
public boolean accept(File dir, String name) {
return (name.endsWith(ext));
}
}
}

Search for file in directory with multiple directories

Here's my goal. I want to be able to pass a parent directory and a filename to a method that searches for that specific file in the directory and any sub-directories. Below is the code I have been working with but can not get it to do exactly what I want. It will find the file I specify but will not return anything.
private static File findFile(File dir, String name) {
String file = "";
File[] dirlist = dir.listFiles();
search:
for(int i = 0; i < dirlist.length; i++) {
if(dirlist[i].isDirectory()) {
findFile(dirlist[i], name);
} else if(dirlist[i].getName().matches(name)) {
file = dirlist[i].toString();
break search;
}
}
return new File(file);
}
I know that when the method finds a directory and calls itself it resets the file variable which is where I am storing the found file. So that is why I am getting a blank return. I am not sure how to accomplish this goal or if it's even possible.
The problem is that you're not returning anything from the recursive call:
if(dirlist[i].isDirectory()) {
findFile(dirlist[i], name); // <-- here
} else if(dirlist[i].getName().matches(name)) {
I would do the following:
private static File findFile(File dir, String name) {
File result = null; // no need to store result as String, you're returning File anyway
File[] dirlist = dir.listFiles();
for(int i = 0; i < dirlist.length; i++) {
if(dirlist[i].isDirectory()) {
result = findFile(dirlist[i], name);
if (result!=null) break; // recursive call found the file; terminate the loop
} else if(dirlist[i].getName().matches(name)) {
return dirlist[i]; // found the file; return it
}
}
return result; // will return null if we didn't find anything
}
In fact there are many solutions to do the job.
I assume that you want to find a unique file (or the first one) found in a directory tree that matches with the fileName.
It is a problem of optimization because there are multiple ways to explore solutions, and we want to find an acceptable solution.
1- Solution using FileUtils.listFiles
public static File searchFileWithFileUtils(final File file, final String fileName) {
File target = null;
if(file.isDirectory()) {
Collection<File> files = FileUtils.listFiles(file, null, true);
for (File currFile : files) {
if (currFile.isFile() && currFile.getName().equals(fileName)) {
target = currFile;
break;
}
}
}
return target;
}
The solution using the library FileUtils is not a suitable solution because the method FileUtils#listFiles() loads all the directory/folder tree (the cost is expensive !).
We don't need to know all the tree, we can choose a better algorithm which stops when the file is found.
2- Recursive Solution
public static File searchFileRecursive(final File file, final String search) {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File f : files) {
File target = searchFileRecursive(f, search);
if(target != null) {
return target;
}
}
} else {
if (search.equals(file.getName())) {
return file;
}
}
return null;
}
The algorithm tests if the file exists inside any folder. If not, it tries subfolder of the current folder... recursively. If the file is not found in the current branch it tries another subfolder.
The exploration is deep, and for any file in a deepness of 1 the algorithm will explore the entirety of previous subfolders (previous branches are completely explored !).
This algorithm has the best performances for files in a deep location inside the first branch.
In the majority of cases, the file location is not deep, so let explore another algorithm that works in most of cases.
3- Fastest Solution : exploration by deepness
public static File searchFileByDeepness(final String directoryName, final String fileName) {
File target = null;
if(directoryName != null && fileName != null) {
File directory = new File(directoryName);
if(directory.isDirectory()) {
File file = new File(directoryName, fileName);
if(file.isFile()) {
target = file;
}
else {
List<File> subDirectories = getSubDirectories(directory);
do {
List<File> subSubDirectories = new ArrayList<File>();
for(File subDirectory : subDirectories) {
File fileInSubDirectory = new File(subDirectory, fileName);
if(fileInSubDirectory.isFile()) {
return fileInSubDirectory;
}
subSubDirectories.addAll(getSubDirectories(subDirectory));
}
subDirectories = subSubDirectories;
} while(subDirectories != null && ! subDirectories.isEmpty());
}
}
}
return target;
}
private static List<File> getSubDirectories(final File directory) {
File[] subDirectories = directory.listFiles(new FilenameFilter() {
#Override
public boolean accept(final File current, final String name) {
return new File(current, name).isDirectory();
}
});
return Arrays.asList(subDirectories);
}
For each deepness, the algorithm searches the file inside all folders of the same level. If the file is not found, it tries the next level (deepness++).
Due to the parallel exploration (symmetry), this solution is suitable in most of cases.
Comparison:
public class FileLocationFinder {
public static void main(final String[] args) {
String rootFolder = args[0];
String fileName = args[1];
long start = System.currentTimeMillis();
File target = searchFileWithFileUtils(new File(rootFolder), fileName);
System.out.println(target.getAbsolutePath());
System.out.println("Duration: " + (System.currentTimeMillis() - start) + "ms");
start = System.currentTimeMillis();
target = searchFileRecursive(new File(rootFolder), fileName);
System.out.println(target.getAbsolutePath());
System.out.println("Duration: " + (System.currentTimeMillis() - start) + "ms");
start = System.currentTimeMillis();
target = searchFileByDeepness(rootFolder, fileName);
System.out.println(target.getAbsolutePath());
System.out.println("Duration: " + (System.currentTimeMillis() - start) + "ms");
}
// Solution with FileUtils#listFiles
//--------------------------------------------
public static File searchFileWithFileUtils(final File file, final String fileName) {
File target = null;
if(file.isDirectory()) {
Collection<File> files = FileUtils.listFiles(file, null, true);
for (File currFile : files) {
if (currFile.isFile() && currFile.getName().equals(fileName)) {
target = currFile;
break;
}
}
}
return target;
}
// Recursive solution
//--------------------------------------------
public static File searchFileRecursive(final File file, final String search) {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File f : files) {
File target = searchFileRecursive(f, search);
if(target != null) {
return target;
}
}
} else {
if (search.equals(file.getName())) {
return file;
}
}
return null;
}
// Fastest solution
//--------------------------------------------
public static File searchFileByDeepness(final String directoryName, final String fileName) {
File target = null;
if(directoryName != null && fileName != null) {
File directory = new File(directoryName);
if(directory.isDirectory()) {
File file = new File(directoryName, fileName);
if(file.isFile()) {
target = file;
}
else {
List<File> subDirectories = getSubDirectories(directory);
do {
List<File> subSubDirectories = new ArrayList<File>();
for(File subDirectory : subDirectories) {
File fileInSubDirectory = new File(subDirectory, fileName);
if(fileInSubDirectory.isFile()) {
return fileInSubDirectory;
}
subSubDirectories.addAll(getSubDirectories(subDirectory));
}
subDirectories = subSubDirectories;
} while(subDirectories != null && ! subDirectories.isEmpty());
}
}
}
return target;
}
private static List<File> getSubDirectories(final File directory) {
File[] subDirectories = directory.listFiles(new FilenameFilter() {
#Override
public boolean accept(final File current, final String name) {
return new File(current, name).isDirectory();
}
});
return Arrays.asList(subDirectories);
}
}
Result:
searchFileWithFileUtils: 20186ms | searchFileRecursive: 1134ms | searchFileByDeepness: 16ms
[EDIT]
You can also use Java 8 Files API to do this job :
public static File searchFileJava8(final String rootFolder, final String fileName) {
File target = null;
Path root = Paths.get(rootFolder);
try (Stream<Path> stream = Files.find(root, Integer.MAX_VALUE, (path, attr) ->
path.getFileName().toString().equals(fileName))) {
Optional<Path> path = stream.findFirst();
if(path.isPresent()) {
target = path.get().toFile();
}
}
catch (IOException e) {
}
return target;
}
But the execution time is not better (994ms).

Categories

Resources