file conversion using beanIO

699 views Asked by At

I am converting csv file to other .xx file using BeanIO. my question is if I have csv file like

  abc,def,ghi,jkl,mno

  pqr,stu,vwx,yzi,sdp

my .xx file I want is to remove ,(comma) and put '$' instead

  abc$def$ghi$jkl$mno

  pqr$stu$vwx$yzi$sdp
2

There are 2 answers

0
PSo On

Is using beanIO a must? You can simple use BufferedReader and Writer to do the task.

BufferedReader br = new BufferedReader(new FileReader(csvFile));
List<String> newLines = new ArrayList<String>();
while ((line = br.readLine()) != null) {
    String newline = line.replace(',', '$');
    newLines.add(newline);
}
br.close();

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(csvFile));
for (int i = 0; i < newLines.size(); i++) {
    bw.write(newLines.get(i));
    bw.newLine();
}
bw.close();

Using beanIO suppose to have a config maybe xml or annotation to map a batch of properties to your Java entity/class, and perform business logic afterwards, but not replacing any character.

To replace character, there are others library such as POI, but native component such as Scanner already can done that for you.

For you information, java provides: FileOutputStream, FileWriter, PrintWriter, OutputStreamWriter, BufferedWriter for I/O manipulation.

0
Michael Brohl On

Yes, you can specify the delimiter for your file like this:

<stream name="mydata" format="csv">
    <parser>
        <property name="delimiter" value="$" />
    </parser>
    ...
</stream>