{"record":{"id":"6b14441808a23644","repo":"stanfordnlp/CoreNLP","slug":"exception-in-printtofileln","errorCode":null,"errorMessage":"Exception: in printToFileLn ","messagePattern":"Exception: in printToFileLn ","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"src/edu/stanford/nlp/util/StringUtils.java","lineNumber":1227,"sourceCode":"        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);\n      pw = new PrintWriter(fw);\n      pw.println(message);\n    } catch (Exception e) {\n      log.warn(\"Exception: in printToFileLn \" + file.getAbsolutePath() + ' ' + message);\n      log.warn(e);\n    } finally {\n      if (pw != null) {\n        pw.flush();\n        pw.close();\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 printToFile(File file, String message, boolean append) {\n    PrintWriter pw = null;\n    try {\n      Writer fw = new FileWriter(file, append);\n      pw = new PrintWriter(fw);","sourceCodeStart":1209,"sourceCodeEnd":1245,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/StringUtils.java#L1209-L1245","documentation":"StringUtils.printToFileLn(File, String, boolean append) writes a message plus newline to a file via a FileWriter/PrintWriter. Any Exception during open or write is not thrown; instead it logs 'Exception: in printToFileLn <path> <message>' and the method returns quietly. The caller cannot tell from the return value that the write failed.","triggerScenarios":"Calling StringUtils.printToFileLn with a File in a nonexistent directory, without write permission, on a full/read-only filesystem; the append variant failing because the existing file is unwritable.","commonSituations":"Appending log-like output to a file the user cannot write; writing to a path on a detached/unmounted volume; directory removed by another process between calls.","solutions":["Ensure the parent directory exists and is writable before calling","Check file permissions and filesystem space","Inspect the following log.warn(e) entry (error 1647) for the exact cause","Use java.nio.file.Files.write with CREATE/APPEND options if you need exceptions propagated"],"exampleFix":"// before\nStringUtils.printToFileLn(logFile, line, true);\n// after\nif (logFile.getParentFile() != null) logFile.getParentFile().mkdirs();\nFiles.write(logFile.toPath(), (line + System.lineSeparator()).getBytes(StandardCharsets.UTF_8),\n    StandardOpenOption.CREATE, StandardOpenOption.APPEND);","handlingStrategy":"try-catch","validationCode":"static void requireWritable(File f) throws java.io.IOException {\n    File p = f.getParentFile();\n    if (p != null && !p.isDirectory() && !p.mkdirs()) throw new java.io.IOException(\"mkdir failed: \" + p);\n    if (f.exists() && !f.canWrite()) throw new java.io.IOException(\"not writable: \" + f);\n}","typeGuard":null,"tryCatchPattern":"StringUtils.printToFileLn(logFile, line, true);\nif (!logFile.exists()) {\n    throw new java.io.IOException(\"printToFileLn failed silently: \" + logFile);\n}","preventionTips":["Verify parent directory exists and is writable before appending","Use java.nio Files with APPEND for critical logs","Check volume mount status before writing to external storage"],"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"}