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
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.
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 ]
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 ]
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.
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
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