{"record":{"id":"02b70132a3e1773d","repo":"chinabugotech/hutool","slug":"path-is-not-directory","errorCode":null,"errorMessage":"Path [{}] is not directory!","messagePattern":"Path \\[(.+?)\\] is not directory!","errorType":"exception","errorClass":"IORuntimeException","httpStatus":null,"severity":"error","filePath":"hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java","lineNumber":98,"sourceCode":"\t}\n\n\t/**\n\t * 列出指定路径下的目录和文件<br>\n\t * 给定的绝对路径不能是压缩包中的路径\n\t *\n\t * @param path 目录绝对路径或者相对路径\n\t * @return 文件列表（包含目录）\n\t */\n\tpublic static File[] ls(String path) {\n\t\tif (path == null) {\n\t\t\treturn null;\n\t\t}\n\n\t\tFile file = file(path);\n\t\tif (file.isDirectory()) {\n\t\t\treturn file.listFiles();\n\t\t}\n\t\tthrow new IORuntimeException(StrUtil.format(\"Path [{}] is not directory!\", path));\n\t}\n\n\t/**\n\t * 文件是否为空<br>\n\t * 目录：里面没有文件时为空 文件：文件大小为0时为空\n\t *\n\t * @param file 文件\n\t * @return 是否为空，当提供非目录时，返回false\n\t */\n\tpublic static boolean isEmpty(File file) {\n\t\tif (null == file || false == file.exists()) {\n\t\t\treturn true;\n\t\t}\n\n\t\tif (file.isDirectory()) {\n\t\t\tString[] subFiles = file.list();\n\t\t\treturn ArrayUtil.isEmpty(subFiles);\n\t\t} else if (file.isFile()) {","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java#L80-L116","documentation":"FileUtil.ls(path) lists the children of a directory. It resolves the path to a File and, if that File is not a directory, throws IORuntimeException(\"Path [{}] is not directory!\"). This is the Hutool guarded equivalent of File.listFiles().","triggerScenarios":"Calling FileUtil.ls(path) where path resolves to a regular file or to a non-existent path (isDirectory() is false for both).","commonSituations":"User-supplied path that actually points at a file rather than a folder; a config value holding a file path fed to ls(); computing a path relative to a file instead of its parent directory.","solutions":["Check FileUtil.isDirectory(path) before calling ls().","If you meant the containing folder, resolve the parent first: FileUtil.file(path).getParentFile().","Validate the path comes from a directory chooser/source."],"exampleFix":"// before\nFile[] children = FileUtil.ls(path); // throws if path is a file\n\n// after\nFile dir = FileUtil.file(path);\nif (dir.isDirectory()) {\n    File[] children = FileUtil.ls(path);\n} else {\n    children = FileUtil.ls(dir.getParent());\n}","handlingStrategy":"validation","validationCode":"File dir = FileUtil.file(path);\nif (dir != null && dir.isDirectory()) {\n    File[] children = FileUtil.ls(path);\n} else if (dir != null && dir.isFile()) {\n    // user gave a file: list its parent instead\n    File[] children = FileUtil.ls(dir.getParent());\n}","typeGuard":"static boolean isListableDir(String path) {\n    if (path == null) return false;\n    File f = new File(path);\n    return f.exists() && f.isDirectory();\n}","tryCatchPattern":"try {\n    return FileUtil.ls(path);\n} catch (IORuntimeException e) {\n    // path was not a directory: return empty or rethrow with context\n}","preventionTips":["Check FileUtil.isDirectory(path) before ls().","If you expect a directory but get a file, list the parent.","Validate paths sourced from user input or config before listing."],"tags":["io","file","directory"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}