Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read key=value file and how to split a comma-separated String?

Tags:

java

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?

like image 704
user238384 Avatar asked Feb 21 '10 03:02

user238384


3 Answers

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.

like image 193
Tim Bender Avatar answered Nov 17 '22 05:11

Tim Bender


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.

like image 20
marklai Avatar answered Nov 17 '22 06:11

marklai


  • 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, 
like image 1
Michał Ziober Avatar answered Nov 17 '22 05:11

Michał Ziober