-
Apr 28th, 2008, 09:39 PM
#1
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?
Thanks,
Navaketan
-
Apr 28th, 2008, 09:48 PM
#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?
-
Apr 28th, 2008, 10:27 PM
#3
Only one file can be dropped into the directory.
-
Apr 28th, 2008, 11:00 PM
#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.
-
Apr 29th, 2008, 10:24 AM
#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?
-
Apr 29th, 2008, 12:21 PM
#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?
-
May 1st, 2008, 12:18 AM
#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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules