| Author |
Complete novice with writing to files and reading from them.
|
Paul Cox
Greenhorn
Joined: Mar 22, 2011
Posts: 2
|
|
Hey great forum btw, Im just starting out with java and im using netbeans so please bear with me
I currently have a little programme for searching and adding houses to and from a txt file.
My first form to add a new house contains all the relevant details to that house i.e address , driveway etc.
I have used the netbeans gui builder to create a form with textfields that should contain the above infomation.
It also has a submit button, at the moment the submit button doesnt do anything, I want it to take the infomation from the text fields and write them to a file on my HDD.
I also have another form which asks which type of house you are looking for , the user then adds the relevant info into textfields and clicks submit, it then should browse the text file created earlier and then return matching results.
What is the basic coding i need to add for writing to a file and the reading from it
If you need to see any of my existing code then say and i will post it
Thanks for having a look
Coxy
|
 |
Edwin Torres
Ranch Hand
Joined: Mar 19, 2011
Posts: 37
|
|
|
You need to decide on the layout of this text file. Is it one house per line? What are the field delimiters? Here's a tutorial on file I/O in Java: Reading, Writing, and Creating Files
|
Follow me: @CompGuyProbs
Read me: Freehold Computers Examiner
|
 |
Paul Cox
Greenhorn
Joined: Mar 22, 2011
Posts: 2
|
|
It would be
eg
10 acacia avenue
town
county/state
postal/zip code
Garden (yes/no)
conservatory (yes/no)
price
|
 |
Edwin Torres
Ranch Hand
Joined: Mar 19, 2011
Posts: 37
|
|
Paul Cox wrote:It would be...
That's for the fields. But how do you plan on delimiting each record (i.e. house)? For example, you could put a blank line between each house record. I'm not saying this is the best way to organize your text file. I'll assume you picked that layout for a reason (I would've chosen XML to store the data, but that's just my .02). But there may be better ways to store the info. For example:
street,town,county,state,zip code,garden,conservatory,price;street2,town2,county2,state2,zip code2,garden2,conservatory2,price2;street3,town3,county3,state3,zip code3,garden3,conservatory3,price3
In this sample data, there are three records (houses) delimited by a semi-colon(;). Each record's fields are delimited by a comma(,). If your file looked like this, then you could read in the entire string and parse it with StringTokenizer. You could then store the data in a data structure (e.g. HashMap) so you can operate on it.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
|
|
|
You ought not to use StringTokenizer; its documentation says why not.
|
 |
Edwin Torres
Ranch Hand
Joined: Mar 19, 2011
Posts: 37
|
|
Campbell Ritchie wrote:You ought not to use StringTokenizer; its documentation says why not.
Thanks. I did not know that.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
|
|
You're welcome
You learn a lot here; I have learned lots here myself.
|
 |
 |
|
|
subject: Complete novice with writing to files and reading from them.
|
|
|