tags:

views:

1770

answers:

4

I have a comma delimited configuration file. The empty lines are ignored and there need to be errors on invalid lines:

foo,bar

foo2, bar3

I want to read this file into a HashMap where the key (foo) is mapped with a value (bar).

What is the best way to do this?

+3  A: 

If you can use x = y instead of x, y then you can use the Properties class.

If you do need to have x, y then look at the java.util.Scanner you can set the delimiter to use as a separator (the javadoc shows examples of doing that).

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

class Main
{
    public static void main(final String[] argv)
    {
        final File file;

        file = new File(argv[0]);

        try
        {
            final Scanner scanner;

            scanner = new Scanner(file);

            while(scanner.hasNextLine())
            {
                if(scanner.hasNext(".*,"))
                {
                    String key;
                    final String value;

                    key = scanner.next(".*,").trim();

                    if(!(scanner.hasNext()))
                    {
                        // pick a better exception to throw
                        throw new Error("Missing value for key: " + key);
                    }

                    key   = key.substring(0, key.length() - 1);
                    value = scanner.next();

                    System.out.println("key = " + key + " value = " + value);
                }
            }
        }
        catch(final FileNotFoundException ex)
        {
            ex.printStackTrace();
        }
    }
}

and the Properties version (way simpler for the parsing, as there is none)

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Properties;

class Main
{
    public static void main(final String[] argv)
    {
        Reader reader;

        reader = null;

        try
        {
            final Properties properties;

            reader = new BufferedReader(
                            new FileReader(argv[0]));
            properties = new Properties();
            properties.load(reader);
            System.out.println(properties);
        }
        catch(final IOException ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            if(reader != null)
            {
                try
                {
                    reader.close();
                }
                catch(final IOException ex)
                {
                    ex.printStackTrace();
                }
            }
        }
    }
}
TofuBeer
TofuBeer, have you considered embrace Sun conventions for braces? :) :) http://java.sun.com/docs/codeconv/html/CodeConventions.doc6.html#15395
OscarRyz
@Oscar - Who cares?
Zerofiz
I used to put { on the line until the day I had to add a single line of code to a method that was about 10 pages long an 4 pages wide with a mix of brace styles (some missing, some on the same line and some on their own line). Took me the better part of a day.
TofuBeer
On that day I decided to code in such a way that nobody would ever have to go through what I had to do that day. A very defining day indeed :-)
TofuBeer
Oh yeah... the why I do it on their own line... it is easier to visually line it up that way (for me).
TofuBeer
+1  A: 

Your best bet is to use the java.util.Scanner class to read in the values in the config file, using the comma as a delimiter. Link here to the Javadoc:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html

Example would be:

Scanner sc = new Scanner(new File("thing.config"));
sc.useDelimiter(",");
while (sc.hasNext()) {
   String token = sc.next();
}
Jon
not going to work as it will also read \r\n as part of the string
A: 
try {
  BufferedReader cfgFile = new BufferedReader(new FileReader(new File("config.file")));
  String line = null;

  // Read the file line by line
  while ((line = cfgFile.readLine().trim()) != null) {

    // Ignore empty lines
    if (!rec.equals("")) {
       String [] fields = line.split(","); 
       String key = fields[0];
       String value = fields[1];  
       // TODO: Check for more than 2 fields
       // TODO: Add key, value pair to Hashmap  
    } // if
  } // while

  cfgFile.close();
} catch (IOException e) {
  System.out.println("Unexpected File IO Error");
}
StartClass0830
now I have to go code a Scanner version to see if it would be simpler... thanks :-/ :-)
TofuBeer
A: 

I personally use a jar by a guy named Stephen Ostermiller, which is his Labeled CSV parser. Here is some sample code.

LabeledCSVParser lcsvp = new LabeledCSVParser(
    new CSVParser(
        new StringReader(
            "Name,Phone\n" +
            "Stewart,212-555-3233\n" +
            "Cindy,212-555-8492\n"
        )
    )
);

while(lcsvp.getLine() != null){
    System.out.println(
        "Name: " + lcsvp.getValueByLabel("Name")
    );
    System.out.println(
        "Phone: " + lcsvp.getValueByLabel("Phone")
    );
}
Nick