By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,648 Members | 1,528 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,648 IT Pros & Developers. It's quick & easy.

Reading and Writing From a Delimited File

P: 2
I'm a relative newbie to Java, but I'm working on a project. I want to take the input from a Java GUI and store it in a text file. I also want to be able to take that same file and read it back to the GUI. The idea here is to, say, store a customer's purchases in a single file, and allow the user to view a particular customer's purchases. Let's say the input looks like this:

John 3-18-2012 Hammer
Bill 3-14-2012 Nail
John 2-8-2012 Broom
Randy 2-5-2012 Rake

I want the user to be able to pull up all of John's(or whoever) purchases. I also want to be able to add records to this file in case Randy ever comes back and buys something else.

I'm thinking that sorting the data according to the customer as it comes in would make it quicker in searching for the correct customer later. I'd like to load the file upon opening the "recordManagement" class, and an array needs to be in there somewhere.

I'm pretty sure I'm looking at FileWriter, BufferedWriter, PrintWriter, BufferedReader, and/or FileReader.

But, even looking at examples, I've having trouble even figuring out where to start for my particular project. Any help would be appreciated.
Mar 20 '12 #1
Share this Question
Share on Google+
5 Replies

P: 13,264
Why not store that information into a database instead of a file?
If you really want to use the file then see this article about how to read/write files:
Mar 21 '12 #2

P: 2
That's actually brilliant, except I have no working knowledge of how to connect my GUI input to the database. I've googled around, but I'm still relatively clueless. Would you provide a good link for what I'm trying to do, or maybe a simple example of where to start?
Mar 21 '12 #3

P: 51
Expand|Select|Wrap|Line Numbers
  1. //try this code is for mdb connection.
  2. import jaca.sql.*;
  3. public class test1
  4. {
  5. public static void main(String [] args) {
  6. String myDsn = "mdbTst";
  7. String dbUrl = "jdbc:odbc:"+myDsn;
  8. try {
  9. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  10. Connection con = DriverManager.getConnection(dbUrl,"","");
  11. Statement s = con.createStatement();
  12. s.execute("create table tst(fld_num integer)");
  13. s.execute("insert into fld_num values(123)");
  14. s.execute("select * from tst");
  16. ResultSet rs = s.getResultSet();
  17. if(rs!=null) {
  18. while( {
  19. System.out.println("Data from fld_num:"+rs.getString(1));
  20. }
  21. s.execute("drop table tst");
  22. s.close();
  23. con.close();
  24. }
  25. catch(Exception err) {
  26. System.out.println("Error:"+err);
  27. }
  28. }
  29. }
Mar 22 '12 #4

P: 51
oops, i mean import java.sql.*;

//you need to create System dsn then name this connection as mdbTst then browse your database..
//you can do this in your odbc manager, can be located in control pannel, administrative tools, ODBC Management.

//this code is just for giving some idea..
//even i dont have a knowledge how to connect java in other type of database.
// i saw this code via google. =)
Mar 22 '12 #5

P: 13,264
Rather prefer JDBC instead of ODBC.
There's a very good tutorial here:
Mar 22 '12 #6

Post your reply

Sign in to post your reply or Sign up for a free account.