views:

136

answers:

2

I run a small online gaming community and deal with a database of accounts.

The setup is this:

Folder named Accounts Inside the Accounts directory, there is 200,000+ text files organized by player name. Access to this folder manually is a pain because of the needed RAM to get in and search files. I find this very inconvenient.

I access this directory to send password reminders or for highscores on who has been playing the longest.

Here is an example of an account file. This file is named Falcon.txt

[ACCOUNT]
character-username = Falcon
character-password = falconpassword

[INFO]
character-coordx = 3252
character-coordy = 3432
character-active = yes
character-ismember = 1
character-messages = 5
character-lastconnection = [removed]
character-lastlogin = 2009-11-29
character-energy = 100
character-gametime = 193
character-gamecount = 183

[EQUIPMENT]
character-equip = 0 4724    0 
character-equip = 1 1052    0 
character-equip = 2 6585    0 
character-equip = 3 4151    0 
character-equip = 4 4720    0 
character-equip = 5 1215    0 
character-equip = 6 -1  0 
character-equip = 7 4722    0 
character-equip = 8 -1  0 
character-equip = 9 775 0 
character-equip = 10    1837    0 
character-equip = 11    -1  0 
character-equip = 12    6735    0 
character-equip = 13    -1  0 

[APPEARANCE]
character-look = 0  1
character-look = 1  1
character-look = 2  2
character-look = 3  3
character-look = 4  5
character-look = 5  2

[STATS]
character-skill = 0 1   0
character-skill = 1 1   0
character-skill = 2 1   0
character-skill = 3 1   0
character-skill = 4 1   0
character-skill = 5 1   0
character-skill = 6 1   0
character-skill = 7 1   0
character-skill = 8 1   0
character-skill = 9 1   0
character-skill = 10    1   0
character-skill = 11    1   0
character-skill = 12    1   0
character-skill = 13    1   0
character-skill = 14    1   0
character-skill = 15    1   0
character-skill = 16    1   0
character-skill = 17    1   0
character-skill = 18    1   0
character-skill = 19    1   0
character-skill = 20    1   0

[ITEMS]

[BANK]

[FRIENDS]

[IGNORES]

[END]

There is a huge database of these and search through the directory in the files for values.

Values I mean by item ID's or IP addresses to find and track other accounts.

However I have a new problem and my development for this is crashing.

As you can see in the file the lines are organized by tabs.

character-equip = 0    4724    1 

If I put the value 4724 in my search application, I want it to print out the value 1 tab to the right of the found search result. I want it to print out the value for the found results only, not extra results.

So the search could look like this:

1 "Enter item to find:"
2 "Enter item to find: 4724"
3 "Account Falcon.txt has 1!"
press any key to continue...

Or if there was more quantity of that equipped item

character-equip = 5    1239    102

1. "Enter item to find:"
2. "Enter item to find: 1239"
3. "Account Falcon2.txt has 102!"
press any key to continue...

I simply want to input an item ID, and have it display the value after the found value. The white space is a tab. I have tried doing this and the only successful way of getting any result is to put a tab in between the search term. So if I want to find item 1239 id type this in the cmd line:

Enter item to find:<tab>1239<tab>

It would then search for that and will display the accounts with that item in it. However I still have to individually open up the accounts to find out the quantity of that item. I want the search results to display the quantity of the item if the value is found. However if the value is a quantity and it trys to search one tab over, I want it to either skip it or say zero.

This is what I mean.

character-equip = 0    1024    1239

Enter item to find: 1239

If it hits this account I want to make the search results display a zero if it cannot tab over or view any values in the white space. So it will display as null or zero Account Falcon3.txt has null!

or

Account Falcon3.txt has 0!

I've attempted to do this but I am unsure how to achieve this.

Here is my code.

import java.io.*;
import java.util.*;

public class ItemDatabase {

    public static void main(String args[]) {

        System.out.print("Enter item to find: ");
        Scanner sc = new Scanner(System.in);
        find(sc.nextLine());
    }

    public static void find(String delim) {
        File dir = new File("accounts");
        if (dir.exists()) {
            String read;
            try {
                File files[] = dir.listFiles();
                for (int i = 0; i < files.length; i++) {
                    File loaded = files[i];
                    if (loaded.getName().endsWith(".txt")) {
                        BufferedReader in = new BufferedReader(new FileReader(loaded));
                        StringBuffer load = new StringBuffer();
                        while ((read = in.readLine()) != null) {
                            load.append(read + "\n");
                        }
                        String delimiter[] = new String(load).split(delim);

                        if(delimiter.length > 1) {
                        System.out.println("Account " + loaded.getName() + "has " + delimiter[1] + "!");
                        }
                    }
                }  
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("error: dir wasn't found!");
        }
    }
}

Thanks guys I hope you can help me.

+7  A: 

This is simply crying out for a database. If your entire back end is running in a single java process I'd recommend going with something like Apache Derby or H2.

If you'd rather not move to a database, and the main problem is the act of listing all the entries in a single directory, could you split it into a heirarchy. Your Falcon account could then be located in F/FA/Falcon.txt - each directory would then contain a more manageable number of files.

developmentalinsanity
As I read and scrolled down I was shouting, "Use a database!" Then I saw your comment! Awesome! +1
LES2
Thank you for the suggestion I will try to create a SQL database for this and get rid of text files.
Surfer Kyle
A: 

Aside from the need for a database, you could probably implement your solution more intuitively and easily using commandline utilities such as find, grep, etc. or a text-processing language such as perl.

Maybe

grep '[0-9]+\t[0-9]+\t1239' Account_Falcon3.txt

Would return

character-equip = 0    1024    1239

You could then easily see that the value is 0 for that item.

I cannot emphasize enough the need to not write a Java program to do this - you won't do as good a job as the authors of the standard shell utilities. Let's face it, the fact that you are asking this question indicates that you are a newb! :) (We are all newbs depending on the topic).

LES2
Wow thank you haha I am a noob for sure! You cheered me up =)Thank you I was really not wanting to setup a DB and this will work perfect! Thank you friend hahaha really thank you for making me day. =)
Surfer Kyle