Reputation: 93
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));
}
}
}
Upvotes: 2
Views: 2440
Reputation: 121
Try the open source library uniVocity-parsers, which provides the solution of columns separator as following:
CsvParserSettings settings = new CsvParserSettings();
settings.setSkipEmptyLines(true);
settings.getFormat().setLineSeparator("\n");
settings.getFormat().setQuote(',');
settings.getFormat().setQuoteEscape('\\'); // escape the double backslash
Upvotes: 1
Reputation: 2863
You want to replace one or more commas with one, so why not use a regex replace instead?
String fileContent = "file,content,,test";
fileContent = fileContent.replaceAll(",+", ",");
This will replace one or more comma with one comma and therefor should remove all duplicates.
Upvotes: 0
Reputation: 1009
you can do that in your while
String [] dataArr = line.split(",") ;
for(String str : dataArr){
if(str == null || str.equlas("")) continue;
System.out.println(str) ;
}
this will help you to get the comma separator file data.
Upvotes: 0