gpt4 book ai didi

java.io.IOException : The system cannot find the path specified writing a textfile 异常

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:20:53 24 4
gpt4 key购买 nike

我正在编写一个程序,试图在当前目录中创建一个新的文本文件,然后向其中写入一个字符串。但是,在尝试创建文件时,此代码块:

//Create the output text file.
File outputText = new File(filePath.getParentFile() + "\\Decrypted.txt");
try
{
outputText.createNewFile();
}
catch (IOException e)
{
e.printStackTrace();
}

给我这个错误信息:

java.io.IOException: The system cannot find the path specified
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(Unknown Source)
at code.Crypto.decrypt(Crypto.java:55)
at code.Crypto.main(Crypto.java:27)

因此我无法写入文件,因为它自然不存在。我在这里做错了什么?

最佳答案

如果您已经在使用 File 类,请考虑充分发挥其潜力,而不是自己完成一半的工作:

File outputText = new File(filePath.getParentFile(), "Decrypted.txt");

关于java.io.IOException : The system cannot find the path specified writing a textfile 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10777156/

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