我正在try 做一个从CSV文件中获取数据并将其存储在数据库中的应用程序. 所以我在我的pom.xml中添加了依赖项

<dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-csv</artifactId>
            <version>1.8</version>
        </dependency>
<dependencies>

But when I try to import csv in my project it can't be found as you can see in imageenter image description here all csv classes are signed red

My code (Helper Class):个 所有CSV类别都标为红色

package com.example.FlorenceConsulting.Helper;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import com.example.FlorenceConsulting.Model.User;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.springframework.web.multipart.MultipartFile;
public class CSVHelper {
    public static String TYPE = "text/csv";
    static String[] HEADERs = { "nome", "cognome", "mail", "indirizzo" };

    public static boolean hasCSVFormat(MultipartFile file) {

        if (!TYPE.equals(file.getContentType())) {
            return false;
        }

        return true;
    }

    public static List<User> csvToTutorials(InputStream is) {
        try (BufferedReader fileReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
             CSVParser csvParser = new CSVParser(fileReader,
                     CSVFormat.DEFAULT.withFirstRecordAsHeader().withIgnoreHeaderCase().withTrim());) {

            List<User> users = new ArrayList<User>();

            Iterable<CSVRecord> csvRecords = csvParser.getRecords();

            for (CSVRecord csvRecord : csvRecords) {
                User user = new User(
                        Long.parseLong(csvRecord.get("nome")),
                        csvRecord.get("cognome"),
                        csvRecord.get("mail"),
                        csvRecord.get("indirizzo")
                );

                users.add(user);
            }

            return users;
        } catch (IOException e) {
            throw new RuntimeException("fail to parse CSV file: " + e.getMessage());
        }
    }
}

Maven Dependency tree (mvn dependency:tree)

