• Post Reply Bookmark Topic Watch Topic
  • New Topic

BufferedReader returning null

 
Alice Huber
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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
 
Piyush Mangal
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Rsudha Ramasamy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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.
}
 
Mezan Shareef
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!