output files from the folder with no extension

后端 未结 1 1779
清酒与你
清酒与你 2021-01-17 04:23

help make this, the following code finds the txt files in the folder, and how to make it lists the names of files without extensions. Here\'s the code:

Strin         


        
相关标签:
1条回答
  • 2021-01-17 04:55

    If I understand correctly, you want to list all files in a folder, regardless of their extension. In this case, use:

    String list[] = new File("C:\\Users\\Eric\\Desktop\\txt\\").list(new FileFilter() {
        public boolean accept(File file) {
            return file.isFile();
        }
    });
    

    Edit: Ok, I did not understand you correctly. You want to strip the extension from the file name in your if statement. You can use substring combined with lastIndexOf:

    String name = list[i];
    String nameWithoutExtension = name.substring(0, name.lastIndexOf("."));
    

    or, since all your extensions are ".txt", the following will work too:

    String nameWithoutExtension = name.substring(0, name.lastIndexOf(".txt"));
    
    0 讨论(0)
提交回复
热议问题