Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Gary kwlai
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[edit]Add code tags. CR[/edit]
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12145
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Liron Meir
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but if someone is looking for a solution to read word by word, this is not it.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic