【问题标题】:LeJOS: How write integer data succesfully to a file in NXTLeJOS:如何将整数数据成功写入 NXT 中的文件
【发布时间】:2015-09-14 15:44:46
【问题描述】:

我正在做一些 AI 项目,我应该在我的 NXT 上使用模糊逻辑实现控制器。为了正确评估我的控制策略,我需要跟踪颜色传感器测量的信息以及发送到电机的数据。为此,我试图实现一个简单的代码来将一些类似的信息写入 .txt 文件。以下是我迄今为止所取得的成就:

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

}

使用这段代码,我基本上是在尝试将 0 到 99 之间的数字写入名为 Test.txt 的文件中。我不知道为什么,但程序是这样写数据的:

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 ...

如您所见,它在每个数字之间添加空格。我已经为 DataOutputStream 尝试了许多写入方法,dataStream.writeChars(String.valueOf(count)); 是“最成功”的一种(其他方法如writeInt(int b) 根据 ASCII 表写入数据)。我也尝试过使用 BufferedOutputStream 类,但没有成功。我可能做错了什么?

【问题讨论】:

    标签: java dataoutputstream nxt lejos-nxj


    【解决方案1】:

    您使用错误的方法将基本文本数据写入文件。来自Java 7 API 文档:

    FileOutputStream 用于写入原始字节流,例如图像数据。要写入字符流,请考虑使用 FileWriter。

    此外,最好使用try-with-resources 而不是手动关闭文件。

    这是我测试过的测试类的更新版本:

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

    请记住,整数必须先转换为字符串,否则将被视为字符引用。

    此外,System.out.println() 可以与 EV3 一起使用,作为LCD.drawString() 的替代品,用于在 LCD 屏幕上的下一个可用行上写入(通过滚动)并保存输出到 EV3 上的文件时出现连接错误用于调试。

    【讨论】:

      【解决方案2】:

      我通过简单地将dataStream.writeChars(String.valueOf(count)) 替换为dataStream.writeBytes(String.valueOf(count)) 解决了这个问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-19
        • 2020-02-10
        • 1970-01-01
        • 2022-11-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多