我正在使用文件阅读器来读取csv文件,csv文件的第二列是rgb值,例如rgb(255,255,255),但csv文件中的列用逗号分隔。如果我使用逗号分隔符,它将读作" rgb(255,"所以如何读取整个rgb值,代码粘贴在下面。谢谢!
FileReader reader = new FileReader(todoTaskFile);
BufferedReader in = new BufferedReader(reader);
int columnIndex = 1;
String line;
while ((line = in.readLine()) != null) {
if (line.trim().length() != 0) {
String[] dataFields = line.split(",");
//System.out.println(dataFields[0]+dataFields[1]);
if (!taskCount.containsKey(dataFields[columnIndex])) {
taskCount.put(dataFields[columnIndex], 1);
} else {
int oldCount = taskCount.get(dataFields[columnIndex]);
taskCount.put(dataFields[columnIndex],oldCount + 1);
}
}
答案 0 :(得分:1)
答案 1 :(得分:0)
line = "rgb(25,255,255)";
line = line.replace(")", "");
line = line.replace("rgb(", "");
String[] vals = line.split(",");
将vals中的值转换为Integer,然后就可以使用它们了。
答案 2 :(得分:0)
以下是如何执行此操作的方法:
Pattern RGB_PATTERN = Pattern.compile("rgb\\((\\d{1,3}),(\\d{1,3}),(\\d{1,3})\\)");
String line = "rgb(25,255,255)";
Matcher m = RGB_PATTERN.matcher(line);
if (m.find()) {
System.out.println(m.group(1));
System.out.println(m.group(2));
System.out.println(m.group(3));
}
这里
\\d{1,3} => match 1 to 3 length digit
(\\d{1,3}) => match 1 to 3 length digit and stored the match
虽然(
或)
是元字符,但我们必须逃避它。