English 中文(简体)
java.io.IOException: 系统找不到写入文本文件的指定路径 。
原标题:java.io.IOException: The system cannot find the path specified writing a textfile

我在撰写一个程序, 我试图在当前目录中创建一个新的文本文件, 然后写一个字符串到它。 但是, 当试图创建文件时, 这个代码块 :

//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 outputText = new File(filePath.getParentFile(), "Decrypted.txt");
问题回答

filePath.get ParentFile () () ) 的值是多少? 您使用的操作系统是什么? 以系统独立的方式加入两个路径可能是更好的办法, 比如 :

filePath.getParentFile() + File.separator + "Decrypted.txt"

它应该作为文件Path 指针的文件的比喻而创建 。

例如,例如,如果

File filePath = new File("C:\\Test\\a.txt");

然后,它应当在试验中被创造出来。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签