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?

Radu Murzea
  • 10,724
  • 10
  • 47
  • 69
Inglonias
  • 468
  • 1
  • 5
  • 18

3 Answers3

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");
Wormbo
  • 4,978
  • 2
  • 21
  • 41
  • `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. – Inglonias May 27 '12 at 21:17
  • 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! – Inglonias May 27 '12 at 21:19
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"
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • 1
    While that got rid of the error message, I don't see the file in the directory I used. Where is it? – Inglonias May 27 '12 at 20:54
  • Actually, on further study, this only removed the error message. I don't see the file – Inglonias May 27 '12 at 20:55
  • @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 – Óscar López May 27 '12 at 20:58
  • It's going to "C:\Users\Joshua (coolguy)\workspace\GUI Steganography MK II\Pictures;Decrypted.txt" which is the workspace path. What gives? – Inglonias May 27 '12 at 21:06
  • Change `File.pathSeparator` to `File.separatorChar` or `File.separator` (either one works), I guess you saw a previous edit of my answer before I got it right – Óscar López May 27 '12 at 21:08
  • Try this then: `filePath.getParentFile().getCanonicalPath() + File.separator + "Decrypted.txt"` – Óscar López May 27 '12 at 21:12
  • 1
    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. – Inglonias May 27 '12 at 21:13
  • `System.out.println()` is your best friend here. Print the paths at every attempt, until you get it right – Óscar López May 27 '12 at 21:15
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.

Jason Sturges
  • 15,855
  • 14
  • 59
  • 80
Dror
  • 1