• Post Reply Bookmark Topic Watch Topic
  • New Topic

not able to read file which exists

 
vidu mayur
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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
 
Adrian Yan
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
I also forgot to mention that I created the file using Notepad.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!