Use of FilenameFilter

后端 未结 5 1718
半阙折子戏
半阙折子戏 2020-12-15 05:33

I have a directory:

File dir = new File(MY_PATH);

I would like to list all the files whose name is indicated as integer numbers strings, e.

5条回答
  •  一整个雨季
    2020-12-15 06:00

    You should override accept in the interface FilenameFilter and make sure that the parameter name has only numeric chars. You can check this by using matches:

    String[] list = dir.list(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.matches("[0-9]+");
        }
    });
    

提交回复
热议问题