• 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

Read from file.

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
How do I read a text file until a particular character, and then stop reading?

The format of the text is a multiple choice question. The file has 300+ questions.

Sample format:

XYZ02 (C) [Ref. 00.a(b)(4)]
If some java programmers are vegetarians and some java programmer parties serve Pizza; are all programmers crazy?
A. Yes
B. No
C. Only C++ programmers
D. I think we've had too much snow this year

XYZ03 (B) [Ref. A.]
Assume your lab partner wears a 36DD...


I need to read in the first 5 characters and then skip the rest of the line; then read everything until the blank line. My own solution, which doesn't work worth a darn, is about 20 lines of code. There has got to be a better way. I'm thinking it'll involve BufferReader(), but I can't figure out how. (Yeah, I'm new at this.)

 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Otto,
I can think of a shorter way, but it's more complicated if you are new to Java.

The Scanner class lets you say what Pattern you want to look for. You can set what you want to look for via a regular expression. So read the first 5 characters and then look for a blank line. It's only a couple of lines, but it does assume you know regular expressions.

For optimizations on what you wrote, feel free to post your 20 lines.
 
Otto Hoel
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here it is. All 29 lines.
This is a modified example from one of my books, but not a very well documented one.

What I want to accomplish is to read the file until the first blank line is found. Which should be indicated by \n, I think.
Then print it to screen.



And here's how it all gets going:

 
Marshal
Posts: 79153
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you using the title "Console" for your class? There already is a Console class, so that is a potential course of confusion.
Why are you using input streams for text files, rather than BufferedReader and FileReader? FileInputStream and BufferedInputStream are not intended for text files.
Why are you using StringBuffer in a non-synchonised environment; you should be using StringBuilder.
If you use the methods which allow you to read a line, you can simply check the length of the line; it is returned as a String, so it has a method for finding its length. Remember a blank line would have length 0, whereas a line filled with spaces will have a greater length.
Simply seeking the new line (correctly called line-feed) character may be unreliable, since different operating systems use different combinations of characters to delimit the lines. If you do in fact find a line-feed character, that might mean you have reached the end of a line, rather than your having an empty line.
 
Otto Hoel
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell,
Thanks for taking the time. I suspected that I was heading down the wrong road, and now I know I was.

Campbell Ritchie wrote:Why are you using the title "Console" for your class? There already is a Console class, so that is a potential course of confusion.
Why are you using input streams for text files, rather than BufferedReader and FileReader? FileInputStream and BufferedInputStream are not intended for text files.
Why are you using StringBuffer in a non-synchonised environment; you should be using StringBuilder.


This all started out a something different that I tried to modify. Didn't work too well

Campbell Ritchie wrote:If you use the methods which allow you to read a line, you can simply check the length of the line; it is returned as a String, so it has a method for finding its length. Remember a blank line would have length 0, whereas a line filled with spaces will have a greater length.
Simply seeking the new line (correctly called line-feed) character may be unreliable, since different operating systems use different combinations of characters to delimit the lines. If you do in fact find a line-feed character, that might mean you have reached the end of a line, rather than your having an empty line.


I've read the docs on BufferedReader and I will start this over again using those.
Ok, back to learning...
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try the appropriate section in the Java™ Tutorials. Look for "buffered" in the headings.
 
reply
    Bookmark Topic Watch Topic
  • New Topic