aspose file tools*
The moose likes Beginning Java and the fly likes How to read file line by line and word by word? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to read file line by line and word by word?" Watch "How to read file line by line and word by word?" New topic
Author

How to read file line by line and word by word?

Gary kwlai
Greenhorn

Joined: Dec 24, 2008
Posts: 12
Hi A beginner question.

I have a text file not in text format (.txt) but it does contain text and numbers.

I would like to know How to read a file line by line and store each word or number into an arraylist, then output them on a new file?

e.g. my text file call ( colorsANDnumbers.data )

Red 2 Blue 3 Yellow 4 Green 5
2 Red 3 Blue 4 Yellow 5 Green

Is that possible to be done with just one arraylist?

regards

Gaz
Bijj shar
Greenhorn

Joined: Mar 11, 2009
Posts: 13

[edit]Add code tags. CR[/edit]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
Please use the CODE button; I have edited that post so you can see how much better it looks.

Please don't simply give out code like that. Since it is pretty standard code, which could have been copied from the Java Tutorials, I think I shall let it stand. But (look at the Beginners' Forum contents page), where we explain that people learn a lot better if they work out things for themselves.

It doesn't actually work in its present condition, and I can see a potentially serious error, which I shall let you find for yourself . I shall also leave you to work out what people would do in Java5 or Java6.

*************************************************************************************************

Yes, you can put those entries into a single List<String>, but is that really appropriate? I suggest you go through the different interfaces in the Collections Framework and you might find something more appropriate for keeping colours and numbers.
Gary kwlai
Greenhorn

Joined: Dec 24, 2008
Posts: 12
Impressive

I have few things not quite understand from the code, what does line 21 and 34 actually doing??, because I have not cover WInputStreamReader and Iterator yet.

Also almost every codes thesedays has Try and Catch in them... are those required? does it prevent the program from crashing or halt when there is an error?

regards

Gaz
Bijj shar
Greenhorn

Joined: Mar 11, 2009
Posts: 13
Ritchie-

Thanks for letting me know to use Code Button. What error you are seeing in present condition please explain and user has asked about read and write data in file and he is reading data from existing file why you are giving him suggestion out of box.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Gary Lai wrote:Also almost every codes thesedays has Try and Catch in them... are those required? does it prevent the program from crashing or halt when there is an error?

regards


If the API throws any kind of exception that inherets from java.lang.Exception the compiler will force you to surround the code with a try/catch block. This allows you to catch any exceptions that are thrown and deal with them. Some API's throw RuntimeExceptions which don't require try/catch blocks but if they throw an exception, the application will just die.


GenRocket - Experts at Building Test Data
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
You are using the wrong classes for reading; you ought to use FileReader and BufferedReader because it is a text file. DataInputStreams are not designed for text files.
You are opening several Readers; I may be mistaken, but are you actually closing them? If you leave the Reader open, you may suffer a memory leak. That was what worried me. Anyway, when I tried your code, I couldn't get it to work; I got what appears to be a FileNotFoundException.

I would simply use the Scanner and Formatter classes for text files; they are much easier to use. Since they "consume" their Exceptions, you can get away without the try-catch.
Liron Meir
Greenhorn

Joined: Jan 20, 2012
Posts: 2
Hi this is not reading word by word. This is how it's done:
Scanner input = new Scanner(new File("liron.txt"));

while(input.hasNext()) {
String word = input.next();
}
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

Liron Meir wrote:Hi this is not reading word by word. This is how it's done:
Given that the question, and the last reply, was almost three years ago, i doubt the original poster is still waiting for an answer, or is terribly worried about it anymore.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Liron Meir
Greenhorn

Joined: Jan 20, 2012
Posts: 2
Yes, but if someone is looking for a solution to read word by word, this is not it.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
Welcome to the Ranch

That is what I was hinting at when I mentioned Scanner. We prefer not to give the full solution and it says the following on this forum’s title page:
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to read file line by line and word by word?