方法未返回CSV文件-Java
我正在尝试读取方法未返回CSV文件-Java,java,file,csv,Java,File,Csv,我正在尝试读取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{ C
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;
}
}
import org.apache.commons.csv.CSVFormat;
导入org.apache.commons.csv.CSVPrinter;
公共级SJV{
公共文件writeCSV(映射特性集)引发IOException{
CSVFormat csvFileFormat=CSVFormat.DEFAULT.withRecordSeparator(新的行分隔符);
地图地图=特征集;
File temp=File.createTempFile(“tempfile”,“.tmp”);
FileWriter fs=新的FileWriter(临时文件);
CSVPrinter csvFilePrinter=新的CSVPrinter(fs,csvFileFormat);
//打印记录(文件头);
打印记录(map.keySet().toArray());
打印记录(map.values().toArray());
csvFilePrinter.close();
fs.close();
返回温度;
}
}
这是我的测试:
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();
}
public void testWriteCSV()引发IOException{
SJV sj=新的SJV();
HashMap hmap=新的HashMap();
hmap.put(“特征1”,1);
hmap.put(“特征2”,2);
文件fs=sj.writeCSV(hmap);
如果(fs.exists()){
扫描仪输入=新扫描仪(fs);
while(input.hasNextLine())
{
System.out.println(input.nextLine());
}
input.close();
}
我真的不明白为什么该方法不返回CSV文件。试试:
fs.flush();
fs.close();
csvFilePrinter.close();
尝试:
试试这个(在这段代码中,我将格式从.tmp更改为.csv)
一个问题是我们没有关闭csvFilePrinter->csvFilePrinter.close(),我从这个问题中了解到,用户需要一个CSV文件扩展名,因此,我在写它时使用了.CSV扩展名。
:
导入java.io.File;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.util.HashMap;
导入java.util.Map;
导入java.util.Scanner;
导入org.apache.commons.csv.CSVFormat;
导入org.apache.commons.csv.CSVPrinter;
公共类TestMain{
公共静态void main(字符串参数[]){
HashMap hmap=新的HashMap();
hmap.put(“特征11”,1);
hmap.put(“特征22”,2);
CSVFormat csvFileFormat=CSVFormat.DEFAULT.withRecordSeparator(System.lineSeparator());
Map=hmap;
文件温度;
试一试{
temp=File.createTempFile(“tempfile”,“.csv”);
FileWriter fs=新的FileWriter(临时文件);
CSVPrinter csvFilePrinter=新的CSVPrinter(fs,csvFileFormat);
打印记录(map.keySet().toArray());
打印记录(map.values().toArray());
csvFilePrinter.close();
系统输出打印项次(“温度”+温度);
如果(临时存在()){
扫描仪输入=新扫描仪(温度);
while(input.hasNextLine()){
System.out.println(input.nextLine());
}
input.close();
}
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
根据注释中的建议进行编辑(此代码适用于我,以下是示例输出):
temp C:\Users\\AppData\Local\temp\tempfile7124969351872886823.csv
特色11、特色22
1,2
试试这个(在这段代码中,我将格式从.tmp更改为.csv)
一个问题是我们没有关闭csvFilePrinter->csvFilePrinter.close(),我从这个问题中了解到,用户需要一个CSV文件扩展名,因此,我在写它时使用了.CSV扩展名。
:
导入java.io.File;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.util.HashMap;
导入java.util.Map;
导入java.util.Scanner;
导入org.apache.commons.csv.CSVFormat;
导入org.apache.commons.csv.CSVPrinter;
公共类TestMain{
公共静态void main(字符串参数[]){
HashMap hmap=新的HashMap();
hmap.put(“特征11”,1);
hmap.put(“特征22”,2);
CSVFormat csvFileFormat=CSVFormat.DEFAULT.withRecordSeparator(System.lineSeparator());
Map=hmap;
文件温度;
试一试{
temp=File.createTempFile(“tempfile”,“.csv”);
FileWriter fs=新的FileWriter(临时文件);
CSVPrinter csvFilePrinter=新的CSVPrinter(fs,csvFileFormat);
打印记录(map.keySet().toArray());
打印记录(map.values().toArray());
csvFilePrinter.close();
系统输出打印项次(“温度”+温度);
如果(临时存在()){
扫描仪输入=新扫描仪(温度);
while(input.hasNextLine()){
System.out.println(input.nextLine());
}
input.close();
}
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
根据注释中的建议进行编辑(此代码适用于我,以下是示例输出):
temp C:\Users\\AppData\Local\temp\tempfile7124969351872886823.csv
特色11、特色22
1,2
缺少一点代码。sj
是什么?是否写入了任何内容?是否有错误?返回的方法是什么而不是CSV文件?我不认为您正在关闭该文件。@Kumaresp不,事实上,这不是另一个问题。这可能是您遇到问题的原因。请尝试更正它。请回答您的问题,然后输入新版本随着关闭。缺少一点代码。什么是sj
?写了什么?
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