Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to read a line and store the data!  RSS feed

 
sheng zhong
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example, the input text file is like this:

KWAFZRMYC 195.08 s 400 0
EDWWKTMYC 195.09 s 400 0
JSQPPHMYC 195.05 s 200 0
LQHTFMMYC 194.95 b 600 1
BTQFADMYC b 500 1
ZOOKNLMYC 195.07 b 400 1
TVZKDZMYC 195.01 b 300 1

First of all, how to read read this file line by line?

After read the file, I need to store them line by line and seperate by space, what data structure recommend, linked list?


Thanks in advance!
 
Gary Morris
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the best thing you can do is to get a copy of the API docs (if you do not already have them) and check out the classes that have to do with streams and files. There are many of them, and they can be combined in various ways to read files into arrays of chars, etc. Java tends to treat I/O as a stream, and you have so many different ways of doing it I won't even try to give any code example. As for parsing and storing the read-in file, there is the String class (as well as others like StringBuffer that simplify some operations), and this type of thing has been done SO many times you shouldn't have much trouble finding loads of examples to help you understand it all. For that matter, you could probably just search this site and find all you need, but I'm new here so I have not had a chance to check out everything there is. That may not help as much as you would like, but a little research should give you plenty to get ideas from, and solving this by using the existing Java classes and trying different things will give you much-needed experience rather than just having someone give you ready-made code!
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sheng,

An excellent place to start learning about File IO is the JavaRanch IO FAQ, which has lots of great links to places to learn more.

As to the best data structure to use to store your data once it is read in, that depends on what you need to do with it. What are you going to use it for? Do you need to sort it? To you need to iterate through the items? The answers to these questions will help you start to figure out the best Collection implementation to use.
 
sheng zhong
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!