对于我的项目,我使用的是weka.jar。我正在使用以下代码将CSV文件转换为ARFF:
import weka.core.Instances;
import weka.core.converters.ArffSaver;
import weka.core.converters.CSVLoader;
import java.io.File;
public class CsvArffConverter
{
public static void Convert(String sourcepath,String destpath) throws Exception
{
// load CSV
CSVLoader loader = new CSVLoader();
loader.setSource(new File(sourcepath));
Instances data = loader.getDataSet();
// save ARFF
ArffSaver saver = new ArffSaver();
saver.setInstances(data);
saver.setFile(new File(destpath));
saver.setDestination(new File(destpath));
saver.writeBatch();
}
public static void main(String args[]) throws Exception
{
Convert("C:\\ad\\BSEIT.csv", "C:\\ad\\test.arff");
}
}
然而,在执行时,我收到以下错误:
Cannot create a new output file. Standard out is used.
Exception in thread "main" java.io.IOException: Cannot create a new output file (Reason: java.io.IOException: File already exists.). Standard out is used.
at `enter code here`weka.core.converters.AbstractFileSaver.setDestination(AbstractFileSaver.java:421)
at Predictor.CsvArffConverter.Convert(CsvArffConverter.java:29)
at Predictor.CsvArffConverter.main(CsvArffConverter.java:34)
答案 0 :(得分:2)
根据weka mail list,此错误是文件问题,可能是权限。其他电子邮件建议使用Java I/O approch来保存arff文件。
This error is coming from the CSVSaver and indicates that it is unable to create the directory and/or file that you've specified. More than likely it is something to do with permissions on where it is trying to write to.
请尝试以下代码。
import weka.core.Instances;
import weka.core.converters.ArffSaver;
import weka.core.converters.CSVLoader;
import java.io.File;
public class CsvArffConverter
{
public static void Convert(String sourcepath,String destpath) throws Exception
{
// load CSV
CSVLoader loader = new CSVLoader();
loader.setSource(new File(sourcepath));
Instances dataSet = loader.getDataSet();
// save ARFF
BufferedWriter writer = new BufferedWriter(new FileWriter(destpath));
writer.write(dataSet.toString());
writer.flush();
writer.close();
}
public static void main(String args[]) throws Exception
{
Convert("BSEIT.csv", "test.arff");
}
}
如您所见,我使用相对路径。由于许可问题可能会阻止绝对路径写入。