This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Java in General and the fly likes How to check whether name of the File is in a Specifi format?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to check whether name of the File is in a Specifi format??" Watch "How to check whether name of the File is in a Specifi format??" New topic
Author

How to check whether name of the File is in a Specifi format??

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

I am expecting the Filename to be in a specified format that is "Test_INT.NO1.<dd><MM><yy><HH><mm><ss>.txt"

for example

"Test_INT.NO1. 28 06 10 12 25 56.txt"

But Please let me know how do i check the File name has got a specific format or not ??

Please share your ideas on this .

Thanks in advance .


Save India From Corruption - Anna Hazare.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Use a regular expression?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I like Paul's answer.
Raj S Kumar
Ranch Hand

Joined: Aug 06, 2006
Posts: 48
Filenames retrieved will be in the form of String. In my opinion, identifying the time format accurately is not always possible.

The only way I see is, to validate every parameter for its limits. such as 31 for <dd>, 12 for <MM> etc. This validation technique could help you to identify some parameters and factor the rest.


Raj S Kumar
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19685
    
  20

I'd prefer to use a DateFormat to check if the date is valid, rather than manually checking bounds. Using a DateFormat and its parse(String, ParsePosition) method you can even prevent any ParseExceptions:
That parse method returns null if the input is invalid. The other check also makes sure that the parsing consumes the entire input; parse methods usually allow trailing data; for instance, with "Test_INT.NO1. 28 06 10 12 25 56.txtabc" as input, the parse position's index will point at the a, allowing you to parse some more. Since we don't want to parse more or allow this trailing data the check ensures that the format will consume the entire text.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to check whether name of the File is in a Specifi format??