{"record":{"id":"c62a546b0d553a5d","repo":"gradle/gradle","slug":"could-not-write-to-file-s","errorCode":null,"errorMessage":"Could not write to file '%s'.","messagePattern":"Could not write to file '(.+?)'\\.","errorType":"exception","errorClass":"UncheckedIOException","httpStatus":null,"severity":"error","filePath":"platforms/core-runtime/base-services/src/main/java/org/gradle/internal/IoActions.java","lineNumber":152,"sourceCode":"            this.encoding = encoding;\n        }\n\n        @Override\n        public void execute(Action<? super BufferedWriter> action) {\n            try {\n                File parentFile = file.getParentFile();\n                if (parentFile != null) {\n                    if (!parentFile.mkdirs() && !parentFile.isDirectory()) {\n                        throw new IOException(String.format(\"Unable to create directory '%s'\", parentFile));\n                    }\n                }\n                try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(file.toPath()), encoding))) {\n                    action.execute(writer);\n                }\n            } catch (FileSystemException e) {\n                throw new UncheckedIOException(String.format(\"%s: '%s'.\", e.getReason(), file), e);\n            } catch (IOException e) {\n                throw new UncheckedIOException(String.format(\"Could not write to file '%s'.\", file), e);\n            }\n        }\n    }\n\n}\n","sourceCodeStart":134,"sourceCodeEnd":158,"githubUrl":"https://github.com/gradle/gradle/blob/534f27719b66953f95cc907aae7f2c1b12f5482d/platforms/core-runtime/base-services/src/main/java/org/gradle/internal/IoActions.java#L134-L158","documentation":"Non-FileSystemException IO failures while writing a text file through IoActions are wrapped as UncheckedIOException(\"Could not write to file '%s'.\"). The cause holds the real IOException - stream failures, encoding problems, or generic I/O errors that the filesystem-reason branch did not classify.","triggerScenarios":"The action executed against the BufferedWriter throws a plain IOException, or the stream fails mid-write in a way that is not a FileSystemException - the second catch branch wraps it with the target file path.","commonSituations":"Writer actions throwing checked exceptions, broken pipes when the consumer of the stream died, or network-mounted filesystems returning generic IO errors.","solutions":["Inspect the cause IOException - it carries the actual reason the write failed","If the file sits on a network mount, check connectivity and remount","Verify the configured encoding string is a valid charset name"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    IoActions.writeTextFile(targetFile, charsetName).execute(action);\n} catch (UncheckedIOException e) {\n    if (e.getMessage() != null && e.getMessage().startsWith(\"Could not write to file\")) {\n        IOException cause = (IOException) e.getCause(); // real reason lives here\n        // handle/retry on the specific IOException\n    }\n    throw e;\n}","preventionTips":["Inspect the cause IOException rather than the wrapper message","Validate charset names before constructing writers","Be cautious with writes onto network-mounted filesystems"],"tags":["io","file-write","unchecked-io"],"backgroundTag":"file-write-failed","analyzedSha":"534f27719b66953f95cc907aae7f2c1b12f5482d","analyzedAt":"2026-08-22T08:09:12.375Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}