gpt4 book ai didi

Java Weka - 无法创建新的输出文件。使用标准输出

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:37:01 24 4
gpt4 key购买 nike

我有一个 Java 程序可以将 CSV 文件转换为 Weka 的 ARFF 格式。

它在任何给定 session 中第一次运行时运行完美,但随后总是失败并显示消息:

Cannot create a new output file. Standard out is used.

程序如下:

import java.io.File;
import weka.core.Instances;
import weka.core.converters.ArffSaver;
import weka.core.converters.CSVLoader;

public class CSV2Arff {
/**
* takes 2 arguments:
* - CSV input file
* - ARFF output file
* @param args
* @throws java.lang.Exception
*/
public static void main(String[] args) throws Exception {
if (args.length != 2) {
System.out.println("\nUsage: CSV2Arff <input.csv> <output.arff>\n");
System.exit(1);
}

// load CSV
CSVLoader loader = new CSVLoader();
loader.setSource(new File(args[0]));
Instances data = loader.getDataSet();

// save ARFF
ArffSaver saver = new ArffSaver();
saver.setInstances(data);
saver.setFile(new File(args[1]));
//saver.setDestination(new File(args[1]));
saver.writeBatch();
}
}

来自控制台的完整错误:

Oct 31, 2016 3:53:39 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Oct 31, 2016 3:53:39 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Oct 31, 2016 3:53:39 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 6142 ms
Cannot create a new output file. Standard out is used.

最佳答案

根据 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 java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.File;
import weka.core.Instances;
import weka.core.converters.ArffSaver;
import weka.core.converters.CSVLoader;

public class CSV2Arff {
/**
* takes 2 arguments:
* - CSV input file
* - ARFF output file
* @param args
* @throws java.lang.Exception
*/
public static void main(String[] args) throws Exception {
if (args.length != 2) {
System.out.println("\nUsage: CSV2Arff <input.csv> <output.arff>\n");
System.exit(1);
}

// load CSV
CSVLoader loader = new CSVLoader();
loader.setSource(new File(args[0]));
Instances data = loader.getDataSet();
String fileName = args[1];


// save ARFF
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
writer.write(data.toString());
writer.flush();
writer.close();

}
}

关于Java Weka - 无法创建新的输出文件。使用标准输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40349302/

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