Hi,
Can anybody send me the java code to do following:
- A directory containing sub-folders with different name and date
- To copy the folder having the most recent date from the directory.
Thanks in advance.
Ayon
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hi,
Can anybody send me the java code to do following:
- A directory containing sub-folders with different name and date
- To copy the folder having the most recent date from the directory.
Thanks in advance.
Ayon
Please show us your thoughts/code attempts to solve the problem and we will help you
You will probably want to use the File Class.
Well , I tried using File class, which has a method called file.lastmodified(), this returns long value of the time.
Using this I am not able to sort the latest folder created. Can you please suggest me , how to sort based on long value?
The long value returned is the number of milliseconds from a pre-determined time (I believe it's Jan. 1, 1960, but don't quote me on this, and it doesn't really matter for sorting). So, the most recently modified file is the file that has the larges long value returned, and the oldest has the smallest value.
If you need to convert this long value to a Date object, I think there's a method inside the Date class, or possibly one of the Calendar classes (the Java Date/Time representation is a little lacking here)
Basically use the File class and create an instance which points to your base directory.
Use file.listFiles() get all the files in the base directory.
Use a comparator to sort the array by lastModified.
Grab the newest file and add it to your list.
Get all directories in the base directory and do the same for each directory.
As a reply to your question on sorting this is what you could do.
Arrays.sort(files, new Comparator<File>() { @Override public int compare(File o1, File o2) { return (int) (o2.lastModified() - o1.lastModified()); } });
Assuming files is an array of File objects. This would put the last edited file/directory at index zero.
// Json
Somthing like?
private static File getRecent(String directory) { File file = null; for(String x : new File(directory).list()) { File inQuestion = new File(directory + x); if(file == null) { file = inQuestion; continue; } if(inQuestion.lastModified() < file.lastModified()) { file = inQuestion; } } return file; }
It returns the most recently created file in the directory,
Hello Time, would that not also return you a directory if the latest modified file in the directory was actually another directory?
// Json
or if(inQuestion.isFile()) perhaps
// Json
Thanks , I have tried this and somehow gotthis working.
Thanks again for your help