开发者

Read config file with arrays in java

开发者 https://www.devze.com 2023-03-13 23:27 出处:网络
I\'ve made a java application that manipulates a few matrices it gets as input, and then outputs some text. Currently the matrices are hardcoded in my application (for testing), but recompiling is qui

I've made a java application that manipulates a few matrices it gets as input, and then outputs some text. Currently the matrices are hardcoded in my application (for testing), but recompiling is quite silly for a new matrix.

So I'm looking for a config file format that would be suited for inputting matrices.

I know I can read ini files easily ( What is the easiest way to parse an INI file in Java?), but then parsing a thing like [ [ 1, 2, 3], [4, 5, 6]开发者_开发问答 ] to a matrix would probably require annoying string manipulation.

Does anyone know of a nice method to read in these matrices?


You can use JSON as the data interchange format. Take a look at JSON in Java.


how about CSV files?

your input could look something like

1;2;3
4;5;6

There are also some already built CSV parser around... like http://commons.apache.org/sandbox/csv/


I second on csv files. However, it can also be handy to use property files, one line representing one matrix. Something like this:

property.test="A,B,C;A1,B1,C1;A2,B2,C2";

And then using java.util.Properties to read the file.

Parsing: The trick here is in choosing separators, they must not be present in the entries themselves.

String str = "A,B,C;A1,B1,C1;A2,B2,C2";
String[] rows = str.split(";");
if(rows != null && rows.length > 0){
   String[][] result = new String[rows.length][];
   for(int row = 0; row < rows.length; row++){
       result[row] = rows[row].split(",");      
   }
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消