{"record":{"id":"8ac5db41b5f7d2f2","repo":"eclipse-vertx/vert.x","slug":"cannot-open-file-for-neither-reading-nor-writing","errorCode":null,"errorMessage":"Cannot open file for neither reading nor writing","messagePattern":"Cannot open file for neither reading nor writing","errorType":"validation","errorClass":"FileSystemException","httpStatus":null,"severity":"error","filePath":"vertx-core/src/main/java/io/vertx/core/file/impl/AsyncFileImpl.java","lineNumber":79,"sourceCode":"  private boolean closed;\n  private Runnable closedDeferred;\n  private long writesOutstanding;\n  private boolean overflow;\n  private Handler<Throwable> exceptionHandler;\n  private Handler<Void> drainHandler;\n  private long writePos;\n  private int maxWrites = 128 * 1024;    // TODO - we should tune this for best performance\n  private int lwm = maxWrites / 2;\n  private int readBufferSize = DEFAULT_READ_BUFFER_SIZE;\n  private InboundBuffer<Buffer> queue;\n  private Handler<Buffer> handler;\n  private Handler<Void> endHandler;\n  private long readPos;\n  private long readLength = Long.MAX_VALUE;\n\n  AsyncFileImpl(VertxInternal vertx, String path, OpenOptions options, ContextInternal context) {\n    if (!options.isRead() && !options.isWrite()) {\n      throw new FileSystemException(\"Cannot open file for neither reading nor writing\");\n    }\n    this.vertx = vertx;\n    Path file = Paths.get(path);\n    HashSet<OpenOption> opts = new HashSet<>();\n    if (options.isRead()) opts.add(StandardOpenOption.READ);\n    if (options.isWrite()) opts.add(StandardOpenOption.WRITE);\n    if (options.isCreate()) opts.add(StandardOpenOption.CREATE);\n    if (options.isCreateNew()) opts.add(StandardOpenOption.CREATE_NEW);\n    if (options.isSync()) opts.add(StandardOpenOption.SYNC);\n    if (options.isDsync()) opts.add(StandardOpenOption.DSYNC);\n    if (options.isDeleteOnClose()) opts.add(StandardOpenOption.DELETE_ON_CLOSE);\n    if (options.isSparse()) opts.add(StandardOpenOption.SPARSE);\n    if (options.isTruncateExisting()) opts.add(StandardOpenOption.TRUNCATE_EXISTING);\n    try {\n      if (options.getPerms() != null) {\n        FileAttribute<?> attrs = PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString(options.getPerms()));\n        ch = AsynchronousFileChannel.open(file, opts, vertx.workerPool().executor(), attrs);\n      } else {","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/eclipse-vertx/vert.x/blob/fb308bd8c3f12c79f4ae89bef67fadf6c80d036e/vertx-core/src/main/java/io/vertx/core/file/impl/AsyncFileImpl.java#L61-L97","documentation":"An AsyncFile must be opened in at least read or write mode. The AsyncFileImpl constructor throws this FileSystemException when OpenOptions has both isRead() and isWrite() false, since StandardOpenOption requires at least one access mode for AsynchronousFileChannel.open.","triggerScenarios":"Calling vertx.fileSystem().open(path, new OpenOptions()) with neither setRead(true) nor setWrite(true) - the OpenOptions default has both false.","commonSituations":"Creating 'new OpenOptions()' without setting any flags before open(); refactoring that removed setWrite(true) but forgot setRead(true); building options from config where the read/write booleans defaulted to false.","solutions":["Set at least one access mode: options.setRead(true) and/or options.setWrite(true) before open().","For read-modify-write use both: new OpenOptions().setRead(true).setWrite(true).","Validate the OpenOptions loaded from configuration before passing it to fileSystem().open()."],"exampleFix":"// before\nOpenOptions opts = new OpenOptions(); // read=false, write=false\nfileSystem.open(\"f.txt\", opts);\n// after\nOpenOptions opts = new OpenOptions().setRead(true).setWrite(true);\nfileSystem.open(\"f.txt\", opts);","handlingStrategy":"validation","validationCode":"if (!options.isRead() && !options.isWrite()) {\n  throw new IllegalArgumentException(\"OpenOptions must set read and/or write\");\n}\nfileSystem.open(path, options);","typeGuard":null,"tryCatchPattern":"try {\n  fileSystem.open(path, options);\n} catch (FileSystemException e) {\n  // neither read nor write requested; fix OpenOptions\n}","preventionTips":["Never pass a bare 'new OpenOptions()' to open(); set setRead(true) or setWrite(true)","Centralize OpenOptions construction in a helper that enforces at least one access mode","Validate file options loaded from configuration before opening files"],"tags":["file","open-options","illegal-argument","asyncfile"],"backgroundTag":"invalid-argument-value","analyzedSha":"fb308bd8c3f12c79f4ae89bef67fadf6c80d036e","analyzedAt":"2026-09-06T11:37:12.241Z","contentChangedAt":"2026-09-06T11:37:12.241Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}