• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading and using numerous files at once  RSS feed

 
Peter Spillane
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I realise this question may have been asked before, but i need help in the context if my code.

I have an application which lets the user name a file which contains their timetable and it is then saved in a timetables folder. e.g CorkToDublin.Txt.
Each timetable has it's own .Txt file.
Each timetable contains 5 values which are written from TextFields.

I am wondering how can i code it so all the files are read and then each one is printed under each other in a TextArea.



I know that the filename is usually put in between the brackets of the FileInputStream but how can i get it to read all the files from the timetables folder?

Also this is the code i use to print the values in the TextArea. If all the files are read, how could I print out each timetable underneath each other seperated by a line?



Both openStream() and displayTimetables() are in the same "Timetable" Class.

I hope i put up enough of my code for the help i need and i hope i explained it properly.
I could put up the whole class but it is pretty long so i thought this would be better.

Thanks in Advance,

Peter
 
Aaron Johnson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter,

I don't think I understand exactly what you want your program to do, but I think what you may be looking for could be the following:

The .list() or .listFiles() methods of a "File" object. You can create a File object pointing to the directory with your files in it and then list the files in the directory..

http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html

Is that what you're looking for?

-Aaron
http://www.weknowerrors.com
A free Wiki for error messages and computer problems.
 
Peter Spillane
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.

Does that just print the names of the files into the TextArea?

What i need the program to do is output what is inside each of the files underneath each other in the TextArea.
For Example.. CorkToDublin.Txt contains where the train is leaving and the times like this.

From To Begins Ends Every
Cork Dublin 09:00 20:00 60 Mins


The headings ("From" "To" "Begins" etc.) are already printed on the GUI with JLabels.
What i need to output is the "Cork" "Dublin" "09:00" etc. but then i want to have the program output all the other files that would be contained in the folder.
For Example if another timetable was created called CorkToKerry.Txt i would want what is inside that file to be printed under the previous file like this.

From To Begins Ends Every
Cork Dublin 09:00 20:00 60 Mins
Cork Kerry 10:00 18:00 30 Mins
 
Peter Spillane
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think (with a bit of tinkering) i have gotten the .list working but now the problem is when the timetables in the files are outputted to the TextArea only one appears. I think it might be printing each timetable on top of each other, overwriting it maybe?
This is the newer code.



 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well your iteration makes sense, but you do not pass anything to displayTimetables(), so that is not going to print anything. Your call to new at objGetTimetable = new ObjectInputStream(inputTimetable); may or may not need a new inputTimetable, assuming each file in the dir is an independent data unit then new on each one makes logical sense, but is not what I would think of as correct way to do it. Code that works and is efficient can be sought by considering the correct immplementation, which in this context probably sounds something like processNextFileIntoTimeTableListing or some name that makes sense to you. I use random decay from outer space because I can realate to it better ( old joke for the wiley amoung us ) but one call to new should be effective if objGetTimetable is passed to a function of that object that adds each files contents to some area.

You can either put them in a Collection if you will need to use them again or just print to JDisplay ( whatever the correct JFrame display area for your needs - JList can be an extremely powerful tool for displaying lists )

You need to pass the result of the function call: new ObjectInputStream(inputTimetable); to someplace, probably the displayTimetables(), but to stack them up first you probably want to accumulate them so it comes to code looking like.

while(nextFileAvailable()){
dumpOntoList(nextFile);
}
displayFiles();

// somewhere else
void displayFiles(){
while(listNotDone)print.nextLine();
 
Peter Spillane
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I managed to print out the Filenames using the JList and I kind of understood what you meant about calling the new ObjetcInputStream once but no matter what i've tried i cant seem to get the timetables to all print out in the TextArea.

The application is due tomorrow so theres not much i can do now.

Thanks for all the help anway,
Peter
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!