• 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

Text file problem

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All. This is my first posting in Javaranch.

I was reading alot of posts over here and looks to me this forum is much better than java forums. people are polite and are willing to help a noob or advance person with their java query rather than busting them and giving google searches to do their own work.

I have been programming in java for 9 months. I really like it. The best OOP i have ever experience after C++. i hate pointers and data structures and i am glad with Java you dont need to know about data structures alot. where as in C++ most of the things are done with pointers. Anyway back to my question

I am having alot of trouble in file reading. like for example i have a file something like this :

Name Product price quantity
xyz Wheels $20.25 10
abc Jam $3.01 1

Now my question is how can i read each specific line in java. whats the strategy i had to use ? i know BufferedReader and string.split is used alot in file reading but i am really very confused. any hints or any example or any strategies in reading a text file and having the output also in a file or in a program will be great.

Secondly here is one more example
Name: Suleman Zia
Class: COSI455
Subject: Java
Grade: B

Name: Rahim
Class Abc
Subject: History
Grade: C

How can i read this text file and displaying the output for each specific person. once i can display then i can encapsulate my data and can call addition? The question remains the same. how can i read this text file data for each person? Hope you can understand now what i am trying to say.

One more question what is the difference between DataOutputStream and PrintOutputStream File file = new File("saloo.txt").

Too many questions. hope to get some help in understanding the file system.

Secondly what if i would like to store the data in a ArrayList or Set? i know how to do it in a regular program. Give me a program to write with HasSet or Map and i will make sure to complete that task easily may be not that easily but what if i would like to store all of the text data or may be the first row in an arraylist or vector then how can i do it?

Thanks for the help and i really appreciate it.
[ April 21, 2007: Message edited by: Suleman zia ]
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suleman,
Welcome to JavaRanch!

The BufferedReader class provides a readLine method to read in a file one line at a time. This is good if you want to do the processing as you read in a file. String.split is good if someone passes you a file as a String. There is a common pattern for reading in a file. See the getContents method in this example. You need to write the logic to store the appropriate lines in the appropriate fields of your object.

I can't find a class in the JavaDoc called PrintOutputStream.

> Secondly what if i would like to store the data in a ArrayList or Set? i know how to do it in a regular program.
The same way you would do so in a regular program. You create the objects based on the lines and then add them to the data structure.
 
Suleman zia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jeanne.

So basically i will be using String split method if i have a file something like this.

Suleman : 20 : Welcome

s.split(" : ");

and if i want to read the contents of the file then i will use the readLine method from the BufferedReader.

Let me go and look at that url the one which you posted.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An alternative to Jeanne Boyarsky's suggestion, whihc will only work in Java 5 or Java 6, is to use the Scanner class. It is described as "simple," but it is very effective. I think it actually takes a BufferedReader and uses that to get at the data. You can say
BTW: you may have problems with the delimieter you used for the String.split method. If you write " : " it will insiste on looking for space-colon-space.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I ought to have read your posting properly before going on about space-colon-space. Sorry. You will have to make sure that in the example you quite you always have " : " between successive entries.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a noob here, but it seems to me that this is a very simple question. I have only been programming in java for a couple months. If you just want to display information about a particular person or what not, why not make a two dimensional array containing all the information that you can just call the specific information or use a binary search or something like that in order to call it for displaying. In my class thats what the group making the school compiler is doing.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic