AsynchronousFileChannel抛出java.nio.file.NoSuchFileException

时间:2015-02-28 20:58:31

标签: java nio

我正在玩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());
    }

1 个答案:

答案 0 :(得分:1)

好的,我发现我应该添加写选项

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