• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

not able to read file which exists

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I am new to java and I am trying to get the file name as an input from
the user. This file exists in my directory. Bu when it is checked with
f.canRead(), it returns false. What is the mistake in this program or is
it the way I give my input.
here is the code

thanks in advance
 
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you check if the file exists first? Do a f.exists() to see if the file is there. Remember, canRead() return true if and only if the file specified by this abstract pathname exists and can be read by the application; false otherwise.
 
vidu mayur
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I tried f.exists() and it is false. I am wondering why would it be false when the file is in the current directory.
I am totally confused about this. Please help.
 
vidu mayur
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also forgot to mention that I created the file using Notepad.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Typically this is because the JVM isn't looking in the same directory you're looking in - because the JVM is starting from a particular base directory which is typically whatever directory you were in when java.exe was invoked. (Which may not be obvious if you're invoking the JVM through some intermediary such as an IDE.) Try these lines:

[ December 06, 2003: Message edited by: Jim Yingst ]
 
vidu mayur
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jim for giving those commands. You are right about the IDE and it is using a different path. When I tried giving the absolute path as the path to the file, f.exists() still returns false. Am I doing something really silly? I am very new to this, so I am sorry to ask you so many questions
[ December 08, 2003: Message edited by: vidu mayur ]
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I tried giving the absolute path as the path to the file, f.exists() still returns false.
What is the path you're specifying? Can you show the code for this?
One possibility is if you use '\' in the path and neglect to escape it as '\\'.
Probably you need to look very carefully at the path; there may be a tiny error somewhere. You can also break the path up and test for existence of each part. E.g. if the path is
"c:\\foo\\bar\\baz\\test.txt"
try testing for the existence of each of these:
"c:\\"
"c:\\foo\\"
"c:\\foo\\bar\\"
"c:\\foo\\bar\\baz\\"
"c:\\foo\\bar\\baz\\test.txt"
This approach may help narrow down where the problem is.
 
vidu mayur
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim, I can't thank you enough for taking the time to post many replies.
I have one more question. I am trying to read an input from file as an object into a vector. The text file has a number and a location string after that separated by a space. The object is of SsnRecord type. I get NullPointerException in the for loop which means the input is not being read into the vector. What should I change in the code. thanks in advance
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know this may be a silly question Vidu, but your file does contain a Vector that is populated with SsnRecord objects, right? Seems like if you are getting a NullPointerException from that Vector object in the for loop definition, that means nothing is being returned from your ObjectInputStream
 
vidu mayur
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jason,u are right. The object input stream is not being read. The vector has to be populated with SsnRecord objects. Since the input is not being read from the file, it gives a null pointer exception
 
I'm THIS CLOSE to ruling the world! Right after reading this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic