I'm going to do a Java library on a simple data frame that can read CSV files, edit the CSV and export CSV file. My problem is on how to export it.
Here is how I read my CSV file:
String line;
List dataFrame = new ArrayList();
String filePath = "C:\\Users\\user\\Desktop\\SimpleDataFrame\\src\\Book1.csv";
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
while ((line = br.readLine()) != null) {
List values = Arrays.asList(line.split(String.valueOf(",")));
dataFrame.add(values);
}
} catch (Exception e) {
System.out.println(e);
}
And this is how I implement the write CSV file:
FileWriter writer = new FileWriter(new File("Book2.csv"));
for(int i = 0; i<dataFrame.size(); i++){
String[] array = (String [])dataFrame.get(i);
for(int j = 0; j<array.length; j++){
writer.write(array[j]);
if(j<array.length-1) writer.write(",");
else writer.write("\n");
}
}
And this is the exception that it throws to me:
Exception in thread "main" java.lang.ClassCastException: class java.util.Arrays$ArrayList cannot be cast to class [Ljava.lang.String; (java.util.Arrays$ArrayList and [Ljava.lang.String; are in module java.base of loader 'bootstrap')
Can I know what is the problem?
>`
– Hulk Dec 03 '20 at 15:00