stanfordnlp/CoreNLP · warning
Exception: in printToFile
Error message
Exception: in printToFile
What it means
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.
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
Example fix
// before StringUtils.printToFile(out, text, false, true, "UTF-8"); // after File out = new File(path); if (out.getParentFile() != null) out.getParentFile().mkdirs(); Files.write(out.toPath(), text.getBytes(StandardCharsets.UTF_8));
Defensive patterns
Strategy: try-catch
Validate before calling
static void assertWritable(File f) throws java.io.IOException {
File parent = f.getParentFile();
if (parent != null && !parent.isDirectory() && !parent.mkdirs())
throw new java.io.IOException("Cannot create dir: " + parent);
if (f.exists() && !f.canWrite()) throw new java.io.IOException("Not writable: " + f);
} Try / catch
try {
StringUtils.printToFile(out, text, false, true, "UTF-8");
} catch (Exception e) { /* method swallows; verify instead */ }
if (!out.exists()) throw new java.io.IOException("printToFile failed silently for " + out); Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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'.
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
- Exception: in printToFileLn
- Error creating data exporter
- Serializing classifier to
- Error opening output file
- RuntimeIOException wrapping IOException from write
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/de59638050014a21.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/util/StringUtils.java:1205
public static void printToFile(File file, String message, boolean append,
boolean printLn, String encoding) {
PrintWriter pw = null;
try {
Writer fw;
if (encoding != null) {
fw = new OutputStreamWriter(new FileOutputStream(file, append),
encoding);
} else {
fw = new FileWriter(file, append);
}
pw = new PrintWriter(fw);
if (printLn) {
pw.println(message);
} else {
pw.print(message);
}
} catch (Exception e) {
log.warn("Exception: in printToFile " + file.getAbsolutePath());
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 printToFileLn(File file, String message, boolean append) {
PrintWriter pw = null;
try {
Writer fw = new FileWriter(file, append);View on GitHub (pinned to 1b7edd19c4)