I want to get the values of the individual arrays from the list and utilize them
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import au.com.bytecode.opencsv.CSVReader;
public class ReadCSV {
public static void main(String[] args) {
String startFile = "/Users/ray/Downloads/hello.csv";
//String outFile = "./outData.xml";
try {
CSVReader reader = new CSVReader(new FileReader(startFile));
String[] line = null;
String[] header = reader.readNext();
List<List<String[]>> out = new ArrayList<List<String[]>>();
while((line = reader.readNext())!=null){
List<String[]> item = new ArrayList<String[]>();
for (int i = 0; i < header.length; i++) {
String[] keyVal = new String[2];
String string = header[i];
System.out.println("the value of the header : "+string);
String val = line[i];
System.out.println("the value of the field : "+val);
keyVal[0] = string;
keyVal[1] = val;
item.add(keyVal);
}
out.add(item);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I have a csv file of the following format:-
Keyword,AlternateKeywords---> these are the header fields
apple,banana
orange,ego kit
ego ce4,venus
demo,cat
I want the Arrays in form of:
array[] keyword={apple,orange,ego ce4,demo}
array[] banana={banana,ego kit,venus,cat}
I do not know how to get the data from the List 'out' and get the values and print the elements as above. It can be any number of headers and any number of elements for that particular column, the above csv is just an example.
Please help me if possible.
Take a look on your structure:
array[] keyword={apple,orange,ego ce4,demo}
array[] banana={banana,ego kit,venus,cat}
You dont need 3D like List<List<String[]>>
but only 2D List<List<String>>
Here is working code
public static void main(String[] args) {
String startFile = "C:\\workspacePrototype\\some.csv";
try {
CSVReader reader = new CSVReader(new FileReader(startFile));
String[] line = null;
String[] headers = reader.readNext();
List<List<String>> build = new ArrayList<List<String>>();
List<String> tempArr;
// generate headers
for(String header : headers){
tempArr = new ArrayList<String>();
tempArr.add(header);
build.add(tempArr);
}
// generate content
while((line = reader.readNext())!=null){
for (int i = 0; i < build.size(); i++) {
tempArr = build.get(i);
String val = line[i];
tempArr.add(val);
build.set(i, tempArr);
}
}
System.out.println(Arrays.asList(build));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Output:
[[[Keyword, apple, orange, ego ce4, demo], [AlternateKeywords, banana, ego kit, venus, cat]]]
[EDIT]
BTW, you can create some class like:
public class Column{
private String mName;
private List<String> mData;
// get/set
}
and instead
List<List<String>> build = new ArrayList<List<String>>();
use like:
List<Column> build = new ArrayList<Column>();
by this way Column
class should store column name to mName
and data to mData
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.