Search code examples
javacsvweka

Java Weka - Cannot create a new output file. Standard out is used


I have a Java program that converts CSV files to Weka's ARFF format.

It works perfectly the first time it's run in any given session, however it subsequently always fails with the message:

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

Here's the program:

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();
  }
}

Full error from console:

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.

Solution

  • According to weka mail list, this error is a file issue. Other emails suggest to use Java I/O approch to save arff file.

    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.
    

    Try following code.

    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();
    
      }
    }