简体   繁体   中英

How to properly use readAllLines in Java?

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(";");
        }
    }
}

Seems ok, also you can use the following code:

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

to get stacktrace in error case

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM