• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

BufferedReader returning null

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

What could be the possible reasons for below code always returning null.
The path mentioned as file name is correct and the file is present at this path.



Thanks
 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make sure that the file is not empty otherwise readLine() method will return null as there is no content to read from the file.
 
Alice Huber
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Piyush. The file being read is not empty and it has around 30-40 lines of data.

Has this anything to do with file being in Binary/ASCII format or so ?
 
Piyush Mangal
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please try with some other file and see if the issue persists. There might be issue with the file itself. As long as \n , \r are encountered, readLine() will consider this as a line and give you a string represention of that line.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you copy/paste the actual code you're using?

In particular, this:


Does the actual code maybe have a stray semicolon?


If that's not the problem, then try File's methods like exists(), canRead(), and length() to learn more about what your code is seeing.

Are you perhaps swallowing an exception somewhere?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piyush Mangal wrote:Can you please try with some other file and see if the issue persists. There might be issue with the file itself. As long as \n , \r are encountered, readLine() will consider this as a line and give you a string represention of that line.



Even if there are not EOL characters, readLine will just read the entire file as one line.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you trying to read the same file with the same FileInputStream : fin before you are reading in
while((upldRec = bufin.readLine()) != null)
{
// control does not reach here.
}
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alice Huber wrote:Hi,

What could be the possible reasons for below code always returning null.
The path mentioned as file name is correct and the file is present at this path.



Thanks



Hi

I try to run the code you posted, it works fine and able to read the text file. The path i gave is forward slash and not backword slash. back slash is for escaping. If you found solution already let me/us know thankyou.
 
Alice Huber
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The issue is resolved now.
Actually same servlet was also being used to create the file , which down the line was being read.



BufferedOutputStream object was not closed at the point of reading the created file , hence the issue was observed.



Thanks
 
It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic