How to read key = value file and how to split comma separated string?

I created a configuration file that has the following format

variableName = value variableName = value variableName = value 

I know that I can read the files that they share by storing them in a variable. But I am looking for an easy way. For example, I want to save the names of variables and their value in a file, and I want, when I read the file, it will automatically restore the variables and their values. (I know how to do this in php, and it is very easy, but I am not a java expert :()

My second question concerns the next reading of the file. I have a file that has rows and columns it could be a CSV, for example

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

I want to read it so that it returns a whole column, for example (one four seven), for others. I do not want to use OpenCSV as a non-csv-oriented application for only one function.

Edition:


Is it possible to write the whole name of a variable and its value, and when I read the file, it will automatically declare this variable and assign values?

+8
java
source share
4 answers

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

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

+9
source share

The Properties class will load your configuration file in the format name = value. Call the download method with FileReader for the configuration file. 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 lines have the same number of values, you can read each line into an array using String.split (","), and assign it to a two-dimensional array. Then get access to the โ€œcolumnโ€ by going through the 2nd array.

+4
source share
  • 1 question: check Serialization
  • 2 question: 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; }} 

Testing 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, 
+1
source share

Try to read in everything and using the regular expression dividing the list into the 2nd array.

0
source share

All Articles