下面是我用于读取 .csv 文件并打印输出的程序,但我无法实现输入和输出中提到的以下功能。如果用户 ID 在 2 个不同的组中可用,我基本上希望单独列出用户 ID。
我的输入是这样的:
User ID Group
ABC Group1
DEF Group2
ABC Group3
GHI Group4
由于 ABC 在 2 个不同的组中可用,我需要输出为:
ABC Group1
ABC Group3
你能帮忙吗?
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.StringTokenizer;
public class dumpRead {
public static void main(String[] args) {
try {
//csv file containing data
String strFile = "C:/Tracker/read/data.csv";
//create BufferedReader to read csv file
BufferedReader br = new BufferedReader( new FileReader(strFile));
String strLine = "";
StringTokenizer st = null;
int lineNumber = 0, tokenNumber = 0;
//read comma separated file line by line
while( (strLine = br.readLine()) != null){
lineNumber++;
//break comma separated line using ","
st = new StringTokenizer(strLine, ",");
while(st.hasMoreTokens()){
//display csv values
tokenNumber++;
System.out.println("Token # " + tokenNumber
+ ", Token : "+ st.nextToken());
}
//reset token number
tokenNumber = 0;
}
}
catch(Exception e) {
System.out.println("Exception while reading csv file: " + e);
}
}
}