简体   繁体   中英

AsynchronousFileChannel throw java.nio.file.NoSuchFileException

I am playing with java nio 2 and I wrote a simple app that should create a file and write contents to it, but i am getting file not exists exception

ByteBuffer buffer = ByteBuffer.wrap("jhkjhkhjkhkjhkjhkjhkjhkhkjhkjhkjh".getBytes());

    Path path = Paths.get("F:", "dummyFile.txt");

    try(AsynchronousFileChannel asynchronousFileChannel =
            AsynchronousFileChannel.open(path, StandardOpenOption.CREATE_NEW)) {
        Future<Integer> future = asynchronousFileChannel.write(buffer, 0);
        while (!future.isDone()) {
            System.out.println("waiting");
        }

        System.out.println(String.format("Done - bytes written %d", future.get()));


    } catch (Exception e) {
        System.out.println(e.toString());
    }

Ok , I found that i supposed to add the write option

AsynchronousFileChannel asynchronousFileChannel =
            AsynchronousFileChannel.open(path, StandardOpenOption.CREATE, StandardOpenOption.WRITE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM