Announcement Announcement Module
Collapse
No announcement yet.
File Handling in Spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • File Handling in Spring

    Hi All,

    I am new to spring. I have developed a small app, which is reading files from source, printing the number of lines and copy them to destination folder. It works good. But Now I want to extend it to further to recognize different filenames, read the files, print the total number of lines and copy them to their respective destination folders.

    Any suggestion on the best way to achieve this task.

    Regards,
    Mahendra Singh

  • #2
    But Now I want to extend it to further to recognize different filenames, read the files, print the total number of lines and copy them to their respective destination folders.
    Do you mean based on the format of the file name and the extension (and not the contents within) you want to process and send them to different output directories?

    Are the possible file name patterns small enough? If so, can defining different inbound adapters with the pattern or regex to match your file names address your problem?

    Comment


    • #3
      Do you mean based on the format of the file name and the extension (and not the contents within) you want to process and send them to different output directories?
      yes. I have four different files, like stated below:


      1) abc.csv
      2) rdf.xls
      3) bnn.txt
      4) mut.csv

      I have ysed the below code for single file(message.csv):
      Code:
      <file:inbound-channel-adapter id="mfInputFileProcessor"
      		directory="file:${input.directory}" filename-pattern="message*">
      		<poller id="mfInputPoller" fixed-delay="10" time-unit="SECONDS" />
      	</file:inbound-channel-adapter>
      		
      	<file:file-to-string-transformer
      		input-channel="mfInputFileProcessor" output-channel="mfInputFileParser"
      		charset="UTF-8" delete-files="false" />
      
      	<splitter input-channel="mfInputFileParser" ref="mFileHandler"
      		method="extractLines" output-channel="message.inbound" />
      
      	<int:service-activator input-channel="mfInputFileProcessor" output-channel="filesOut" ref="mFileHandler" method="handleFile"/>
      	
      	<file:outbound-channel-adapter id="filesOut"
                   directory="${output.directory}"
                   auto-create-directory="true"
                   delete-source-files="false" charset="UTF-8"/>
      Do i need to use the above code 4 times, if I would like it to process 4 different file?

      Regards,
      Mahendra Singh

      Comment


      • #4
        I would model this in the following way.

        Since i have four different types of files, i would do the following

        1. have four inbound file adapters to read files and send to different output channels.
        2. Have an header enricher on each of the output channels to add some kind of identifier header in the messages and send them to one common channel.
        3. The logic here is common i am assuming for all the types of files, so i will have this processing component common, which will receive messages from this common channel, process them and put the messages on a common output channel.
        4. Have a router which routes the messages to different channels based on the header values of the input message.
        5. Have outbound adapters for each possible output channel of the router (same as the possible number of file formats/names) which will write the files to some specific directory.

        Copying this above config four times (with different channels for each path) will also achieve the result, but that would duplicate the processing logic in the config file.
        If that logic is common, the approach i suggested above would suit better.

        Comment


        • #5
          Hold on, i saw an .xls file in the list you gave. Are you sure you want to convert that to string?

          Comment


          • #6
            Yes that is also needed.

            Comment

            Working...
            X