gpt4 book ai didi

java - 问题: cannot resolve constructor CSVPrinter

转载 作者:行者123 更新时间:2023-12-02 09:54:51 25 4
gpt4 key购买 nike

我正在尝试生成并从 CSV 文件加载,但 CSVPrinter 出现问题。声明如下:

Cannot resolve constructor 'CSVPrinter(java.io.BufferedWriter,org.apache.commons.csv.CSVFormat)"

import org.apache.commons.csv.CSVFormat; 
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;
import java.io.BufferedWriter;
import java.io.*;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;

try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(file.getAbsolutePath()));
CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT
.withHeader("title", "description", "priority"))

) {

for (int i = 0; i < serializer.toDO.size(); i++){
csvPrinter.printRecord(serializer.toDO.get(i).getTitle(),
serializer.toDO.get(i).getDescription().replace("\n"," "),
serializer.toDO.get(i).getPriority(),
serializer.toDO.get(i).getLocalDate(),
"toDo");
}

最佳答案

假设您使用的是 apache 变体,您的代码对我有用。

我认为您需要添加一些您向我们保证已完成的细节。我的猜测是您的项目/类路径中有错误的库。

  • 您在什么环境中运行代码(在 IDE 中还是通过命令提示符独立运行)?
  • 您是如何合并该库的(直接从 Apache 下载 JAR 或通过服务自动下载 - 例如 maven)?

这是我的源代码版本:

package csvwriter;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVFormat;

public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Main m = new Main();
m.go(args);
}

public void go(String [] args) {
File file = new File (args[0]);
try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(file.getAbsolutePath()));
CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT
.withHeader("title", "description", "priority"))

) {
System.out.println("Done.");
// for (int i = 0; i < serializer.toDO.size(); i++){
// csvPrinter.printRecord(serializer.toDO.get(i).getTitle(),
// serializer.toDO.get(i).getDescription().replace("\n"," "),
// serializer.toDO.get(i).getPriority(),
// serializer.toDO.get(i).getLocalDate(),
// "toDo");
// }
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

这是我的图书馆:

Project structure in Netbeans - showing libraries

我从apache download center下载了该库

当我运行时,我得到以下输出:

run:
Done.
BUILD SUCCESSFUL (total time: 0 seconds)

通过 Maven 在 IntelliJ(Java 项目,合并 apache-commons-csv-1.6)中重复此练习,会产生相同的结果:

"C:\Program Files\Java\jdk1.8.0_172\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.1\lib\idea_rt.jar=59482:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_172\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\rt.jar;C:\cygwin64\home\gm310509\Projects\Learning\Miscellaneous\CSVPrinter\out\production\CSVPrinter;C:\cygwin64\home\gm310509\Projects\Learning\Miscellaneous\CSVPrinter\lib\commons-csv-1.6.jar" stackoverflow.Main c:\temp\resistor.html
Done.

Process finished with exit code 0

这是“通过 Maven 添加库”对话框:

enter image description here

关于java - 问题: cannot resolve constructor CSVPrinter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56071925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com