本文介绍了在Java中解析后如何处理csv.file数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我是java的新手,正在练习处理csv文件。我已经成功解析了csv文件,将其保存在数组中,并且摆脱了标题。 I am new to java and practicing processing csv file. I've already successfully parsed the csv file, saved it in an array, and got rid of the header. 文件如下:class, gender, age, bodyType, profession, pregnant, species, isPet, rolescenario:green, , , , , , ,person, female, 24, average , doctor , FALSE , , , passengeranimal, male , 4 , , FALSE , dog , true , pedestrian . .文件中没有字符串的列为空。像上面的物种和isPet一样。the column without a string is empty in the file. Like the species and isPet above.现在,我想遍历此数组并创建实例,这对我来说太复杂了。 Now, I want to iterate through this array and create the instance, and it's too complex for me to figure it out. 例如,我有一个带有构造函数的 Scenario 类:For example, I have a Scenario class with the constructor:Scenario(ArrayList<Character> passenger, ArrayList<Character> pedestrian, boolean greenOrRed),并且在创建场景实例之前,我必须使用两个不同子类 Person 的构造函数创建 Character 。 code>和动物。此外,将它们分为两个单独的组,分别是乘客和行人:and before creating scenario instance, I have to create Character by the constructor with two different subclasses Person and Animal. Further, arrange them into two separate groups, which are passenger and pedestrian:Person(Gender gender, int age, Profession profession, BodyType bodyType, boolean isPregnant)Animal(Gender gender, int age, BodyType bodyType, String species)我尝试过的操作:public void loadCsv() throws IOException { String csvFile = "config.csv"; String line = ""; String csvSplit = ","; try (BufferedReader csvReader = new BufferedReader(new FileReader(csvFile));) { String headerLine = csvReader.readLine(); while ((line = csvReader.readLine()) != null) { for (String token : data) { if (!token.isEmpty() && token.equals("scenario:green")) { scenario.setLegalCrossing(true); //pass to setter works //how to process with next token? } } } } catch (FileNotFoundException e) { e.printStackTrace(); return; }}我已经提到了帖子:如何轻松处理CSV文件到List< MyClass> 任何帮助或提示都受到高度赞赏。Any help or hint is highly appreciated. 编辑 顺序如下:首先,在场景中设置布尔值greenOrRed 类。//if the class equals to Scenario:greensetLegalCrossing(true);第二个是生成 Person 和动物 //if class equals to PersonPerson people =Person(Gender gender, int age, Profession profession, BodyType bodyType, boolean isPregnant)//if class equals to AnimalAnimal animal = Animal(Gender gender, int age, BodyType bodyType, String species)第三,将它们添加到超类数组中:Third, add them into superclass array:ArrayList<Character> passenger = new ArrayList<Character>();ArrayList<Character> pedestrian = new ArrayList<Character>();passenger.add(people); // if role equals passengerpedestrian.add(animal); // if role equals pedestrian最后,将乘客和行人添加到场景Finally, add passenger and pedestrian into Scenario constructor with the first step boolean value to form an instance.Scenario singleScenario = Scenario(passenger, pedestrian, legalCrossing) 推荐答案请查看以下示例,示例数据(省略了读取CSV文件和跳过标题的所有详细信息):Please review the following example of parsing your sample data (all details of reading the CSV file and skipping the header are omitted):List<String> csvContents = Arrays.asList(// "class, gender, age, bodyType, profession, pregnant, species, isPet, role", "scenario:green", "person, female, 24, average , doctor , FALSE , , , passenger", "animal, male , 4, , , FALSE , dog , true , pedestrian");Scenario scenario = null;List<Character> passengers = new ArrayList<>();List<Character> pedestrians = new ArrayList<>();for (String line : csvContents) { String[] data = line.split("\\s*,\\s*"); // split by comma and remove redundant spaces Character character = null; String clazz = data[0].toLowerCase(); if (clazz.startsWith("scenario")) { scenario = new Scenario(); scenario.setLegalCrossing(clazz.endsWith("green")); continue; } else if ("person".equals(clazz)) { character = new Person(data[1], Integer.parseInt(data[2]), data[3], data[8], data[4], Boolean.parseBoolean(data[5])); } else if ("animal".equals(clazz)) { character = new Animal(data[1], Integer.parseInt(data[2]), data[3], data[8], data[6], Boolean.parseBoolean(data[7])); } String role = data[8].toLowerCase(); if ("passenger".equals(role)) { if (null != character) passengers.add(character); } else if ("pedestrian".equals(role)) { if (null != character) pedestrians.add(character); }}System.out.println("passengers: " + passengers);System.out.println("pedestrians: " + pedestrians);if (null != scenario) { scenario.setPassengers(passengers); scenario.setPedestrians(pedestrians);}输出:passengers: [Person(super=Character(gender=female, age=24, bodyType=average, role=passenger), profession=doctor, isPregnant=false)]pedestrians: [Animal(super=Character(gender=male, age=4, bodyType=, role=pedestrian), species=dog, isPet=true)]您应该能够将此代码段作为参考,并根据需要进行修改。You should be able to use this code snippet as a reference and modify it according to your needs. 这篇关于在Java中解析后如何处理csv.file数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-27 15:35