【问题标题】:Method is not returning CSV file - Java方法未返回 CSV 文件 - Java
【发布时间】:2017-06-06 10:45:32
【问题描述】:

我正在尝试读取 HashMap 并写入 CSV 文件。我正在使用CSVPrinter,但我的方法没有返回 CSV 文件。

这是我使用的代码: 编辑代码

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

public class SJV{
    public File writeCSV(Map featureSet) throws IOException{

        CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(NEW_LINE_SEPARATOR);
        Map<String, Integer> map = featureSet;
        File temp = File.createTempFile("tempfile", ".tmp");
        FileWriter fs = new FileWriter(temp);

        CSVPrinter csvFilePrinter = new CSVPrinter(fs, csvFileFormat);
        //csvFilePrinter.printRecord(FILE_HEADER);

        csvFilePrinter.printRecord(map.keySet().toArray());
        csvFilePrinter.printRecord(map.values().toArray());



csvFilePrinter.close();
fs.close();


        return  temp;
    }
}

这是我的测试:

public void testWriteCSV() throws IOException {
    SJV sj = new  SJV();

    HashMap<String, Integer> hmap = new HashMap<String, Integer>();
    hmap.put("Feature1", 1);
    hmap.put("Feature2", 2);
    File fs = sj.writeCSV(hmap);

    if (fs.exists()){
        Scanner input = new Scanner(fs );

        while (input.hasNextLine())
        {
           System.out.println(input.nextLine());
        }
        input.close();
    }

我真的不明白为什么该方法没有返回 CSV 文件。

【问题讨论】:

  • 缺少一点代码。 sj 是什么?有什么写的吗?有什么错误吗?
  • 返回什么方法而不是 CSV 文件?
  • 我认为您没有关闭文件。
  • @Kumaresp 不,实际上,这不是另一个问题。这可能是您遇到问题的原因。尝试纠正它。
  • edit你的问题,并把你的新版本关闭。

标签: java file csv


【解决方案1】:

试试这个(在这段代码中,我已将格式从 .tmp 更改为 .csv)

一个问题是我们没有关闭 csvFilePrinter -> csvFilePrinter.close() 并且我从问题中了解到用户期望 CSV 文件扩展名,因此,我在编写时使用了 .csv 扩展名。 :

    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Scanner;

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

    public class TestMain {

        public static void main(String args[]) {

            HashMap<String, Integer> hmap = new HashMap<String, Integer>();
            hmap.put("Feature11", 1);
            hmap.put("Feature22", 2);
            CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(System.lineSeparator());
            Map<String, Integer> map = hmap;
            File temp;
            try {
                temp = File.createTempFile("tempfile", ".csv");
                FileWriter fs = new FileWriter(temp);

                CSVPrinter csvFilePrinter = new CSVPrinter(fs, csvFileFormat);
                csvFilePrinter.printRecord(map.keySet().toArray());
                csvFilePrinter.printRecord(map.values().toArray());
                csvFilePrinter.close();

                System.out.println(" temp " + temp);

                if (temp.exists()) {
                    Scanner input = new Scanner(temp);

                    while (input.hasNextLine()) {
                        System.out.println(input.nextLine());
                    }
                    input.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }

根据评论中的建议进行编辑(此代码适用于我,这是示例输出):

 temp C:\Users\<MY USERNAME>\AppData\Local\Temp\tempfile7124969351872886823.csv
Feature11,Feature22
1,2

【讨论】:

  • 最好不要抛出 IOException 并自己尝试/捕获它。
【解决方案2】:

试试:

fs.flush();
fs.close();
csvFilePrinter.close();

【讨论】:

  • 关闭前不需要刷新。也无需在关闭CSVPrinter 之前关闭FileWriter(如果CSVPrinterclose())。
  • fs.close() 将在刷新 csvFilePrinter 中缓冲的任何内容之前关闭 fs。然后,当您尝试关闭 csvFilePrinter 时,在 fs 已经关闭之前什么都不会发生。
  • 你已经验证了文件是空的,而不仅仅是在代码中检查它,对吧?
猜你喜欢
  • 2017-07-03
  • 1970-01-01
  • 2014-05-09
  • 1970-01-01
  • 1970-01-01
  • 2015-07-09
  • 1970-01-01
  • 2017-07-25
  • 2019-07-31
相关资源
最近更新 更多