Java 将csv值提取到键值对中
我有一个有两列的CSV文件;A栏和B栏。我想提取ColumnA和ColumnB的值,以便稍后在for循环中解析它们 CSV示例:Java 将csv值提取到键值对中,java,csv,hashmap,key-value,Java,Csv,Hashmap,Key Value,我有一个有两列的CSV文件;A栏和B栏。我想提取ColumnA和ColumnB的值,以便稍后在for循环中解析它们 CSV示例: John, John Smith James, James Bond 我的程序在系统打印变量oldName时成功地提取了ColumnA。但是,我不知道如何提取ColumnB的值,我认为原因是我选择的数据结构,我不知道如何正确地适应我的需要 基本上,我希望我的for循环在第一次迭代中使用变量oldName=“John”和newName=“John Smith”,在第二
John, John Smith
James, James Bond
我的程序在系统打印变量oldName时成功地提取了ColumnA。但是,我不知道如何提取ColumnB的值,我认为原因是我选择的数据结构,我不知道如何正确地适应我的需要
基本上,我希望我的for循环在第一次迭代中使用变量oldName=“John”和newName=“John Smith”,在第二次迭代中使用变量oldName=“James”和newName=“James Bond”等
String fName=“TestFile.csv”;
把这条线串起来;
FileInputStream fis=新的FileInputStream(fName);
DataInputStream myInput=新的DataInputStream(fis);
字符串oldName;
字符串newName;
int i=0;
String[]GroupArray=新字符串[1000];
而((thisLine=myInput.readLine())!=null)
{
字符串strar[]=thisLine.split(“,”);
对于(int j=0;j您的思路是正确的,但是您有一些多余的代码。请注意,DataInputStream#readLine
已被弃用,因此我使用了BufferedReader#readLine
String filename = "/path/to/your/file";
File csv = new File(filename);
BufferedReader reader = new BufferedReader(new FileReader(csv));
String[] groupArray = new String[1000];
String line;
int i = 0;
while((line = reader.readLine()) != null) {
String[] split = line.split(",");
groupArray[i] = split[0].trim();
groupArray[i+1] = (split.length > 1) ? split[1].trim() : "";
i += 2;
}
String oldName, newName;
for(int j = 0; j < i; j += 2) {
oldName = groupArray[j];
newName = groupArray[j+1];
System.out.println ("User: "+ oldName +" has been renamed to: "+ newName +"<BR>");
}
输出:
User: John has been renamed to: John Smith<BR>
User: James has been renamed to: James Bond<BR>
用户:John已重命名为:John Smith
用户:詹姆斯已重命名为:詹姆斯·邦德
我认为您在不需要的情况下过度复杂了任务。以下示例应作为起点:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) throws IOException {
File file = new File("TestFile.csv");
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
List<String[]> allLines = new ArrayList<>();
while ((line = br.readLine()) != null) {
String[] splited = line.split("\\s*,\\s*");
allLines.add(splited);
}
for(String[] row : allLines){
String oldName = row[0];
String newName = row[1];
System.out.println ("User: "+ oldName +" has been renamed to: "+ newName );
}
}
}
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
公开课考试{
公共静态void main(字符串[]args)引发IOException{
File File=新文件(“TestFile.csv”);
BufferedReader br=新的BufferedReader(新文件读取器(文件));
弦线;
List allLines=new ArrayList();
而((line=br.readLine())!=null){
String[]splited=line.split(\\s*,\\s*);
所有行。添加(拆分);
}
对于(字符串[]行:所有行){
字符串oldName=行[0];
字符串newName=行[1];
System.out.println(“用户:“+oldName+”已重命名为:“+newName”);
}
}
}
User: John has been renamed to: John Smith<BR>
User: James has been renamed to: James Bond<BR>
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) throws IOException {
File file = new File("TestFile.csv");
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
List<String[]> allLines = new ArrayList<>();
while ((line = br.readLine()) != null) {
String[] splited = line.split("\\s*,\\s*");
allLines.add(splited);
}
for(String[] row : allLines){
String oldName = row[0];
String newName = row[1];
System.out.println ("User: "+ oldName +" has been renamed to: "+ newName );
}
}
}