I am new to Java and know the basics by now. I have a csv file which lines are all of the following structure:
Int,,text,text,Int,text,text,text,,text,text,,text,text,,,text,,text,,,Int,Int
I was very confused when I saw that csv file since it is separated by single commas, double commas and triple commas. Sometimes a specific text or int is also empty and excel can´t handle to display the csv in the correct way any more.
So I thought I use Java to write a program to make the columns separated by only one comma. And save the result in a new csv file afterwards. (I haven´t implemented how to write it in another file) With some research I managed to write a File Reader to read the csv file but that´s it. How can I come to my desired result?
What I have done so far:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
class Read {
public static void main(String[] args) {
FileReader myFile = null;
BufferedReader buff = null;
final ArrayList<String> lines = new ArrayList<String>();
try {
myFile = new FileReader("thisisthepathofthecsvsource");
buff = new BufferedReader(myFile);
String line;
while ((line = buff.readLine()) != null) {
lines.add(line);
}
} catch (IOException e) {
System.err.println("Error2 :" + e);
} finally {
try {
buff.close();
myFile.close();
} catch (IOException e) {
System.err.println("Error2 :" + e);
}
}
final String[][] valuesArray = new String[lines.size()][];
int cnt = 0;
for (final String line : lines) {
valuesArray[cnt++] = line.split(",");
}
for (String[] arr : valuesArray) {
System.out.println(Arrays.toString(arr));
}
}
}