• 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:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

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: 73285
332
  • 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
 
Creativity is allowing yourself to make mistakes; art is knowing which ones to keep. Keep this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic