Created
April 28, 2016 15:21
-
-
Save ckob/8ee3799ea9efe8f56974744cc096429b to your computer and use it in GitHub Desktop.
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
import java.io.BufferedReader; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.Comparator; | |
public class CSVUtils { | |
// TODO: 28/04/16 toString, print, toHTML? | |
private static double toDouble(String text) { | |
String millaresSeparator = "\\."; | |
char decimalSeparator = ','; | |
text = text.replaceAll(millaresSeparator, ""); | |
return Double.parseDouble(text.replace(decimalSeparator, '.')); | |
} | |
private static int toInt(String text) { | |
String millaresSeparator = "\\."; | |
return Integer.parseInt(text.replaceAll(millaresSeparator, "")); | |
} | |
/** | |
* @param content arraylist with the content | |
* @param camp camp to order by | |
* @param asc specify if order ascendent (true) or descendent (false) | |
* @param type type of data | |
* @return true if the order worked | |
*/ | |
public static boolean orderArr(ArrayList<String[]> content, final int camp, final boolean asc, final DATE_TYPE type) { // Ordena la taula a partir de la fila donada (string numeric) | |
Collections.sort(content, new Comparator<String[]>() { | |
@Override | |
public int compare(String[] str1, String[] str2) { | |
if (str1[camp].isEmpty())// || str1[camp].equals("\\N")) | |
return asc ? -1 : 1; | |
if (str2[camp].isEmpty()) //|| str2[camp].equals("\\N")) | |
return asc ? 1 : -1; | |
if (type == DATE_TYPE.NUMERIC) { | |
if (asc) | |
return (int) (toDouble(str1[camp]) - toDouble(str2[camp])); // per numeric | |
else | |
return (int) (toDouble(str2[camp]) - toDouble(str1[camp])); | |
} else {// if (tipusCamps[camp] == TIPUS_DADA.TEXT) | |
if (asc) | |
return str1[camp].compareTo(str2[camp]); // per Strings | |
else | |
return str2[camp].compareTo(str1[camp]); // per Strings | |
} | |
} | |
}); | |
return true; | |
} | |
/** | |
* @param content | |
* @param fromColumn | |
* @param toColumn | |
* @return | |
*/ | |
public static ArrayList<String[]> getColumnsBetween(ArrayList<String[]> content, int fromColumn, int toColumn) { | |
ArrayList<String[]> newContent = new ArrayList<>(content.size()); | |
int nColumns = toColumn - fromColumn; | |
for (String[] line : content) { | |
String[] newLine = new String[nColumns]; | |
// int i =0; | |
// for (int column : columns) { | |
// newLine[i] = line[column]; | |
// i++; | |
// } | |
int j = 0; | |
for (int i = fromColumn; i < toColumn; i++) { | |
newLine[j] = line[i]; | |
j++; | |
} | |
newContent.add(newLine); | |
} | |
return newContent; | |
} | |
/** | |
* @param content | |
* @param columns starting with 0, columns to get in a new arraylist | |
* @return | |
*/ | |
public static ArrayList<String[]> getColumns(ArrayList<String[]> content, int... columns) { | |
ArrayList<String[]> newContent = new ArrayList<>(content.size()); | |
for (String[] line : content) { | |
String[] newLine = new String[columns.length]; | |
int i = 0; | |
for (int column : columns) { | |
newLine[i] = line[column]; | |
i++; | |
} | |
newContent.add(newLine); | |
} | |
return newContent; | |
} | |
/** | |
* @param entryFile file of the content | |
* @return arraylist with the content of the file | |
*/ | |
public static ArrayList<String[]> fileCsvToArrayList(String entryFile) { | |
ArrayList<String[]> fileContent = new ArrayList<>(); | |
BufferedReader br = null; | |
try { | |
br = new BufferedReader(new FileReader(entryFile)); | |
String actualLine; | |
while ((actualLine = br.readLine()) != null) { | |
fileContent.add(liniaCsvToArrString(actualLine, '"', ',')); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} finally { | |
try { | |
if (br != null) | |
br.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
return fileContent; | |
} | |
/** | |
* Get the content of a file, from line number to the end of the file | |
* | |
* @param entryFile file of the content | |
* @return arraylist with the content of the file | |
*/ | |
public static ArrayList<String[]> fileCsvToArrayList(String entryFile, int startLine) { | |
ArrayList<String[]> fileContent = new ArrayList<>(); | |
BufferedReader br = null; | |
try { | |
br = new BufferedReader(new FileReader(entryFile)); | |
String actualLine; | |
for (int i = 0; i < startLine; i++) { | |
br.readLine(); | |
} | |
while ((actualLine = br.readLine()) != null) { | |
fileContent.add(liniaCsvToArrString(actualLine, '"', ',')); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} finally { | |
try { | |
if (br != null) | |
br.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
return fileContent; | |
} | |
/** | |
* Get the content of a file, from line number to line number (included both lines) | |
* | |
* @param entryFile file of the content | |
* @return arraylist with the content of the file | |
*/ | |
public static ArrayList<String[]> fileCsvToArrayList(String entryFile, int startLine, int endLine) { | |
ArrayList<String[]> fileContent = new ArrayList<>(); | |
BufferedReader br = null; | |
try { | |
br = new BufferedReader(new FileReader(entryFile)); | |
String actualLine; | |
int i = 0; | |
for (; i < startLine; i++) { | |
br.readLine(); | |
} | |
while ((actualLine = br.readLine()) != null && i <= endLine) { | |
fileContent.add(liniaCsvToArrString(actualLine, '"', ',')); | |
i++; | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} finally { | |
try { | |
if (br != null) | |
br.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
return fileContent; | |
} | |
/** | |
* @param text un string separat amb estil csv | |
* @return array de Strings amb un valor per cada camp separat correctament. | |
*/ | |
public static String[] liniaCsvToArrString(String text, char possibleDelimitadorCamps, char separadorCamps) { | |
ArrayList<String> strings = new ArrayList<>(); | |
String str = ""; | |
boolean entreCometes = false; | |
for (int i = 0; i < text.length(); i++) { | |
if (text.charAt(i) == possibleDelimitadorCamps) { | |
if (i + 1 < text.length()) { | |
if ((text.charAt(i + 1) == possibleDelimitadorCamps)) { // Escapar cometes | |
str += text.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 == text.length() - 1) { // evitar l'ultim possible delimitador de la linia | |
break; | |
} | |
} | |
if (text.charAt(i) == separadorCamps && !entreCometes) { | |
strings.add(str); | |
str = ""; | |
} else { | |
str += text.charAt(i); | |
} | |
} | |
strings.add(str); // ultim camp | |
return strings.toArray(new String[strings.size()]); | |
} | |
public static boolean print(String[] fieldTitles, ArrayList<String[]> content, DATE_TYPE[] date_types) { | |
if (!content.isEmpty()) { | |
// Get max length of fields of the field titles | |
int[] maxLengthFields = new int[fieldTitles.length]; | |
for (int i = 0; i < maxLengthFields.length; i++) { | |
maxLengthFields[i] = fieldTitles[i].length(); | |
} | |
// get the max length of the content | |
for (String[] line : content) { | |
for (int i = 0; i < line.length; i++) { | |
if (line[i].length() > maxLengthFields[i]) { | |
maxLengthFields[i] = line[i].length(); | |
} | |
} | |
} | |
//A separator line to separe the rows | |
String separatorLine = ""; | |
for (int i = 0; i < maxLengthFields.length; i++) { | |
separatorLine += "+"; | |
if (maxLengthFields[i] > 0) { | |
for (int j = 0; j < maxLengthFields[i]; j++) { | |
separatorLine += "-"; | |
} | |
} | |
} | |
separatorLine += "+"; | |
System.out.println(separatorLine); | |
return true; | |
} else { | |
// There's no content to show | |
return false; | |
} | |
} | |
// public void print() { | |
// if (treball()) { | |
// if (!consulta.isEmpty()) { | |
// String[] aliasCampsAmbNulls; | |
// if (aliasCamps != null) { | |
// aliasCampsAmbNulls = new String[nomsCamps.length]; | |
// for (int i = 0; i < aliasCamps.length; i++) { | |
// aliasCampsAmbNulls[i] = aliasCamps[i]; | |
// } | |
// } else { | |
// aliasCampsAmbNulls = null; | |
// } | |
// | |
// int[] maxLengthCamps = new int[nomsCamps.length]; | |
// int aux = 0; | |
// for (int i : select) { | |
// if (aliasCampsAmbNulls == null || aliasCampsAmbNulls[aux] == null) { | |
// maxLengthCamps[i] = nomsCamps[i].length(); | |
// } else { | |
// maxLengthCamps[i] = aliasCampsAmbNulls[aux].length(); | |
// } | |
// aux++; | |
// } | |
// | |
// for (String[] linia : consulta) { | |
// for (int i : select) { | |
// if (linia[i].length() > maxLengthCamps[i]) { | |
// maxLengthCamps[i] = linia[i].length(); | |
// } | |
// } | |
// } | |
// String filaSeparadora = ""; | |
// for (int i : select) { | |
// filaSeparadora += "+"; | |
// if (maxLengthCamps[i] > 0) { | |
// for (int j = 0; j < maxLengthCamps[i]; j++) { | |
// filaSeparadora += "-"; | |
// } | |
// } | |
// } | |
// filaSeparadora += "+"; | |
// | |
// String str = "" + filaSeparadora + "\n" + "|"; | |
// aux = 0; | |
// for (int i : select) { | |
// if (aliasCampsAmbNulls == null || aliasCampsAmbNulls[aux] == null) { | |
// str += encaixarStrAMida(nomsCamps[i], maxLengthCamps[i], TIPUS_JUSTIFICACIO.CENTRAT) + "|"; | |
// } else { | |
// str += encaixarStrAMida(aliasCampsAmbNulls[aux], maxLengthCamps[i], TIPUS_JUSTIFICACIO.CENTRAT) + "|"; | |
// } | |
// aux++; | |
// } | |
// | |
// // Fila única per afegir a sota dels noms/alias dels camps. Per poder indicar si s'ha ordenat per algun camp: | |
// if (!orderBys.isEmpty()) { | |
// String filaUnica = ""; | |
// boolean ordenat = false; | |
// //for (int i = 0; i < maxLengthCamps.length; i++) { | |
// OrderBy[] arrOrderBy = new OrderBy[nomsCamps.length]; | |
// for (OrderBy orderBy : orderBys) { | |
// arrOrderBy[orderBy.camp] = orderBy; | |
// } | |
// for (int i : select) { | |
// filaUnica += "+"; | |
// if (maxLengthCamps[i] > 0) { | |
// if (arrOrderBy[i] != null) { | |
// for (int j = 0; j < maxLengthCamps[i]; j++) { | |
// if ((maxLengthCamps[i]) / 2 == j) { | |
// if (arrOrderBy[i].asc) { | |
//// filaUnica+="/\\"; | |
// filaUnica += "△"; | |
// j++; | |
// } else { | |
//// filaUnica+="\\/"; | |
// filaUnica += "▽"; | |
// j++; | |
// } | |
// } | |
// filaUnica += "-"; | |
// } | |
// } else { | |
// for (int j = 0; j < maxLengthCamps[i]; j++) { | |
// filaUnica += "-"; | |
// } | |
// } | |
// } | |
// } | |
// filaUnica += "+"; | |
// str += "\n" + filaUnica + "\n"; | |
// } else { | |
// str += "\n" + filaSeparadora + "\n"; | |
// } | |
// | |
// for (String[] fila : consulta) { | |
// str += "|"; | |
// for (int i : select) { | |
// if (tipusCamps[i] == TIPUS_DADA.NUMERIC) { | |
// str += encaixarStrAMida(fila[i], maxLengthCamps[i], TIPUS_JUSTIFICACIO.DRETA) + "|"; | |
// } else { | |
// str += encaixarStrAMida(fila[i], maxLengthCamps[i], TIPUS_JUSTIFICACIO.ESQUERRA) + "|"; | |
// } | |
// } | |
// str += "\n" + filaSeparadora + "\n"; | |
// } | |
// System.out.println(str); | |
// } else { | |
// System.out.println("La consulta no ha produït cap resultat."); | |
// } | |
// } else { | |
// System.out.println("S'ha produït un error."); | |
// } | |
// } | |
/** | |
* enums | |
*/ | |
private enum DATE_TYPE { | |
TEXT, NUMERIC | |
} | |
public static void main(String[] args) { | |
ArrayList<String[]> arr = fileCsvToArrayList("testFiles/emigraciosexe2012.csv", 6, 9); | |
// arr=getColumns(arr,3,6); | |
arr = getColumnsBetween(arr, 3, 6); | |
orderArr(arr, 0, true, DATE_TYPE.TEXT); | |
for (String[] strings : arr) { | |
System.out.println(Arrays.toString(strings)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment