简体   繁体   中英

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

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?

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.

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.

  • 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, 

尝试阅读所有内容并使用正则表达式将列表拆分为二维数组。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM