tags:

views:

71

answers:

4

Hello All,

I am writing a code that collects all the credit card information of each buyer and storing it in a File. The format that I am using to store information in a file is:

[Credit Card Number] | [User Name] | [Street Address] | [Zipcode] | @  
[Credit Card Number] | [User Name] | [Street Address] | [Zipcode] | @  
.....

Now, Credit Card Number, <User Name>, <Street Address> and <Zipcode> are actual UI fields. I capture this information, use Field separator "|" and record separator "@" in my code so that I can access fields and records as and when I need. But this is just a logic I have and now I have begun to wonder if this would work, primarily due to the File API in Java. There is no method that lets me access specific fields in a record even after having a delimiter. I also checked RandomAccessFile API.

Can anyone please suggest me other possible approaches to this problem?

I want to differentiate between fields in a record and between various records so that I can perform operations like searching for a user with Credit Card Number = XXX and pull out the details.

+1  A: 

Either use a BufferedReader#readLine() to read it line by line in combination with various String methods like split(), or use java.util.Scanner, see also this tutorial. You can collect them in a List<Data> where Data is your own class containing the fields of a single row. This way you can access "various" records after reading the file.

As an alternative, you could also just store it all in a SQL database. This way you can easily select records using the SQL language. Or if it really needs to be a file, you can also consider using CSV or XML format. For those formats there are convenient API's available to treat them.

BalusC
Thank you so much for the input. Which APIs deal with creating CSV file?
darkie15
There are pretty much of them. [SuperCSV](http://supercsv.sourceforge.net/), [OpenCSV](http://opencsv.sourceforge.net/), [JavaCSV](http://sourceforge.net/projects/javacsv/), etc. You could also [homegrow](http://stackoverflow.com/questions/3225108/issues-converting-csv-to-xls-in-java-only-core-java-experience-needed-question/3225146#3225146) it.
BalusC
+5  A: 

Nope, what you're asking (random access into variable length fields) is generally not possible. There's ways to hack it on, but it'll be ugly and more trouble than it's worth. You are stuck with reading the entire file sequentially, scanning for the record you want. You probably want to look into a more structured approach to storing data, either through fixed-length records or (preferred) a database, say SQLite.

All that said, storing credit cards in a manner like this is a big no-no. The fact that you're asking this question indicates pretty surely that you should not be designing a system to handle credit cards. For extensive discussions on why this is a bad idea, you should read up on PCI compliance. Here's a starter link, and you should do some searching around on Wikipedia and StackOverflow for why this is a really bad idea.

Steven Schlansker
Thank you so much for the input !!
darkie15
+2  A: 

It's probably a student project.

I'd learn to use a database like MySQL before I implemented something like this.

But if you're dead-set on doing it with a file, yeah, just read it into memory. A Map would be very nice, with the credit card number being the hash key.

Tony Ennis
I'm thinking same way ;)
jjczopek
+1  A: 

If I were in Your shoes, I'd extend HashMap where Credit card number would be my key and wrap other details in a bean and store it as a value in the map.

I'd implement methods for loading data from file (reading line by line, parsing with regex) and saving data into file. Loading all data from file (caching) would give me the ability to modify freely data and save when necessary. I think it would increase performance, since there is no need to read the file all the time - just modifying memory object.

jjczopek