Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

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

 
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.
 
Marshal
Posts: 25194
64
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.
 
Marshal
Posts: 68061
258
  • 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?.
 
Marshal
Posts: 67363
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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.
 
Sheriff
Posts: 21865
105
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
The first person to drink cow's milk. That started off as a dare from this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!