{"record":{"id":"de59638050014a21","repo":"stanfordnlp/CoreNLP","slug":"exception-in-printtofile","errorCode":null,"errorMessage":"Exception: in printToFile ","messagePattern":"Exception: in printToFile ","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"src/edu/stanford/nlp/util/StringUtils.java","lineNumber":1205,"sourceCode":"  public static void printToFile(File file, String message, boolean append,\n                                 boolean printLn, String encoding) {\n    PrintWriter pw = null;\n    try {\n      Writer fw;\n      if (encoding != null) {\n        fw = new OutputStreamWriter(new FileOutputStream(file, append),\n                                         encoding);\n      } else {\n        fw = new FileWriter(file, append);\n      }\n      pw = new PrintWriter(fw);\n      if (printLn) {\n        pw.println(message);\n      } else {\n        pw.print(message);\n      }\n    } catch (Exception e) {\n      log.warn(\"Exception: in printToFile \" + file.getAbsolutePath());\n      log.warn(e);\n    } finally {\n      if (pw != null) {\n        pw.flush();\n        pw.close();\n      }\n    }\n  }\n\n\n  /**\n   * Prints to a file.  If the file already exists, appends if\n   * {@code append=true}, and overwrites if {@code append=false}.\n   */\n  public static void printToFileLn(File file, String message, boolean append) {\n    PrintWriter pw = null;\n    try {\n      Writer fw = new FileWriter(file, append);","sourceCodeStart":1187,"sourceCodeEnd":1223,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/StringUtils.java#L1187-L1223","documentation":"StringUtils.printToFile(File, String, boolean append, boolean printLn, String encoding) writes a message to a file through a PrintWriter. If opening or writing throws any Exception (missing directory, permissions, disk full, bad encoding), the method does not propagate it; it logs 'Exception: in printToFile <path>' via the logging framework and returns normally. The write silently fails from the caller's perspective.","triggerScenarios":"Calling StringUtils.printToFile with a File whose parent directory does not exist, without write permission, on a read-only or full filesystem, or with an invalid charset name for the encoding argument.","commonSituations":"Writing output to a path with a typo or missing output directory; running as a user without write access; disk quota exceeded; wrong encoding string like 'utf_8'.","solutions":["Ensure the parent directory exists before calling: file.getParentFile().mkdirs()","Check write permissions on the target path and that the disk is not full","Validate the encoding string (e.g. 'UTF-8')","Check the application log for the logged exception to identify the exact IOException","Switch to a writer API that throws (Files.write) if you need hard failure"],"exampleFix":"// before\nStringUtils.printToFile(out, text, false, true, \"UTF-8\");\n// after\nFile out = new File(path);\nif (out.getParentFile() != null) out.getParentFile().mkdirs();\nFiles.write(out.toPath(), text.getBytes(StandardCharsets.UTF_8));","handlingStrategy":"try-catch","validationCode":"static void assertWritable(File f) throws java.io.IOException {\n    File parent = f.getParentFile();\n    if (parent != null && !parent.isDirectory() && !parent.mkdirs())\n        throw new java.io.IOException(\"Cannot create dir: \" + parent);\n    if (f.exists() && !f.canWrite()) throw new java.io.IOException(\"Not writable: \" + f);\n}","typeGuard":null,"tryCatchPattern":"try {\n    StringUtils.printToFile(out, text, false, true, \"UTF-8\");\n} catch (Exception e) { /* method swallows; verify instead */ }\nif (!out.exists()) throw new java.io.IOException(\"printToFile failed silently for \" + out);","preventionTips":["Create parent directories before writing","Prefer Files.write / explicit writers for essential output","Check disk space and permissions in deployment scripts","Validate encoding names against StandardCharsets"],"tags":["java","io","file-write","swallowed-exception"],"backgroundTag":"file-write-failed","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}