Skip to content

Instantly share code, notes, and snippets.

@elarif
Last active September 5, 2016 22:32
Show Gist options
  • Save elarif/46062d5d51214fd00bf617b93f35f791 to your computer and use it in GitHub Desktop.
Save elarif/46062d5d51214fd00bf617b93f35f791 to your computer and use it in GitHub Desktop.
parse csv java
package test;
import java.util.List;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
public class CSVLine
{
public final String number;
public final List<String> liste;
public final String valeur;
public static Builder builder(){
return new Builder();
}
private CSVLine(Builder builder) {
this.number = builder.number;
this.liste = builder.liste;
this.valeur = builder.valeur;
}
public static class Builder{
private String number;
private List<String> liste = ImmutableList.of();
private String valeur;
public Builder withNumber(String number) {
this.number = number;
return this;
}
public Builder withListe(List<String> liste) {
this.liste = ImmutableList.copyOf(liste);
return this;
}
public Builder addToListe(String s) {
liste = ImmutableList.<String>builder().addAll(liste).add(s).build();
return this;
}
public Builder withValeur(String valeur) {
this.valeur = valeur;
return this;
}
public CSVLine build() {
return new CSVLine(this);
}
}
@Override
public String toString() {
return MoreObjects.toStringHelper( getClass() )
.add( "number", number )
.add( "liste", liste )
.add( "value", valeur)
.toString();
}
}
package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.List;
import test.CSVLine.Builder;
import com.google.common.collect.ImmutableList;
/**
* Hello world!
*
*/
public class App {
private static final String DEFAULT_SEPARATOR = ";";
private static final String csvString =
"63; Rule number: 63 [conversions=23.6666666666667 cover=18 (0%)];23,7\r\n" +
"63; city_paris=1;23,7\r\n" +
"63; browser_Mobile Safari=1;23,7\r\n" +
"63; day_7=0;23,7\r\n" +
"63; day_6=0;23,7\r\n" +
"63; day_1=1;23,7\r\n" +
"63;;23,7\r\n" +
"55; Rule number: 55 [conversions=22.9444444444444 cover=18 (0%)];22,9\r\n" +
"55; city_paris=1;22,9\r\n" +
"55; browser_Mobile Safari=0;22,9\r\n" +
"55; browser_Chrome Mobile=1;22,9\r\n" +
"55; day_7=0;22,9\r\n" +
"55; day_2=1;22,9\r\n" +
"55;;22,9\r\n" +
"251; Rule number: 251 [conversions=20.3529411764706 cover=17 (0%)];20,4\r\n" +
"251; city_paris=1;20,4\r\n" +
"251; browser_Mobile Safari=1;20,4\r\n" +
"251; day_7=0;20,4\r\n" +
"251; day_6=0;20,4\r\n" +
"251; day_1=0;20,4\r\n" +
"251; day_5=0;20,4\r\n" +
"251; day_4=1;20,4\r\n" +
"251;;20,4";
public static void main(String[] args) throws Exception {
List<CSVLine> csvLines = parseCsv();
for (CSVLine csvLine : csvLines){
System.out.println(csvLine);
}
}
private static List<CSVLine> parseCsv() {
ImmutableList.Builder<CSVLine> csvLines = ImmutableList.<CSVLine>builder();
try (BufferedReader br = new BufferedReader(new StringReader(csvString))) {
String line = br.readLine();
if(line != null){
String values[] = line.split(DEFAULT_SEPARATOR);
String number = values[0];
Builder builder = CSVLine.builder()
.withNumber(number)
.addToListe(values[1])
.withValeur(values[2]);
while ((line = br.readLine()) != null) {
String _values[] = line.split(DEFAULT_SEPARATOR);
String _number = _values[0];
if(_number.equals(number)){
builder.addToListe(_values[1]);
}else{
csvLines.add(builder.build());
number = _values[0];
builder = CSVLine.builder()
.withNumber(_number)
.addToListe(_values[1])
.withValeur(_values[2]);
}
}
csvLines.add(builder.build());
}
} catch (IOException e) {
e.printStackTrace();
}
return csvLines.build();
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment