Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm writing a program where I'm trying to create a new text file in the current directory, and then write a string to it. However, when trying to create the file, this block of code:

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

is giving me this error message:

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)

Because of this I cannot write to the file because it naturally does not exist. What am I doing wrong here?

like image 450
Inglonias Avatar asked May 27 '12 20:05

Inglonias


2 Answers

If you're working with the File class already, consider using its full potential instead of doing half the work on your own:

File outputText = new File(filePath.getParentFile(), "Decrypted.txt");
like image 96
Wormbo Avatar answered Jan 01 '23 21:01

Wormbo


What's the value of filePath.getParentFile()? What operating system are you using? It might be a better idea to join both paths in a system-independent way, like this:

filePath.getParentFile() + File.separator + "Decrypted.txt"
like image 27
Óscar López Avatar answered Jan 01 '23 21:01

Óscar López