{"record":{"id":"6dad99b687a07383","repo":"stanfordnlp/CoreNLP","slug":"exception-in-printtofile-file-getabsolutepath","errorCode":null,"errorMessage":"Exception in printToFile \" + file.getAbsolutePath()","messagePattern":"Exception in printToFile \" \\+ file\\.getAbsolutePath\\(\\)","errorType":"exception","errorClass":"RuntimeIOException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/util/StringUtils.java","lineNumber":1248,"sourceCode":"      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);\n      pw.print(message);\n    } catch (Exception e) {\n      throw new RuntimeIOException(\"Exception in printToFile \" + file.getAbsolutePath(), e);\n    } finally {\n      IOUtils.closeIgnoringExceptions(pw);\n    }\n  }\n\n\n  /**\n   * Prints to a file.  If the file does not exist, rewrites the file;\n   * does not append.\n   */\n  public static void printToFile(File file, String message) {\n    printToFile(file, message, false);\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   */","sourceCodeStart":1230,"sourceCodeEnd":1266,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/StringUtils.java#L1230-L1266","documentation":"StringUtils.printToFile(File, String, boolean append) writes a message to a file via FileWriter/PrintWriter. Any exception during opening or writing is rethrown as RuntimeIOException('Exception in printToFile <file.getAbsolutePath()>'), so callers get a uniform runtime exception when the write fails.","triggerScenarios":"Calling printToFile when the target file cannot be created or written: nonexistent parent directory, no write permission, the path is a directory, or disk is full.","commonSituations":"Logging output to a directory that was never created; writing to read-only volumes in containers; output path pointing at an existing directory; exceeding disk quota on shared clusters.","solutions":["Ensure the parent directory exists (file.getParentFile().mkdirs()) before calling printToFile","Verify write permission on the target directory and that the path is not a directory itself","Check available disk space/quota; inspect the cause of the RuntimeIOException for the exact IOException"],"exampleFix":"// before\nStringUtils.printToFile(new File(\"out/result.txt\"), text, false); // out/ missing\n// after\nFile f = new File(\"out/result.txt\");\nf.getParentFile().mkdirs();\nStringUtils.printToFile(f, text, false);","handlingStrategy":"try-catch","validationCode":"File f = new File(path);\nif (f.isDirectory()) throw new IllegalStateException(\"Output path is a directory: \" + f);\nFile parent = f.getParentFile();\nif (parent != null && !parent.isDirectory()) parent.mkdirs();","typeGuard":null,"tryCatchPattern":"try {\n  StringUtils.printToFile(file, message, append);\n} catch (RuntimeIOException e) {\n  log.error(\"Failed writing \" + file + \": \" + e.getCause());\n}","preventionTips":["Create parent directories before writing output files","Check disk space and write permissions on output volumes, especially in containers","Log to a writable temp path with a fallback if the primary output path fails"],"tags":["java","io","file-write"],"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"}