How to list the names of the files that are in the directory?

3

The output of the following method is:

  

C: \ Users \ User \ Desktop \ files \ filename.jpg

How can I get only the filename.jpg in String format? I tried to use FileUtils but as an output I got this:

.ELs / G Yos e ([ E v ^ C O W ݭ : Zj 2 + յ v [{޶ J

public static void getImgs(String path){
    File file = new File(path);
    File[] arquivos = file.listFiles();

    try {
        for (File arquivo : arquivos) {
            System.out.println(arquivo);
        }
    }catch (Exception e){
        e.printStackTrace();
    }
}
    
asked by anonymous 26.02.2016 / 19:01

1 answer

5

When you use System.out.println(arquivo); you are actually printing getPath() .

follow the documentation

To print the name, try the following:

public static void getImgs(String path){
    File file = new File(path);
    File[] arquivos = file.listFiles();

    try {
        for (File arquivo : arquivos) {
            System.out.println(arquivo.getName());
        }
    }catch (Exception e){
        e.printStackTrace();
    }
}
    
26.02.2016 / 19:15