0

I am trying to read every line in a file and then splitting them. It is the first time I do this and I am not sure if I am using the right Charset or path to the file. Thank you for your help! Here is my try:

The path (ruta) in main is specified like this:

Path ruta=FileSystems.getDefault().getPath("mapa.csv");

The name of my file is mapa.csv, I am trying to get to its folder. The constructor is the following:

public void construirMapa() {
    List<String> lineas = null;   
    try{
        //this returns null
        lineas = Files.readAllLines(ruta, Charset.defaultCharset());  
    }catch(IOException ex){
        System.out.println(ex.getMessage());
    }
    if(lineas!=null){
        Iterator <String> linea=lineas.iterator();
        while(linea.hasNext()){
            String[] atributo=linea.next().split(";");
        }
    }
}
  • The code you are showing looks fine in general; maybe except for the thing that it does nothing with that **atributo** array that it creates. You wouldnt even notice that anything is going on there; your code would just read that file, process content ... but without any visible side effects! – GhostCat Dec 12 '16 at 09:19
  • Why is `ruta` specified like that? Use `Path ruta = Paths.get("mapa.csv")` – Andreas Dec 12 '16 at 09:22

1 Answers1

-1

Seems ok, also you can use the following code:

catch (IOException ex)
{
    e.printStackTrace();
}

to get stacktrace in error case

N00b Pr0grammer
  • 4,503
  • 5
  • 32
  • 46
mystdeim
  • 4,802
  • 11
  • 49
  • 77