Skip to Content

“JAVA MAPPING”, an alternate way of reading a CSV file

If you want to read a CSV file using the file adapter, file content conversion is the first option which strikes your mind.

If you are comfortable with java coding, here i present you with an alternate solution.

I will show you how to use java mapping to achieve the same.

Below is the content in the csv file which i want to read.





‘1’ is used to identify the header level record and ‘2’ the item level.

Now you have to write the java mapping program which will read this data and create a target xml structure

package TXTMapping;







import java.util.Map;


public class TMapping implements StreamTransformation {

private Map map;


public void setParameter (Map param){

      map = param;


public void execute (InputStream in, OutputStream out){


    out.write(“”Name“”Company“”Place“”Desgn“”Since“.xls or .pdf or .txt) and do the conversion in the mapping program.

You must be Logged on to comment or reply to a post.
  • this very example is part of almost any course of 'Java for Absolute Beginners' and done much more elegant there, e.g. using a method

    insertNode(nodename, nodecontent)



  • Well, I would recommend not to use that kind of code in teaching XML.

    In particular, it's a very bad idea to first claim that the output is UTF-8, and then to use String.getBytes() to produce it (because that one will use whatever the system encoding happens to be).

    Best regards, Julian

    • Julian...
      You are correct..i am not a java developer and the code written is just a sample example...i just wanted to highlight that it can be done using java mapping

      Rahul Nawale

  • hi Rahul,

    why do you want to read it in a mapping?
    you can do it via adapter module (in file adapter)
    or via java proxy if you really need to use
    java to read a flat file

    why did you choose to do it in a mapping?


    • yes michal...we can do it using adapter module and java proxies, no doubt about it. Creating a java class is simpler for the people who dont know ejb development.
  • Hi rahul Good Work that, but i am not sure why you want to use Java Maps for reading the files, Adapter modules could be doing this No??

    Any way good simple explanatory Blog.., Good attempt,

    I had a same case with receiver adapter needed to send the CSV file i used Java Code and attached that to the module.

    Any way


  • Hi Rahul,

    I dont think that it is a good idea to go for java mapping for just reading a file, because, u have taken a very simple file structure to do this. If the structure becomes complex, then Content conversion is easier than this (if u want to do mapping after that).

    Also, if v need to change the target structure, then v need to another mapping for the same which will definelty affect the performance of the scenario.

    Prasad U

  • Hi Rahul,

    Since i can see many explaining that content conversion can be done in the channel instead of in the mapping, you can also mention, for a better clarity, in your blog, that in cases where HTTP (or any channel for that matter where content conversion is not possible) is the sender channel and the data from the sender application is 'comma delimited' then we will be left with no choice than to do the content conversion using a java mapping as shown in your blog.

    And also cant we use a string tokenizer in your code, is there any particular reason you hav chosen a char array.. jst curious 🙂

  • Hello Rahul,

    First of all thanks a zillion for this wonderful blog. We are trying to follow your instructions to read from a binary file. I quote a text from your post "Compile the above java mapping code. Zip the class file which is created and import it in the imported archive. Now while doing the interface mapping, specify the type of the mapping program as java class and the imported archieve as the mapping program."
    In case of interface mapping we need a source xml structure and a target xml structure. The target xml structure is being generated by your java mapping code, but how and what to specify in the source xml structure. Since we are not using FCC,how are we going to get the source xml structure.Could please kindly help us in this regard.

    regards Anupam