0

I'm trying to write in a file with a path like this:

D:\abcd\efgh\..\ijkl\file.txt

So I have an File object with such a path, but in the line

FileOutputStream fos = new FileOutputStream(f);

I get this:

java.io.FileNotFoundException: ..\ijkl\file.txt (The system cannot find the path specified)

Does anybody know what's wrong here? Is there a possibility to resolve the path in an absolute path?

Initialisation of the File object:

File f = new File(strImagePath);

strImagePath is built out of different Strings and looks exactly like the path shown above.

Thanks!

S.Pro
  • 633
  • 2
  • 12
  • 23

1 Answers1

0

According to your code java try to access the folder D:\abcd\ijkl\file.txt as you have place ..\ijkl\file.txt but in your system doesnot have any file on that path. Thus you are getting the error.
Edit: can you please, try using D:\\abcd\\efgh\\../efgh\\ijkl\\file.txt

Pradip
  • 3,189
  • 3
  • 22
  • 27
  • file.txt does exist in the ijkl directory. So the problem isn't a missing file. – S.Pro Sep 23 '13 at 06:38
  • is file.txt present inside ijkl? – Pradip Sep 23 '13 at 06:43
  • yes, the program should delete already existing files. The files are there and the path is right. It seems that java is having troubles resolving the ".." in the path. – S.Pro Sep 23 '13 at 06:56
  • Yes that's I mention above. when java get `../` in file path it take it as command to one directory level up. are you got my point, if not please let me know. – Pradip Sep 23 '13 at 06:59
  • I don't really get, what you mean. Java is running in D:\abcd\efgh and the files I want to delete are in D:\abcd\ijkl so I thought that I have to use ".." to get to the parent directory. – S.Pro Sep 23 '13 at 07:06
  • As I understood, You are getting the error `java.io.FileNotFoundException` as file does not exist in that location. FileOutputStream does not create file if not exist. Pls check the java doc : http://docs.oracle.com/javase/6/docs/api/java/io/FileOutputStream.html#FileOutputStream%28java.lang.String,%20boolean%29 – Pradip Sep 23 '13 at 07:16
  • The files are existing in this directory. The problem seems to be, that java isn't able to find them with ".." in the path. File.exists() is also false, but the files are definitly at this location. – S.Pro Sep 23 '13 at 07:25
  • Man, please try \\ instead of \ – Pradip Sep 23 '13 at 07:28
  • I corrected the path-String as you mentioned, but after the initialisation of "f" the .getAbsolutePath-Methode returns a path with "..\" again. – S.Pro Sep 23 '13 at 09:28
  • Man, if you are still getting this error means, file is not there or wrong directory structure you are using. Pls, take a look on this link how to use fileOutputStream http://www.mkyong.com/java/how-to-write-to-file-in-java-fileoutputstream-example/ . Keep your mind cool. you'll surely find it. – Pradip Sep 23 '13 at 09:57