The moose likes Beginning Java and the fly likes Reading words from a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reading words from a file" Watch "Reading words from a file" New topic

Reading words from a file

Jay Dilla
Ranch Hand

Joined: Aug 12, 2004
Posts: 201
I need to read lines in a file word by word and spit it back out with something appended to it.
I know I can use Scanner to read lines, but there is not Scanner method to parse strings from the line. Is it possible to Scan lines in a loop then parse the words from the line in another loop?
What's the best method to implement this?
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
Well definitely yes, it is a standard cs exercise; but please give some idea of what the string looks like you want to parse and what splitting chars may be available to split the string on and so on. It is all very straight forward, just provide some more details so that we do not begin guessing and give time-wasting blunders to your effort.

"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Rob Spoor

Joined: Oct 27, 2005
Posts: 19649

I haven't really worked with it, but you could look at java.text.BreakIterator. It has a static method getWordInstance() that returns a BreakIterator that might be useful for you.

How To Ask Questions How To Answer Questions
Jay Dilla
Ranch Hand

Joined: Aug 12, 2004
Posts: 201
There are multiple words in the file , some on different lines.
For instance
The only delimiter would be space between each word. How would I find out if i'm at the end of a line and how do i write it into another file exactly like that , but with the ability to append things in between each word if i want.
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
Well, I patched together the following in the hopes of pulling some more program specifications.
A wish list would do just fine, even random thoughts or a plain:

"I have not thought about that."

You would really be suprised how many development paths this program can take, and in fact I will probably use the above short snippet in my program.

Use StringBuffer or StringBuilder to insert words into a line of text. readline returns a String, which will not let you insert characters in it.

[ Message edit: See the topic: each cutomer have multiple records ,they are identified by their calling number it is a few steps ahead of where your question is, but is the same question - also note my code has errors in it and you are supposed to read the code for ideas, not blindly template your work from the code ]
[ November 13, 2007: Message edited by: Nicholas Jordan ]
wood burning stoves
subject: Reading words from a file
Similar Threads
Scanning a text file
How to count and scan the number of characters in a line?
Removing the longest words
Lines lost during reading of a file in JTextPane
Reading lines of ints from file with a sentinel