簡體   English   中英

AsynchronousFileChannel拋出java.nio.file.NoSuchFileException

[英]AsynchronousFileChannel throw java.nio.file.NoSuchFileException

我正在使用java nio 2,並且編寫了一個簡單的應用程序,該應用程序應創建一個文件並向其中寫入內容,但是我得到的文件不存在異常

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());
    }

好的,我發現我應該添加寫選項

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM