Created
April 25, 2016 17:39
-
-
Save ckob/61644f7ce1fd7f9e9b7171f292bad0ce to your computer and use it in GitHub Desktop.
Lector de csv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private char possibleDelimitadorCampsEntrada = '\"'; | |
private char separadorEntrada = ','; | |
/** | |
* | |
* @param fila un string separat amb estil csv | |
* @return array de Strings amb un valor per cada camp separat correctament. | |
*/ | |
public String[] filaCsvToArrString(String fila) { | |
ArrayList<String> strings = new ArrayList<>(); | |
String str = ""; | |
boolean entreCometes = false; | |
for (int i = 0; i < fila.length(); i++) { | |
if (fila.charAt(i) == possibleDelimitadorCampsEntrada) { | |
if (i + 1 < fila.length()) { | |
if ((fila.charAt(i + 1) == possibleDelimitadorCampsEntrada)) { // Escapar cometes | |
str += fila.charAt(i); // Afegeixo unes cometes de les dues que hi han. | |
i++; // i ja no cal mirar les següents | |
} else { | |
entreCometes = !entreCometes; | |
} | |
continue; | |
} else if (i==fila.length()-1) { // evitar l'ultim possible delimitador de la linia | |
break; | |
} | |
} | |
if (fila.charAt(i) == separadorEntrada && !entreCometes) { | |
strings.add(str); | |
str = ""; | |
} else { | |
str += fila.charAt(i); | |
} | |
} | |
strings.add(str); // ultim camp | |
return strings.toArray(new String[strings.size()]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment