【发布时间】:2020-10-08 08:16:31
【问题描述】:
我有一个包含两列的 CSV 文件; A 列和 B 列。我想提取 ColumnA 和 ColumnB 的两个值,以便稍后在 for 循环中解析它们。
CSV 示例:
John, John Smith
James, James Bond
当我对变量 oldName 进行系统打印时,我的程序成功提取了 ColumnA。但是,我不知道如何提取ColumnB的值,我认为原因是我选择的数据结构,我不知道如何正确适应我的需求?
基本上,我希望我的 for 循环变量 oldName = "John" 和 newName = "John Smith" 用于第一次迭代,第二次 oldName = "James" 和 newName = "James Bond" 等等。
String fName = "TestFile.csv";
String thisLine;
FileInputStream fis = new FileInputStream(fName);
DataInputStream myInput = new DataInputStream(fis);
String oldName;
String newName;
int i=0;
String[] GroupArray = new String[1000];
while ((thisLine = myInput.readLine()) != null)
{
String strar[] = thisLine.split(",");
for(int j=0;j<strar.length;j++)
{
if(i!=0)
{
GroupArray[i] = strar[j];
}
else
{
GroupArray[i] = strar[j];
}
}
i++;
}
try{
for (int l = 0; l < i; l++)
{
oldName = GroupArray[0];
newName = GroupArray[1];
out.println ("User: "+ oldName +" has been renamed to: "+ newName +"<BR>");
【问题讨论】:
标签: java csv hashmap key-value