• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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
 
There are 10 kinds of people in this world. Those that understand binary get this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic