Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File IO

 
Nevris
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I read in data from a data file and implement methods that are called in the data file?
IE)

would be a line in the data file, and it would call a method in the program that performed merge sort 20 times
[This message has been edited by Nevris (edited April 19, 2000).]
 
paul wheaton
Trailboss
Pie
Posts: 21385
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you talking about making your own programming language?
 
Nevris
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope, that's not what I'm talking about.
What I am talking about is ...
say I read in a line of input from a file, and that line said "PrintInOrder" for example .. then that would trigger something in the java program that would print out a tree that I had created ... and it would print it out using the print in order method of binary search trees ... but i have no clue how to do this .. so that's my question ... how would i do that?
 
Jesper Ottosson
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just use a FileInputStream to read in the lines, then compare them to the method names in a giant if-elseif statement, and call the proper methods inside this. I don't think there's an easy "magic" way of doing it, you have to do one if per method... well perhaps if you look through the reflection api (java.reflect), you may find something... the stuff there sure is magic.
 
Tim Reid
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a recent discussion of this issue along with examples using java reflection at the following:
http://forum.java.sun.com/forum?13@@.787c29c0/4
 
John Bateman
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about implementing an XML Document.
The DTD could have a tag called "MethodCaller" and then another tag with a bunch of text. You can then use reflection (as mentioned in previous mail), or some sort of custom XML parser, to analyze the DTD/XML doc and run the methods you need.
The problem is that you now have to make sure that the person creating the DTD/XML documents MUST know all about your classes.
I suggest XML here because if you DTD doesn't change and everyone writing these 'text/dat' files follows the dtd (which they have to) then you can be assured that the text/dat files can be correctly read by your program.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic