Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

exists("..\\relativepath") not working?  RSS feed

 
Manuel Dun
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
im trying to read a text file that im going to make in notepad, but first i want to know if the file exists, so i tried this:

but the output is: "does not exists"
when i try to use a absolute path it works:

output: does exists

im using netbeans and the file was added clicking: file>new File>other>empty file; I can see the file on both the src\.. folder and build\classes\... folder.
it is probably the way im writing the relative path but i searched the web and tried a lot of ways and never got the "does exists" output.
 
Paul Clapham
Sheriff
Posts: 22468
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What did you intend that relative path to be relative to?

And what was the current working directory when you ran that code?

The answers to those two questions should be the same, but in your case they were different.
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch

Remember that .. means the parent directory of the current directory.
 
Manuel Dun
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought the relative path was relative to the .class files or the .java files(one of those)(considering that netbeans put the files there).
current working directory? :S dont know exactly what are you talking about :l
 
Manuel Dun
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i want "x.txt" file on the same directory as my .class file (or .java file i dont know which one xD) how do i write the path?.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66182
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no way to do that. What if, for example, the class comes from a jar file?

Before you go any further, you need to figure out and understand what "current directory' is all about.

And be advised that "that's what my IDE does" doesn't count when you try to run a program in the real world outside of the IDE.
 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you only need to read the contents of the file, add it as a resource and use Class' getResourceAsStream method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!