Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- public class DominoCSVtoArrayList {
- public static void main(String[] args) {
- BufferedReader dominoBuffer = null;
- try {
- String dominoLine;
- dominoBuffer = new BufferedReader(new FileReader("src/dominos.csv")); //li le fichier CSV
- while ((dominoLine = dominoBuffer.readLine())!=null) { //tant que chaque ligne lue n'est pas egale à null, on lit le fichier
- System.out.println ("RawCSV : "+ dominoLine); // et on renvoie cela.
- System.out.println("Converted ArrayList Data : "+ dominoCSVtoArrayList(dominoLine)+"\n"); //Insert a newline in the text at this point.
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (dominoBuffer != null) dominoBuffer.close();
- } catch (IOException dominoException) {
- dominoException.printStackTrace();
- }
- }
- }
- public static ArrayList<String> dominoCSVtoArrayList (String dominoCSV){
- ArrayList<String> dominoResult = new ArrayList <String>(); //nous créons une liste appelee dominoResult
- if (dominoCSV != null) {
- String[] splitData = dominoCSV.split("\\s*,\\s*"); //si le document csv est différent de null, une virgule est utilisée pour séparer les éléments
- /*
- \s* any number of whitespace characters
- a comma
- \s* any number of whitespace characters
- which will split on commas and consume any spaces either side
- */
- for (int i=0; i<splitData.length; i++) {
- if (!(splitData[i]== null) || !(splitData[i].length()==0)){
- dominoResult.add(splitData[i].trim()); // The java lang.string.trim()is a built-in function that eliminates leading and trailing spaces
- }
- }
- }
- return dominoResult;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement