Okay, I am putting together this program and need some suggestions on the best way in Java to export data to a text file (2 dimensional array), that can later be recalled. The program is designed such that the user enters in how many hours they worked and how much they charged per hour. This information is displayed in a jtext area, but I also need it to write the information into an array. Since the data is dealing with hours and money, the data type needs to be numeric. It is also very critical that the array is a .txt file, not an .xml file. The program has a second function which is to allow it to call the information from the array. My question though is what is the best way to export the data from the jtext area to the .txt file. A friend of mine suggested using stringbuilder. I am just looking for the most efficient way.

January 5th, 2012, 08:25 PM

snowguy13

Re: Suggestions about best way to export/inport data to a .txt file

JTextArea's getText() method may be helpful for retrieving text. As for storing information in a .txt file, I'd look at the PrintWriter class. For reading data from a .txt file, you may want to check out the FileReader class. That would be appropriate if you are only using text and simple characters to store data. To be more specific (which I don't think you'll need) you may want to specify a InputStream (use one of its subclasses).

January 5th, 2012, 09:31 PM

elisha.java

Re: Suggestions about best way to export/inport data to a .txt file

This might not be part of the suggestions but instead of employing a 2 - dimensional arrays with two fields, wouldn't it be easier to use the Java Collection Framework's Maps? Just asking because I am trying to learn too. Thanks for the question. On another note, what scenarios will prompt you to use 2-dimensional arrays rather than either lists or Maps?

January 5th, 2012, 10:50 PM

copeg

Re: Suggestions about best way to export/inport data to a .txt file

Quote:

I am just looking for the most efficient way

Most efficient and best way are relative (and often alternatives work just fine). My recommendation: write the data to a file as you see fit and understand, and read said data as needed. If needed, make up your own file format (or use Serialization).

Once you have a working solution you will be able to determine if it is efficient, perhaps compare it to alternatives.

January 10th, 2012, 04:13 PM

jhy2a

Re: Suggestions about best way to export/inport data to a .txt file

snowguy13, Thanks for the suggestions. I will look into these although I don't think the InputStream is what I'm looking for either. Copeg, I would have included some code with it, however, this project is in the conceptual stage right now so there is no code written as of yet (other than a generic framework that has the text fields and the input button). I have a decent understanding of Java and I guess I should have been a little more clear when I posted my request. Instead of saying most efficient or best way, I should have just asked for some general ideas on ways to do it. The guys wanting this done really don't care, they just want it to take input from the JText fields and store them in the 2D array, then be able to recall that information at a later date. Thanks for the suggested reading.

October 31st, 2013, 01:35 AM

Vertisys

Re: Suggestions about best way to export/inport data to a .txt file

What type of database are you using?

October 31st, 2013, 10:48 AM

copeg

Re: Suggestions about best way to export/inport data to a .txt file

@Vertisys, please note that the date on this thread is over a year and a half old.