stanfordnlp/CoreNLP · warning

Exception: in printToFileLn

Error message

Exception: in printToFileLn 

What it means

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.

Solutions

  1. Ensure the parent directory exists and is writable before calling
  2. Check file permissions and filesystem space
  3. Inspect the following log.warn(e) entry (error 1647) for the exact cause
  4. Use java.nio.file.Files.write with CREATE/APPEND options if you need exceptions propagated

Example fix

// before
StringUtils.printToFileLn(logFile, line, true);
// after
if (logFile.getParentFile() != null) logFile.getParentFile().mkdirs();
Files.write(logFile.toPath(), (line + System.lineSeparator()).getBytes(StandardCharsets.UTF_8),
    StandardOpenOption.CREATE, StandardOpenOption.APPEND);
Defensive patterns

Strategy: try-catch

Validate before calling

static void requireWritable(File f) throws java.io.IOException {
    File p = f.getParentFile();
    if (p != null && !p.isDirectory() && !p.mkdirs()) throw new java.io.IOException("mkdir failed: " + p);
    if (f.exists() && !f.canWrite()) throw new java.io.IOException("not writable: " + f);
}

Try / catch

StringUtils.printToFileLn(logFile, line, true);
if (!logFile.exists()) {
    throw new java.io.IOException("printToFileLn failed silently: " + logFile);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/6b14441808a23644. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/util/StringUtils.java:1227

        pw.flush();
        pw.close();
      }
    }
  }


  /**
   * Prints to a file.  If the file already exists, appends if
   * {@code append=true}, and overwrites if {@code append=false}.
   */
  public static void printToFileLn(File file, String message, boolean append) {
    PrintWriter pw = null;
    try {
      Writer fw = new FileWriter(file, append);
      pw = new PrintWriter(fw);
      pw.println(message);
    } catch (Exception e) {
      log.warn("Exception: in printToFileLn " + file.getAbsolutePath() + ' ' + message);
      log.warn(e);
    } finally {
      if (pw != null) {
        pw.flush();
        pw.close();
      }
    }
  }

  /**
   * Prints to a file.  If the file already exists, appends if
   * {@code append=true}, and overwrites if {@code append=false}.
   */
  public static void printToFile(File file, String message, boolean append) {
    PrintWriter pw = null;
    try {
      Writer fw = new FileWriter(file, append);
      pw = new PrintWriter(fw);

View on GitHub (pinned to 1b7edd19c4)