Click here to Skip to main content
15,936,565 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a folder with different files with different dates i need to get if one hour file is there or not,need to display file name
after that 1 day file need to displayed

after 1 week file name should be displayed ..

31-05-2018 folder inside filename is like these : 31-05-2018_10-42-25_rakesh_hike_load_608_output.txt if i need to enter 1 day details i need to display that file

What I have tried:

<pre>package hellomongo;

import java.util.Date;

public class Timedate {

	 public static void main(String[] args)
	  String path = "/home/msr/hike";

	  String files;
	  File folder = new File(path);
	  File[] listOfFiles = folder.listFiles();

	 for (int i = 0; i < listOfFiles.length; i++)
		  System.out.println(listOfFiles[i].getName()+"\t"+new Date(listOfFiles[i].lastModified()));

Updated 31-May-18 1:02am
Richard MacCutchan 31-May-18 6:53am    
What is the question?

1 solution

Use a FilenameFilter (Java Platform SE 7 )[^] and pass that to listFiles():
FilenameFilter fileNameFilter = new FilenameFilter() {
    public boolean accept(File dir, String name) {
        // Hourly files
        String match = String.format("%02d-%02d-%04d_%02d", day, month, year, hour);
        // Use this for daily files
        //String match = String.format("%02d-%02d-%04d", day, month, year);
        // When having a Date or Calendar use (without "_%1$tH" for daily)
        //String match = String.format("%1$td-%1$tm-%1$tY_%1$tH", date);
        return name.startsWith(match);
File[] listOfFiles = folder.listFiles(fileNameFilter);
The above will work for hourly and daily files. Weekly files is more complicated and requires for example creating seven day match strings (as array) and comparing against all. Then using the Calendar (Java Platform SE 7 )[^] type and incrementing that in the loop is easier than doing the date calulation manually (the Java Date type has no add method while the Calendar type has).
Share this answer
Member 13809409 31-May-18 8:55am    
shall i need to declare variables day,month,year,hour?
Jochen Arndt 31-May-18 9:01am    
If you are going to use them. You have not told us how the date to be looked for is specified.

"how to read files there"
Probably by getting each name from the listOfFiles[] array and opening them?
It depends on what are going to do with the files.
Member 13809409 31-May-18 9:02am    
day, month, year, hour these variables getting error
Jochen Arndt 31-May-18 9:06am    
It is example code showing you how to do it in general.

But you have to specify first for which day, hour, or week you want to get the file names and put that information into some variables. That might be multiple variables for each part or a Data or Calendard.
Member 13809409 31-May-18 9:11am    
am not able to do still getting errors

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900