简体   繁体   中英

Java read a text file 2 columns and store in array

我有一个文本文件,例如"01/01/2011,14.25" "02/01/2011,16.78""01/01/2011,14.25" "02/01/2011,16.78" 02/01 "01/01/2011,14.25" "02/01/2011,16.78" ,还有更多行。我想读取并存储在数组的第一列中作为字符串(稍后再作图) x ax)和第二列为double.Columns分隔符可以是逗号,分号,空格或制表符。我已经能够使用FileReader和BufferedReader读取单列文本文件,但如果我有两个或更多,则不知道怎么做columns.I使用Java和NetBeans.Thanks!

1 Read whole row using BufferedReader readLine() method/
2 split String by your delimiter and have string array

for example

String str = "01/01/2011,14.25";
String arr[] = str.split(",");
//arr[0]; will hold date part and arr[1] will hold double part you can parst it to double using Double.parseDouble(string);

Read each line using buffered reader and then do that:

HashMap<String, Double> values = new HashMap<String, Double>;

// read the line here
String line = ...;
String strings[] = line.split(",");
values.put(strings[0], Double.valueOf(strings[1]);

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