我有一个读取文件的程序,但我需要我的程序以某种方式读取文件。 以下是程序读取的文件的样子:
Sample ID Date Time Elem Conc (Samp) Samp Units
381220-1 11/17/2014 7:14:57 PM Y %
381220-1 11/17/2014 7:14:57 PM Ge %
381220-1 11/17/2014 7:14:57 PM Ge %
381220-1 11/17/2014 7:14:57 PM Al 0.008739442 mg/L
381220-1 11/17/2014 7:14:57 PM As 0.006080418 mg/L
381220-1 11/17/2014 7:14:57 PM B 0.016079117 mg/L
381220-1 11/17/2014 7:14:57 PM Ba -0.000985422 mg/L
381220-1 11/17/2014 7:14:57 PM Be -0.00025851 mg/L
381220-1 11/17/2014 7:14:57 PM Ca -0.012101454 mg/L
381220-1 11/17/2014 7:14:57 PM Cd -0.001148811 mg/L
381220-1 11/17/2014 7:14:57 PM Ce -0.045899969 mg/L
381220-2 11/17/2014 7:14:57 PM Al 2.007892442 mg/L
381220-2 11/17/2014 7:14:57 PM As 2.006080418 mg/L
我需要我的程序来获取示例381220-1的SampleID并将所有元素(Y,Ge,As等)与结果/值一起放在一行中,这样它应该是这样的。
381220-1, Al, 0.008739442, mg/L, As, 0.006080418, mg/L, etc.
381220-2, Al, 0.008739442, mg/L, As, 2.006080418, mg/L, etc.
我尝试了不同的方法来解决这个问题,但仍然无法得到它。 这就是我所做的。
try{
BufferedReader br = new BufferedReader(new FileReader("381220.csv"));
BufferedReader br2 = new BufferedReader(new FileReader("381220.csv"));
FileWriter fw = new FileWriter("file.txt", false);
PrintWriter pw = new PrintWriter(fw);
String line = null;
String line2 = null;
String[] value = null;
String[] value2 = null;
ArrayList<String> Queuelist = new ArrayList<String>();
ArrayList<String> lineoftxt = new ArrayList<String>();
while((line = br.readLine()) != null){
if(line.contains("Sample")){
continue;
}
value = line.split(",");
int k = 0;
for(String S : value){
//System.out.println("Value ["+k+"] = " + S);
k++;
}
Queuelist.add(value[0]+",");
}
for(String s : Queuelist){
//System.out.println(s);
}
br.close();
pw.close();
Queuelist = new ArrayList<String>(new LinkedHashSet<String>(Queuelist));
Set<String> toRetain = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
toRetain.addAll(Queuelist);
Set<String> set = new LinkedHashSet<String>(Queuelist);
set.retainAll(new LinkedHashSet<String>(toRetain));
Queuelist = new ArrayList<String>(set);
for(String s : Queuelist){
//System.out.println(s);
}
while((line2 = br2.readLine()) != null){
if(line2.contains("Sample") || line2.contains("Y") || line2.contains("Ge")){
continue;
}
value2 = line2.split(",");
int k = 0;
for(String S : value2){
//System.out.println("Value ["+k+"] = " + S);
k++;
}
lineoftxt.add(line2);
if(Queuelist.get(0).contains(lineoftxt.get(0))){
System.out.println(Queuelist.get(0)+","+lineoftxt.get(0));
}
}
for(String s : lineoftxt){
//System.out.println(s);
}
br2.close();
}
catch(IOException e){}
有任何建议可以指导我完成此操作。 谢谢!
答案 0 :(得分:0)
您可以为要分组的其余数据创建一个Whatever
和final String id;
的班级List<WhateverDetail> detailList
。使用Map<String, Whatever>
对每个ID的所有Whatever
进行分组,在ID已注册的情况下恢复现有的Whatever
实例,并填写WhateverDetail
的新实例以添加{ {1}}。