• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

trying to use a txt file like a database

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there an easy way to do this? Currently it looks like I will be doing this the messy way using a FileReader wrapped by a BufferedReader and going through it line by line, character by character with the String methods. My txt files will be uniform and predicatable so there will be no problem with the data.
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, are you using delimited lines?
For instance, usually if forced to use a flat file, many will use a tab, colon, or semi-colon delimited system. You then use a StringTokenizer for each line to pull out the data you need. Since the file is in a predictable format, this is probably the way to go.
 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have a StringTokenizer code example using a ',' deliminated flat file.
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So if your text file is fixed length, you could use the RandomAccessFile class.
You will have to know how the text is encoded in your file, because you will be reading the lines into an array of bytes. For example, is your file Ascii or Unicode?
This class will allow you to index the lines in your text file by ordinal position. If you want to find a line by its contents, you will have no choice but to read the entire file sequentially until you find the line you are looking for. But if you know that you want to read line #167, and that the records are 80 bytes long (counting the line seperator), your code might look similar to this (off the top of my head.. might not be exact)
 
Luther Adon
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The txt file can actually have delimiters or whatever needed put in if necessary. I created the file in notepad as a txt file and it has ANSI encoding. It looks like this:
Java 12 34 250
VB 12 6 100
DB2 10 6 0
asp.net 5 200
So there are end of lines to be read and spaces. Really, what I am trying to accomplish is the easiest possible way of reading and writing this txt file and putting the values into a gui. I am stuck on the most efficient way of getting to where I want without a database.
 
Michael Zalewski
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the file contains text of variable length lines, I don't see any good way of doing what you want. You probably have to read the file sequentially, and build some kind of collection from the contents.
Are you looking for the easiest way to find a particular row in the text file? Or are you looking for an easy way to parse each line into its component fields?
 
jason adam
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read each line using a BufferedReader so that you can use readLine() to get the String representation of each line in the file.
Pass that String to a StringTokenizer, and call
while( tokenizer.hasMoreTokens() )
then break apart the string with tokenizer.nextToken() and do whatever you want with that. For instance, you may build some sort of data object that holds these values, and have your GUI application get the values from that object.
Pretty simple, pretty effecient.
 
Luther Adon
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I am doing in the application is going to each line and reading or writing. So I will have to go to different lines at different times.
Thanks so much for your responses I will definitely look into StringTokenizer. As it sounds like what I want
 
jason adam
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may want to read all of your file into a Collection of objects, and use that collection for searching, removing, adding to, etc. When the application is done, or at regular intervals or when the user hits "Commit" or something similar, iterate through the Collection and write each object back to the file in the predefined format. You'd create a "new" database file to do this.
For a small database file, this is manageable, but if you expect the file to be huge, that could be a lot of I/O, so you'll want to look into the different file reader and writer classes for the best optimization.
 
Michael Zalewski
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the file is small enough, you might want to think of reading the whole file into a Collection when your application starts, then writing the Collection back out as a file when your application stops.
Otherwise, you will need some way to position your file pointer before writing. And if your Strings in the file are all different lenghts, that can be very difficult to do. One possibility is to make a file with fixed length records, where the fixed length is equal to one more than the largest possible string (one more because you need a line-feed at the end of each String). That's not the kind of file that is created with NOTEPAD.
It's not Java that makes it difficult. It's the definition of a text file.
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IMHO...
I think that without using a database, the easiest way would be to create an object consisting of all neccessary data and then writing the data out using DataOutputStream.
If you define each field as being a certain length and pad each field that is shorter then you will be able to read the object back one line at a time, any line you want using RandomAccessFile (java.io.*). Its fairly easy to do and it would eliminate the neccessity of lugging a collection that could be burdensom.
Good Luck!
 
Evil is afoot. But this tiny ad is just an ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic