Reading and Writing From a Delimited File

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mfay103010
    New Member
    • Mar 2012
    • 2

    Reading and Writing From a Delimited File

    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 "recordManageme nt" 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.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    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: http://bytes.com/topic/java/insights...-writing-files

    Comment

    • mfay103010
      New Member
      • Mar 2012
      • 2

      #3
      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?

      Comment

      • rekedtechie
        New Member
        • Feb 2012
        • 51

        #4
        Code:
        //try this code is for mdb connection.
        import jaca.sql.*;
        public class test1
        {
        public static void main(String [] args) {
        String myDsn = "mdbTst";
        String dbUrl = "jdbc:odbc:"+myDsn;
        try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con = DriverManager.getConnection(dbUrl,"","");
        Statement s = con.createStatement();
        s.execute("create table tst(fld_num integer)");
        s.execute("insert into fld_num values(123)");
        s.execute("select * from tst");
        
        ResultSet rs = s.getResultSet();
        if(rs!=null) {
        while(rs.next()) {
        System.out.println("Data from fld_num:"+rs.getString(1));
        }
        s.execute("drop table tst");
        s.close();
        con.close();
        }
        catch(Exception err) {
        System.out.println("Error:"+err);
        }
        }
        }

        Comment

        • rekedtechie
          New Member
          • Feb 2012
          • 51

          #5
          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. =)

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Rather prefer JDBC instead of ODBC.
            There's a very good tutorial here:http://docs.oracle.com/javase/tutori...ics/index.html

            Comment

            Working...