8

I made a config file it has some following format

variableName = value
variableName = value
variableName = value

I know I can read file split them store them in variable. But I am looking for an easy way. For example I want to store variable names and its value in a file and I want when I read file it automatically restore variables and its values. ( I know how to do it in php and it is very easy but I am not java expert :( )

My second question is about following file read. I have a file that has rows and colums it can be CSV, for example

one,two,three
four,five,six
seven,eight,nine

I want to read it that it return whole column for example ( one four seven ) same for others. I don't want to use OpenCSV as its not csv oriented application just for one function.

EDITED:


Is it possible to write all variable name and its value and when I read file it automatically declare those variable and assign values?

user238384
  • 2,396
  • 10
  • 35
  • 36
  • 3
    You should ask your two questions as two questions posted separately :) – Tim Bender Feb 21 '10 at 03:20
  • Thanks, I will keep in mind. But I thought both questions are inter related so I can ask at the same time – user238384 Feb 21 '10 at 03:39
  • about the last question: you can assign values to the variables in the class by using reflection, not sure about declaring new variables dynamically. – jutky Feb 21 '10 at 04:58

4 Answers4

9

Use java.util.Properties to read in the key=value file. Here is a Sun tutorial.

As for the CSV, you can read in all the lines and use String#split() to break each line into an array of values.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Tim Bender
  • 20,112
  • 2
  • 49
  • 58
  • thanks for your reply. Is there any other way for CSV? For key=value please explain little bit more if you can point any tutorial, related to this thing please send me link is it possible to save variables and values and restore them? – user238384 Feb 21 '10 at 03:24
  • Thanks, It helped me for first question. But for CSV. I can split it first in lines then each line in array with for loops and split. But don't you think it is efficient way to do it?? That's why I asked if you can recommend any other way like you recommended properties :) – user238384 Feb 21 '10 at 03:38
  • Why is it inefficient? Is it because you need to write a loop? Uh well, have you benchmarked it? After all, there is no magic. There is just code. And you write it. – BalusC Feb 21 '10 at 03:40
  • Thanks BalusC, Yeah, I like to write code. But some time I write long code and I come to know that there is one simple function in java :) anyways, Thanks for your help. – user238384 Feb 21 '10 at 03:43
  • Be happy that there's `String#split()`, else you would hassle in more lines with `String#substring()`, `String#indexOf()`, `StringTokenizer`, `Scanner`, etc..etc.. – BalusC Feb 21 '10 at 03:47
  • This will not work for parsing a CSV file, which can have quoted values containing commas. – Lawrence Dol Feb 21 '10 at 04:07
  • @Software Monkey: The OP already disclaimered that it's not a real CSV file. Just a *comma separated format*. If one would be interested, here's a basic example how you should parse CSV in Java: http://stackoverflow.com/questions/2241915/regarding-java-string-manipulation/2241950#2241950 – BalusC Feb 21 '10 at 04:14
5

The Properties class will load your config file in the name=value format. Call the load method with a FileReader to the config file. The you can access any variable using the getProperty method.

Properties props = new Properties();
props.load(new FileReader(configFilePath));

String value = props.getProperty("name");

As for the CSV file, if all rows have the same number of values, you can read each line into an array using String.split(",") and assign it to a 2-d array. Then access a "column" by walking the 2-d array.

Miki
  • 2,493
  • 2
  • 27
  • 39
marklai
  • 2,030
  • 1
  • 14
  • 14
1
  • 1 question: check Serialization
  • 2 question: Please see this source code:

    class RowReader {
    private String path;
    private String columnSeparator;
    private List<String[]> rows;
    private int columnCount;
    public RowReader(String path, String columnSeparator) {
        this.path = path;
        this.columnSeparator = columnSeparator;
        this.rows = getRows();
        if (this.rows == null || this.rows.size() == 0)
            this.columnCount = 0;
        else
            this.columnCount = this.rows.get(0).length;
    }
    
    public List<String> getColumn(int i) {
        List<String> column = new ArrayList<String>();
        for (String[] row : rows) {
            column.add(row[i]);
        }
        return column;
    }
    
    public int getColumnCount() {
        return columnCount;
    }
    
    private List<String[]> getRows() {
        List<String[]> rows = new ArrayList<String[]>();
        try {
            FileInputStream fstream = new FileInputStream(path);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String line = null;
            while ((line = br.readLine()) != null) {
                rows.add(line.split(columnSeparator));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return rows;
    }}
    

Test class:

public class Program {
    public static void main(String[] args) {
        RowReader reader = new RowReader("j:\\test.txt", ",");
        for (int i = 0; i < reader.getColumnCount(); i++) {
            List<String> column = reader.getColumn(i);
            for (String c : column)
                System.out.print(c + ", ");
            System.out.println();
        }
    } 
}

Console output:

one, four, seven, 
two, five, eight, 
three, six, nine, 
Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
0

Try reading in everything and using a regular expression splitting the list in to a 2-d array.

WarmWaffles
  • 531
  • 5
  • 16