gpt4 book ai didi

java - AsynchronousFileChannel 抛出 java.nio.file.NoSuchFileException

转载 作者:行者123 更新时间:2023-12-01 11:51:31 27 4
gpt4 key购买 nike

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

关于java - AsynchronousFileChannel 抛出 java.nio.file.NoSuchFileException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28786805/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com