{"record":{"id":"401557bc496d873d","repo":"dianping/cat","slug":"can-t-write-to-an-existing-directory-s","errorCode":null,"errorMessage":"Can't write to an existing directory(%s)","messagePattern":"Can't write to an existing directory\\((.+?)\\)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"cat-client/src/main/java/com/dianping/cat/support/Files.java","lineNumber":246,"sourceCode":"      }\n\n      public String readFrom(InputStream is, String charsetName) throws IOException {\n         ByteArrayOutputStream baos = new ByteArrayOutputStream(16 * 1024);\n\n         copy(is, baos, AutoClose.INPUT);\n         return baos.toString(charsetName);\n      }\n\n      public String readUtf8String(InputStream is) throws IOException {\n         ByteArrayOutputStream baos = new ByteArrayOutputStream(16 * 1024);\n\n         copy(is, baos, AutoClose.INPUT);\n         return baos.toString(\"utf-8\");\n      }\n\n      public void writeTo(File file, byte[] data) throws IOException {\n         if (file.isDirectory()) {\n            throw new IOException(String.format(\"Can't write to an existing directory(%s)\", file));\n         }\n\n         Dir.INSTANCE.createDir(file.getParentFile());\n\n         FileOutputStream fos = new FileOutputStream(file);\n\n         try {\n            fos.write(data);\n         } finally {\n            try {\n               fos.close();\n            } catch (IOException e) {\n               // ignore it\n            }\n         }\n      }\n\n      public void writeTo(File file, String data) throws IOException {","sourceCodeStart":228,"sourceCodeEnd":264,"githubUrl":"https://github.com/dianping/cat/blob/e815e74d4c2dd74edac831241f1253fcc7d25381/cat-client/src/main/java/com/dianping/cat/support/Files.java#L228-L264","documentation":"Thrown by Files.writeTo(File, byte[]) when the target File object is an existing directory. The utility refuses to open a FileOutputStream on a directory because the OS would reject it anyway; failing fast with a clear message is preferable. It is a plain IOException, so it propagates to whatever file-persistence logic called it.","triggerScenarios":"Calling Files.INSTANCE.writeTo(file, data) (or the convenience writeUtf8StringTo/writeCharsTo wrappers) where file.isDirectory() is true — e.g. the path points at an existing folder, or a path string was built without a file name component (\"/data/cat\" instead of \"/data/cat/report.txt\").","commonSituations":"Misbuilt output paths (directory prefix passed instead of full file path), a file name that is empty so getPath() resolves to the parent directory, or a race where a directory was created at the location a file was expected. Common in report/storage writers that derive file paths from domain/date concatenation.","solutions":["Log or inspect the File object right before the call: print getAbsolutePath() and isDirectory() to find why it resolves to a folder.","Fix the path construction so it always appends a concrete file name (and extension) after the parent directory.","If the file name is dynamic, guard it: reject empty/blank names before calling writeTo.","As a defensive measure for races, check isDirectory() first and either delete the stray directory or pick an alternate path."],"exampleFix":"// before\nFile f = new File(baseDir + domain); // domain dir, no file name\nFiles.INSTANCE.writeTo(f, bytes); // throws\n\n// after\nFile dir = new File(baseDir, domain);\nFile f = new File(dir, domain + \".ser\");\nFiles.INSTANCE.writeTo(f, bytes);","handlingStrategy":"validation","validationCode":"if (file == null || file.isDirectory()) {\n    throw new IllegalArgumentException(\"Not a writable file path: \" + file);\n}","typeGuard":null,"tryCatchPattern":"catch (IOException e) { /* message names the directory; fix path and retry once manually */ }","preventionTips":["Build output paths as parent-dir + explicit file-name pairs, never string-concat a bare prefix.","Assert file names are non-empty before constructing the File.","Unit-test path builders with empty/blank file-name inputs."],"tags":["filesystem","io","path-construction"],"backgroundTag":null,"analyzedSha":"e815e74d4c2dd74edac831241f1253fcc7d25381","analyzedAt":"2026-08-14T14:22:34.512Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}