Announcement Announcement Module
No announcement yet.
How to create FlatFileItemReader? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to create FlatFileItemReader?

    I have a requirement to process flat file from a directory in the classpath. I do not know the name of the file. Can anyone guide me what can I do in this situation to create the FlatFileItemreader?


  • #2
    You don't know the name of the file? Is it assumed there is only one file in the directory, or some other way of eventually figuring it out?


    • #3
      Only one file can be dropped into the directory.


      • #4
        You'll have to proxy the Resource you pass the FlatFileItemReader. The reader simply calls getFile(), which is all you'll really need to deal with in your proxy (you could easily use AOP to do this) Then it's as simple as calling file.listFiles() and grabbing what should be the only entry in the returned array. That's at least the only way I can think to do it.


        • #5
          Since I do not know AOP that well, I tried to achieve it differently.

          I created my own ClassPathResource by extending the ClassPathResource. In the getFile method of MyClassPathResource I used the logic of returning the first element from file.listFiles(). Things started working. But unfortunately I found that the FieldSet is coming empty to my FieldSetmapper object. Any idea what I can do to get past it?


          • #6
            That's a completely different issue. If you're even getting a FieldSet at all you were able to open the file correctly. I'm not sure how it could come back empty unless either there's an issue with the file you're using, or the tokenizer was misconfigured. Can you post the file itself and how you configured the reader?


            • #7
              Finally I made it to work.

              Whenever extending the ClasssPathResource, besides overriding the getFile(), getInputStream() needs to be overridden also. It is little ambiguous. If the getInputStream() is not overridden then FieldSet gets populated in with the name of the file. There will be no data in it. It could be a bug in the ClasssPathResource.getInputStream(). I am using spring-batch-1.0.0.m5 version.

              In any case, one solution of addressing the requirement I have mentioned above is to extend the ClassPathResource and override getFile() and getInputStream().

              Thanks a lot for all the help and guidance.