• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading a file thourgh a pattern [date at the end]

 
Murali Sankar Selvaraj
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I've a program to read a file with full qualified path. But the actual file will be different name each day (As file name contains date format). How to read the file?



Example File Name:
ABC12Dec2014_1
ABC13Dec2014_2

File Name format: ABC [Standard identifier] + Date format + "_" + sequence number

Thanks for your suggestion.
 
Tony Docherty
Saloon Keeper
Posts: 3138
72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

If the file name changes for each day then you need to construct the filename by concatenating the fixed part (ie ABC) with the date and the sequence number. The date part is easy, you can set up a DateFormatter to output todays date in the format specified for the file name. The sequence number is a little trickier and how you go about this depends on things such as whether or not you can have multiple files for the same day or some days without files etc. If you can't calculate what the next sequence number is then you will need to look at every file in the directory (see java.io.File.listFiles(FilenameFilter filter))and see if the name starts with ABC+today date and then if there are multiple file get all the files or maybe just find the file with the highest sequence number.

An alternative approach if you can guarantee your application is running when the file is created is you watch the directory for a file being created and take action on receiving notification see https://docs.oracle.com/javase/tutorial/essential/io/notification.html
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!