逐行读取保存在SD卡中的文本文件

时间:2014-05-16 03:08:52

标签: arduino

我在SD卡中保存了data.txt个文件,其中包含驱动3个步进电机所需的变量值。我希望逐行阅读data.txt文件,将行拆分为","并将每个值保存到相应的变量中。 data.txt文件的数据如下:

8.24, 5.67, 7.34  3,86, 3.56, 4.49   5.38, 6.29, 3.67

我希望在x中保存第一个整数的值,在y中保存第二个,在z中保存第三个并执行代码。一旦执行代码,我想获得下一行并在各自的变量中加载相应的值并继续循环,直到data.txt文件中没有剩下的行

1 个答案:

答案 0 :(得分:0)

这个矩阵代码,让我们改变你需要的某种类型

public  Matrix loadIntMapData(InputStream input) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
        String line;
        ArrayList<double[]> list = new ArrayList<double[]>();
        while ((line = reader.readLine()) != null) {
            if (line.equals("")) {
                //ignore
            } else {
                String[] vs = line.split(",");
                double ints[] = new double[vs.length];
                for (int i = 0; i < ints.length; i++) {
                    ints[i] = Double.parseDouble(vs[i]);
                }
                list.add(ints);
            }

        }
        double[][] map = new double[list.size()][];
        for (int i = 0; i < map.length; i++) {
            map[i] = list.get(i);
        }
        return new Matrix(map);
    }

    public static final void writInteMapData(Matrix matrix, File file) throws IOException {
        double[][] map = matrix.getArray();
        StringBuffer output = new StringBuffer();
        for (int i = 0; i < map.length; i++) {

            for (int j = 0; j < map[i].length; j++) {
                output.append(Double.toString(map[i][j]));
                if (j != map[i].length - 1) {
                    output.append(",");
                }
            }

            if (i != map.length - 1) {
                output.append("\r\n");
            }
        }


        BufferedWriter writer = new BufferedWriter(new FileWriter(file));
        writer.write(output.toString());
        writer.close();
    }