Search code examples
javaandroidfilemachine-learningweka

Weka - Premature End of file, line 1


I know that this question may sound like a duplicate but it is not. None of the other questions about this error in Weka were able to solve my problem and they had to do with arff files. I am using a text file. After successfully using Weka in Eclipse, I wanted to use it for an Android app. Therefore, I am creating and storing a file in the internal storage of the device and I am able to read from it. This is how I am creating the file:

String filename = "mydata";
String relation = "@relation mydata";
String attributes = "\n@attribute cheek numeric\n\n" +
        "@attribute eye numeric\n" +
        "@attribute mouth numeric\n" +
        "@attribute nose numeric\n" +
        "@attribute name {0, 1, 2}";
String data = "\n\n@data\n" +
        "58.319244, 35.185467, 28.329004, 19.908704, 0\n" +
        "64.63182, 36.4422 , 30.721794, 20.632916, 1" + "\n" +
        "65.45019, 36.28883, 27.934803, 26.095049, 2";

FileOutputStream outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write((relation + attributes + data).getBytes());
outputStream.close();

And when I read from the file, this is what it looks like:

@relation mydata

@attribute cheek numeric
...
@data
58.319244, 35.185467, 28.329004, 19.908704, 0
...

The error that I am getting is:

java.io.IOException: premature end of file, read Token[EOF] line 1

The error occurs on the last line:

FileInputStream fis = getApplication().openFileInput("practice");

InputStreamReader isr = new InputStreamReader(fis); BufferedReader bufferedReader = new BufferedReader(isr); Instances combined = new Instances(bufferedReader);

I have spent a lot of time searching up this error but have not been able to figure out a solution. Once again, I am not saving this an arff file, I think it is being saved as a text file. I also believe the error has to do with getting the BufferedReader and passing it into the Instances constructor.

Would anyone be able to help me solve this issue? I was using a text file in Eclipse with a similar structure, but now in Android, it is giving me this error.


Update:

I have found a solution to my problem and posted it below. The problem was with reading the file into the BufferedReader.


Solution

  • I was able to get it to work by doing following a different approach to getting the BufferedReader from the File. The following code successfully creates a BufferedReader with the data from the file in internal storage and can then be used in the Instances constructor.

    String yourFilePath = getApplicationContext().getFilesDir() + "/" + "practice";
    File yourFile = new File(yourFilePath);
    BufferedReader bufferedReader = new BufferedReader(new FileReader(yourFile));
    

    Originally, I had been trying the following:

    FileInputStream fis = getApplication().openFileInput("practice");
    InputStreamReader isr = new InputStreamReader(fis);
    BufferedReader bufferedReader = new BufferedReader(isr);
    

    The code above was causing the exception:

    java.io.IOException: premature end of file, read Token[EOF] line 1