alibaba/nacos · error · IOException
file not exist
Error message
file not exist
What it means
Thrown by ConcurrentDiskUtil.writeFileContent when opening the file with RandomAccessFile raises FileNotFoundException, which is re-thrown as IOException with this message. The file was checked/created just above, so this indicates it was deleted between the existence check and the open, or it is a directory, or permissions prevent access.
Source
Thrown at client/src/main/java/com/alibaba/nacos/client/utils/ConcurrentDiskUtil.java:123
* @throws IOException IOException
*/
public static Boolean writeFileContent(File file, String content, String charsetName)
throws IOException {
if (!file.exists() && !file.createNewFile()) {
return false;
}
try (RandomAccessFile raf = new RandomAccessFile(file, READ_WRITE);
FileChannel channel = raf.getChannel();
FileLock lock = tryLock(file, channel, false)) {
byte[] contentBytes = content.getBytes(charsetName);
ByteBuffer sendBuffer = ByteBuffer.wrap(contentBytes);
while (sendBuffer.hasRemaining()) {
channel.write(sendBuffer);
}
channel.truncate(contentBytes.length);
} catch (FileNotFoundException e) {
throw new IOException("file not exist");
}
return true;
}
/**
* transfer ByteBuffer to String.
*
* @param buffer buffer
* @param charsetName charsetName
* @return String
* @throws IOException IOException
*/
public static String byteBufferToString(ByteBuffer buffer, String charsetName)
throws IOException {
Charset charset = Charset.forName(charsetName);
CharsetDecoder decoder = charset.newDecoder();
CharBuffer charBuffer = decoder.decode(buffer.asReadOnlyBuffer());
return charBuffer.toString();View on GitHub (pinned to 9b989acdf1)
Solutions
- Verify the file path does not point to a directory and the parent directory exists.
- Check filesystem permissions for the JVM process on the target path.
- Investigate concurrent cleanup processes that may delete the file between creation and open.
- Ensure the disk has free space and the filesystem is writable.
Defensive patterns
Strategy: try-catch
Validate before calling
File parent = file.getParentFile();
if (parent != null && !parent.exists() && !parent.mkdirs()) {
throw new IOException("Cannot create parent directory: " + parent);
}
if (file.exists() && file.isDirectory()) {
throw new IOException("Path is a directory, not a file: " + file);
} Try / catch
try {
ConcurrentDiskUtil.writeFileContent(file, content, charsetName);
} catch (IOException e) {
if ("file not exist".equals(e.getMessage())) {
// retry once after ensuring the file and parent exist
file.getParentFile().mkdirs();
file.createNewFile();
ConcurrentDiskUtil.writeFileContent(file, content, charsetName);
} else {
throw e;
}
} Prevention
- Verify the file path is not a directory and the parent directory exists before writing.
- Check filesystem permissions for the JVM process.
- Investigate concurrent cleanup processes that may delete files during write.
When it happens
Trigger: Between createNewFile() and new RandomAccessFile(file, 'rw'), the file is deleted by another process; the path points to a directory; or the JVM lacks read/write permission on the file after creation.
Common situations: Concurrent processes cleaning up temp/config directories; the file path actually resolves to a directory; filesystem permissions changed; the disk is full or the filesystem is read-only causing createNewFile to silently fail in a race.
Related errors
AI-assisted analysis of alibaba/nacos@9b989acdf1 (2026-08-14).
Data as JSON: /api/errors/6f564d0124164ca0.
Report an issue: GitHub.