Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AsynchronousFileChannel引发java.nio.file.NoSuchFileException_Java_Nio - Fatal编程技术网

AsynchronousFileChannel引发java.nio.file.NoSuchFileException

AsynchronousFileChannel引发java.nio.file.NoSuchFileException,java,nio,Java,Nio,我正在玩JavaNIO2,我编写了一个简单的应用程序,应该创建一个文件并将内容写入其中,但我得到的文件不存在异常 ByteBuffer buffer = ByteBuffer.wrap("jhkjhkhjkhkjhkjhkjhkjhkhkjhkjhkjh".getBytes()); Path path = Paths.get("F:", "dummyFile.txt"); try(AsynchronousFileChannel asynchronousFileChannel

我正在玩JavaNIO2,我编写了一个简单的应用程序,应该创建一个文件并将内容写入其中,但我得到的文件不存在异常

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());
    }
ByteBuffer buffer=ByteBuffer.wrap(“jhkjhkjhkjhkjh.getBytes());
Path Path=Path.get(“F:,“dummyFile.txt”);
try(异步文件通道异步文件通道=
AsynchronousFileChannel.open(路径,StandardOpenOption.CREATE\u NEW)){
Future-Future=asynchronousFileChannel.write(缓冲区,0);
而(!future.isDone()){
System.out.println(“等待”);
}
System.out.println(String.format(“Done-bytes writed%d”,future.get());
}捕获(例外e){
System.out.println(例如toString());
}

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

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