• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Reading text file.

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! I'm trying to read a .txt file and put each line in a array of strings but it's not working! What is wrong with this code?
He stops in "while" loop.

 
Bartender
Posts: 2237
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. What to you mean by 'not working'? Remember ItDoesntWorkIsUseless.
Post the file you are trying to read.

I launched your program with text file below and it worked:However it kept looping when file was larger that 7 lines.

Unless you know that your file is exactly 7 lines long (and it will always be!), don't read data into fixed-length array of length 7.
Use a list instead.
And there is no need to call br.ready(). You could just use readLine method of BufferedReader. It returns null when you reach end of file.
Also, there is no need to check if file exists. FileReader constructor will throw an exception if it happens.
If you are using Java7 you could use try-with-resources.

Check out this code:
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It helps to be specific. WHICH while loop does it get stuck in? How do you know?

I'm guessing it is the first loop. And the obvious answer is it gets stuck in there because br.ready() keeps returning true.

But without more details, it's very hard to help you.
 
Robson Martinz
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much, Pawel Pawlowicz!
I'm sorry for use "it does not work". When i say this, i mean he is not working properly, like you said, he kept looping when file was larger that 7 lines. Now i know where i'm wrong. In examples i read in internet they say to me to use ready() and they say the same thing about check if file exists. Why they say this? Is a good practice of programming verify this things or it's really not necessary?
 
Paweł Baczyński
Bartender
Posts: 2237
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Checking if file exists is redundat. FileReader will do that anyway and throw an axception if there were any problem with a file. Any you have to handle that exception anyway (it is checked).
One more thing, just checking that a file (path) exists does not guarantee anything. You might not have read rights on it. It might be a directory... The file might got deleted between checking and using it.

About br.ready()... I admit using it is better that what I wrote before (less code to write). I never used it before

 
Ranch Hand
Posts: 121
12
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Robson Martinz wrote:In examples i read in internet they say to me to use ready()


This is a harmful advice! You should not use ready/available methods most of the time. These methods do not check for "end of file" condition. They check for "steam can be read without blocking". Steam may be not "ready" and still have some data to read. Following read may be blocking (but is not guaranteed to be blocking). Such misuse may lead to problem which occurs very rarely (some specific combination of data in the file, OS and Filesystem settings and pure luck) and are very hard to debug.
 
reply
    Bookmark Topic Watch Topic
  • New Topic