I want to combine columns in csv file using java
here in this file i want combine first two columns "Product No" and "Product Name".
This is My CSV File
Productno,Productname,Price,Quantity
1,java,300,5
2,java2,500,10
3,java3,1100,120
Here is My Code
private void parseUsingOpenCSV(String filename)
{
CSVReader reader;
FileWriter out = null;
CSVWriter outt;
try
{
reader = new CSVReader(new FileReader(filename));
String[] row;
try {
out= new FileWriter("E:/data/test/newww.csv");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
while ((row = reader.readNext()) != null)
{
for (int i = 0; i < row.length; i++)
{
// display CSV values
System.out.println(row[i]);
String com = row[i];
out.write(com);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
catch (FileNotFoundException e)
{
System.err.println(e.getMessage());
}finally{
if (out != null) {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
By using this code i get output is below
Product No
Product Name
Price
1
java
300
2
java2
500
3
java3
1100
But I want Output like this....
ProductnoProductname,Price,Quantity
1java,300,5
2java2,500,10
3java3,1100,120
解决方案
while ((row = reader.readNext()) != null)
{
for (int i = 0; i < row.length; i++)
{
// display CSV values
System.out.print(row[i]);
String com = row[i];
out.write(com);
}
System.out.println();
}
this breaks the output only after ove SV line is writen