I see no reason for not writing comments in the header of the ARFF file. The specification clearly says:
Lines starting with the% character are comments.
Thus, although this is technically feasible, it can be tricky if you want to use the ArffSaver#setFile . This method makes a lot (convenient, but somewhat arbitrary and unspecified) work inside until it finally calls
setDestination(new FileOutputStream(m_outputFile));
If this is not required, the easiest option is to write directly to the OutputStream , which you can then simply set as the destination for ArffSaver . This can be wrapped with a small helper method, for example, as follows:
static void writeArff( Instances instances, List<String> commentLines, OutputStream outputStream) throws IOException { ArffSaver saver = new ArffSaver(); saver.setInstances(instances); if (commentLines != null && !commentLines.isEmpty()) { BufferedWriter bw = new BufferedWriter( new OutputStreamWriter(outputStream)); for (String commentLine : commentLines) { bw.write("% " + commentLine + "\n"); } bw.write("\n"); bw.flush(); } saver.setDestination(outputStream); saver.writeBatch(); }
When calling this type
List<String> comments = Arrays.asList("A comment", "Another one"); writeArff(instances, comments, outputStream);
then these comments will be inserted at the top of the ARFF file.
source share