在Java中解析文件和检测模式 [英] Parsing file and detection of pattern in Java
问题描述
我的问题很简单,我解析一个CSV文件,一行一行 ,我想得到的列的值。
所使用的分隔符只是简单的;
,但我的文件可以有很多列,并且他们不会总是在相同的顺序。 / p>
例如对于我的.CSV文件:
time; columnA ; columnB,ColumnC
27-08-2013 14:43:00;这是一个文本;这个也是;在这里
并且我想能够获得所有的时间值,columnA ,columnB和columnC。
我使用 StringUtils.countMatches(input,;);
我开始尝试创建一个
String index [] [] = {{time,columnA,columnB,columnC} {}};
我的计划是在每个其他变量之前填充
;
的数字,以便我可以很容易地现在哪个结果代表哪个变量。 但现在我很抱歉...
如果你想让我显示更多的代码, 我可以。
希望有人可以帮助我!
您可以使用 split()
方法
例如:
扫描仪aScanner = ...
ArrayList< String> L = new ArrayList< String>();
while(aScanner.hasNextLine()){
L.add(aScanner.nextLine());
}
int rows = L.size();
String [] [] S = new String [rows] [];
for(int i = 0; i< rows; i ++){
S [i] = L.get(i).split(;);
}
My problem is quite simple, i am parsing a CSV file, line after line and i want to get the values of the columns.
The separators used are simply ";
", but my file can have quite a lot of columns, and they won't be always in the same order.
So as example for my .CSV file :
time;columnA;columnB,ColumnC
27-08-2013 14:43:00; this is a text; this too; same here
And i would like to be able to get all the values of time, columnA, columnB and columnC. What would be the easiest way?
I used StringUtils.countMatches(input, ";");
to get the number of separators i have.
I started trying to make a String index[][] = {{"time", "columnA", "columnB", "columnC"}{}};
And my plan was to fill this with the number of ";
" before each other variable, so that i could easily now which result stands for which variable.
But now i'm quite stuck...
If you want me to show more of my code, i can. Hope that someone can help me ! :)
(sorry for the poor english).
You can simply use split()
method
For instance:
Scanner aScanner = ...
ArrayList<String> L = new ArrayList<String>();
while(aScanner.hasNextLine()){
L.add(aScanner.nextLine());
}
int rows = L.size();
String[][] S = new String[rows][];
for (int i = 0; i < rows; i++) {
S[i] = L.get(i).split(";");
}
这篇关于在Java中解析文件和检测模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!