问题:无法解析构造函数CSVPrinter

时间:2019-05-10 06:28:57

标签: java csv intellij-idea javafx constructor

我正在尝试从CSV文件生成和加载,并且CSVPrinter出现了问题。该语句如下:

  

无法解析构造函数'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");
        }

1 个答案:

答案 0 :(得分:0)

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

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

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

这是我的源代码版本:

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