[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------< com.example:FlorenceConsulting >-------------------
[INFO] Building FlorenceConsulting 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:3.5.0:tree (default-cli) @ FlorenceConsulting ---
[INFO] com.example:FlorenceConsulting:jar:0.0.1-SNAPSHOT
[INFO] +- org.springframework.boot:spring-boot-starter-data-jpa:jar:3.1.5:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-aop:jar:3.1.5:compile
[INFO] |  |  +- org.springframework:spring-aop:jar:6.0.13:compile
[INFO] |  |  \- org.aspectj:aspectjweaver:jar:1.9.20:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-jdbc:jar:3.1.5:compile
[INFO] |  |  +- com.zaxxer:HikariCP:jar:5.0.1:compile
[INFO] |  |  \- org.springframework:spring-jdbc:jar:6.0.13:compile
[INFO] |  +- org.hibernate.orm:hibernate-core:jar:6.2.13.Final:compile
[INFO] |  |  +- jakarta.persistence:jakarta.persistence-api:jar:3.1.0:compile
[INFO] |  |  +- jakarta.transaction:jakarta.transaction-api:jar:2.0.1:compile
[INFO] |  |  +- org.jboss.logging:jboss-logging:jar:3.5.3.Final:runtime
[INFO] |  |  +- org.hibernate.common:hibernate-commons-annotations:jar:6.0.6.Final:runtime
[INFO] |  |  +- io.smallrye:jandex:jar:3.0.5:runtime
[INFO] |  |  +- com.fasterxml:classmate:jar:1.5.1:runtime
[INFO] |  |  +- net.bytebuddy:byte-buddy:jar:1.14.9:runtime
[INFO] |  |  +- org.glassfish.jaxb:jaxb-runtime:jar:4.0.3:runtime
[INFO] |  |  |  \- org.glassfish.jaxb:jaxb-core:jar:4.0.3:runtime
[INFO] |  |  |     +- org.eclipse.angus:angus-activation:jar:2.0.1:runtime
[INFO] |  |  |     +- org.glassfish.jaxb:txw2:jar:4.0.3:runtime
[INFO] |  |  |     \- com.sun.istack:istack-commons-runtime:jar:4.1.2:runtime
[INFO] |  |  +- jakarta.inject:jakarta.inject-api:jar:2.0.1:runtime
[INFO] |  |  \- org.antlr:antlr4-runtime:jar:4.10.1:compile
[INFO] |  +- org.springframework.data:spring-data-jpa:jar:3.1.5:compile
[INFO] |  |  +- org.springframework.data:spring-data-commons:jar:3.1.5:compile
[INFO] |  |  +- org.springframework:spring-orm:jar:6.0.13:compile
[INFO] |  |  +- org.springframework:spring-context:jar:6.0.13:compile
[INFO] |  |  +- org.springframework:spring-tx:jar:6.0.13:compile
[INFO] |  |  +- org.springframework:spring-beans:jar:6.0.13:compile
[INFO] |  |  +- jakarta.annotation:jakarta.annotation-api:jar:2.1.1:compile
[INFO] |  |  \- org.slf4j:slf4j-api:jar:2.0.9:compile
[INFO] |  \- org.springframework:spring-aspects:jar:6.0.13:compile
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:3.1.5:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:3.1.5:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-autoconfigure:jar:3.1.5:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:3.1.5:compile
[INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.4.11:compile
[INFO] |  |  |  |  \- ch.qos.logback:logback-core:jar:1.4.11:compile
[INFO] |  |  |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.20.0:compile
[INFO] |  |  |  |  \- org.apache.logging.log4j:log4j-api:jar:2.20.0:compile
[INFO] |  |  |  \- org.slf4j:jul-to-slf4j:jar:2.0.9:compile
[INFO] |  |  \- org.yaml:snakeyaml:jar:1.33:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-json:jar:3.1.5:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.15.3:compile
[INFO] |  |  \- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.15.3:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-tomcat:jar:3.1.5:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:10.1.15:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:10.1.15:compile
[INFO] |  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:10.1.15:compile
[INFO] |  +- org.springframework:spring-web:jar:6.0.13:compile
[INFO] |  |  \- io.micrometer:micrometer-observation:jar:1.11.5:compile
[INFO] |  |     \- io.micrometer:micrometer-commons:jar:1.11.5:compile
[INFO] |  \- org.springframework:spring-webmvc:jar:6.0.13:compile
[INFO] |     \- org.springframework:spring-expression:jar:6.0.13:compile
[INFO] +- org.springframework.boot:spring-boot-docker-compose:jar:3.1.5:runtime
[INFO] |  +- org.springframework.boot:spring-boot:jar:3.1.5:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.15.3:compile
[INFO] |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.15.3:compile
[INFO] |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.15.3:compile
[INFO] |  \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.15.3:compile
[INFO] +- org.apache.commons:commons-csv:jar:1.8:compile
[INFO] +- com.mysql:mysql-connector-j:jar:8.0.33:runtime
[INFO] +- org.projectlombok:lombok:jar:1.18.30:compile
[INFO] \- org.springframework.boot:spring-boot-starter-test:jar:3.1.5:test
[INFO]    +- org.springframework.boot:spring-boot-test:jar:3.1.5:test
[INFO]    +- org.springframework.boot:spring-boot-test-autoconfigure:jar:3.1.5:test
[INFO]    +- com.jayway.jsonpath:json-path:jar:2.8.0:test
[INFO]    +- jakarta.xml.bind:jakarta.xml.bind-api:jar:4.0.1:runtime
[INFO]    |  \- jakarta.activation:jakarta.activation-api:jar:2.1.2:runtime
[INFO]    +- net.minidev:json-smart:jar:2.4.11:test
[INFO]    |  \- net.minidev:accessors-smart:jar:2.4.11:test
[INFO]    |     \- org.ow2.asm:asm:jar:9.3:test
[INFO]    +- org.assertj:assertj-core:jar:3.24.2:test
[INFO]    +- org.hamcrest:hamcrest:jar:2.2:test
[INFO]    +- org.junit.jupiter:junit-jupiter:jar:5.9.3:test
[INFO]    |  +- org.junit.jupiter:junit-jupiter-api:jar:5.9.3:test
[INFO]    |  |  +- org.opentest4j:opentest4j:jar:1.2.0:test
[INFO]    |  |  +- org.junit.platform:junit-platform-commons:jar:1.9.3:test
[INFO]    |  |  \- org.apiguardian:apiguardian-api:jar:1.1.2:test
[INFO]    |  +- org.junit.jupiter:junit-jupiter-params:jar:5.9.3:test
[INFO]    |  \- org.junit.jupiter:junit-jupiter-engine:jar:5.9.3:test
[INFO]    |     \- org.junit.platform:junit-platform-engine:jar:1.9.3:test
[INFO]    +- org.mockito:mockito-core:jar:5.3.1:test
[INFO]    |  +- net.bytebuddy:byte-buddy-agent:jar:1.14.9:test
[INFO]    |  \- org.objenesis:objenesis:jar:3.3:test
[INFO]    +- org.mockito:mockito-junit-jupiter:jar:5.3.1:test
[INFO]    +- org.skyscreamer:jsonassert:jar:1.5.1:test
[INFO]    |  \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
[INFO]    +- org.springframework:spring-core:jar:6.0.13:compile
[INFO]    |  \- org.springframework:spring-jcl:jar:6.0.13:compile
[INFO]    +- org.springframework:spring-test:jar:6.0.13:test
[INFO]    \- org.xmlunit:xmlunit-core:jar:2.9.1:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.723 s
[INFO] Finished at: 2023-10-20T18:57:05+02:00
[INFO] ------------------------------------------------------------------------

推荐答案

This is how to reload dependencies in IntelliJ manually:

enter image description here

Here you can configure the automatic reload:

enter image description here

enter image description here

(Re)building with maven is also possible from the IDE:

enter image description here

Java相关问答推荐

如何在Spring Boot中创建500错误的响应正文?

当切换javaFX场景时,stage的大小正在Minimize

将成为一个比较者.比较…在现代Java中,编译器会对`CompareTo`方法进行优化吗?

上下文初始化期间遇到异常-使用Java配置配置HibernateTemplate Bean时

内存中的H2修剪尾随空格

更新GWT 2.5.1到2.11.0和sencha GXT 3.1.1到4.1时出现错误

从Spring5迁移到Spring6:无法在雅加达包中找到类

在JDK 1.8源代码中,为什么使用A-B 0来确定哪个更大,而不是A B?

如何在antlr4中跳过所有反斜杠-换行符而保留换行符?

如何只修改父类ChroniclerView位置0处的第一个嵌套ChroniclerView(child)元素?

如何将Pane的图像快照保存为BMP?

有效的公式或值列表必须少于或等于255个字符

允许同时执行两个方法,但不能同时执行这两个方法

Java中不兼容的泛型类型

当构造函数创建一个新实例时,Java为什么需要&new";

Regex以查找不包含捕获组的行

如何使这两种方法合二为一?

除0错误/抱歉我的句子是PT

将天数添加到ZonedDateTime不会更改时间

找不到 jar 文件系统提供程序try 使用 jdeps 和 jlink 创建收缩 Java 映像来运行 Minecraft