• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

how to select file extension

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

I want to write a class for the following scenario:
I have hundreds of files with different extensions(ex: pdf,txt,csv,xml etc)
I want to read the file and parse it based on its extension(type) & put it in a DB.

But a general If-Else / switch approach will be not the optimum solution.

Can any one help me if there is a pattern for this sort of thing..
* I have tried putting the extension & action in a file as pairs and then accessing them

Thanks
 
Marshal
Posts: 80647
473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Please check the Java Ranch naming policy which required first name-space-last name and adjust your name accordingly.

There is an interface called FileFilter or something similar which can probably help sort out your extension problem.
 
sanjay kumar
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell,

Have sorted the naming issue.

I dont think you are getting the problem I'm facing.

I have 500 (for example) files all with different extensions.
Now to associate with an action corresponding to the File Parsing if-else or switch is not a good solutions as i will have to generate/write as many if blocks. So is the case of a filter I believe?

Thanks
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by frank:

I have tried putting the extension & action in a file as pairs and then accessing them



Bang on. This is a good approach, provided you read the file once at the application start and put it in memory after that.
I also hope that you have an interface that defines the contract for processing a kind of extension.
If you are doing the above, you are on track and in some way following the Factory method design pattern
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic