1

I am working on some AI project which I am supposed to implement a controller using fuzzy logic on my NXT. In order to evaluate properly my control strategy, I have the need to track the information measured by the color sensor and the data that are being send to the motors. For this, I was trying to implement a simple code to write some similar infos to a .txt file. Here is what I've accomplished so far:

import java.io.*;
import lejos.nxt.*;

public class DataLogger {

    public static void main(String[] args) {

        int count = 0;
        FileOutputStream fileStream = null;
        try {
            fileStream = new FileOutputStream(new File("Test.txt"));
        } catch (Exception e) {
            LCD.drawString("Can't make a file", 0, 0);
            System.exit(1);
        }

        DataOutputStream dataStream = new DataOutputStream(fileStream);

        do {
                try {
                    dataStream.writeChars(String.valueOf(count));
                   fileStream.flush();
                    count++;
                } catch (IOException e) {
                    LCD.drawString("Can't write to the file", 0, 1);
                    System.exit(1);
                }
        } while (count < 100);

        try {
            fileStream.close();
        } catch (IOException e) {
            LCD.drawString("Can't save the file", 0, 1);
            System.exit(1);
        }
    }

}

With this code I am basically trying to write numbers between 0 and 99 to a file called Test.txt. I don't know why, but the program is writting the data like this:

0 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 ...

As you can see, it is adding blank spaces between every digit. I've already tried many writting methods for the DataOutputStream, and dataStream.writeChars(String.valueOf(count)); was the "most successfull" one (other ones like writeInt(int b) write the data according to the ASCII table). I've also tried to use the BufferedOutputStream class, but I had no success. What I could possibly be doing wrong?

Mudkip
  • 373
  • 6
  • 27

2 Answers2

0

You're using the wrong method to write basic text data to a file. From the Java 7 API docs:

FileOutputStream is meant for writing streams of raw bytes such as image data. For writing streams of characters, consider using FileWriter.

Additionally, it's best practice to use try-with-resources rather than manually closing the file.

Here's an updated version of your test class which I have tested:

import java.io.*;

public class DataLogger {

    public static void main(String[] args) {

        int count = 0;
        String filename = "Test.txt";
        File file = new File(filename);

        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {

            if ( ! file.exists() ) {
                file.createNewFile();
            }

            do {

                writer.write(Integer.toString(count));
                count++;

            } while (count < 100);

        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

}

Keep in mind that the integer must first be converted to a string, otherwise it will be treated as a character reference.

Also, System.out.println() can be used with the EV3 as an alternative to LCD.drawString() for ligging errors as it writes on the next available line on the LCD screen (with scrolling) and also saves output to a file on the EV3 for debugging.

shea
  • 1,129
  • 2
  • 21
  • 38
0

I solved the problem by simply substituting dataStream.writeChars(String.valueOf(count)) by dataStream.writeBytes(String.valueOf(count)).

Mudkip
  • 373
  • 6
  • 27