9

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?

3
  • 1
    How is filePath constructed? Commented May 27, 2012 at 20:51
  • File filePath = fc.getSelectedFile(); Commented May 27, 2012 at 20:52
  • what is the value of filePath.getParentFile() just before outputText is constructed? Commented May 27, 2012 at 20:54

3 Answers 3

6

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");
Sign up to request clarification or add additional context in comments.

2 Comments

java.io.FileNotFoundException: ::{031E4825-7B94-4DC3-B131-E946B44C8DD5}\Pictures.library-ms\Decrypted.txt (The system cannot find the path specified) is what happens. Bear in mind, this is the Pictures library.
I'm marking this as the answer, because it works and the error messages I'm getting are not my fault, but rather the fault of Windows and the way Libraries are handled. Thanks for all of your help, everyone!
2

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"

8 Comments

While that got rid of the error message, I don't see the file in the directory I used. Where is it?
Actually, on further study, this only removed the error message. I don't see the file
@Inglonias write this in your code, right after creating the new file: System.out.println(outputText.getCanonicalPath());. Just to be sure where's the file, if it's not where you expect it you'll need to fix the path accordingly
It's going to "C:\Users\Joshua (coolguy)\workspace\GUI Steganography MK II\Pictures;Decrypted.txt" which is the workspace path. What gives?
This thing is being incredibly frustrating! I have a different error message now. java.io.IOException: The filename, directory name, or volume label syntax is incorrect It's possible that since I'm working on the file from the Pictures library (Win 7) it's being stupid. EDIT: Moving the file to the Desktop fixes all errors.
|
0

It should be created as a sibling of the file pointed by filePath.

for example if

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

Then it should be created under Test dir.

1 Comment

Consider using File.separator as opposed to those god-forsaken exited-backslashes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.