Java CSV将数据并排写入

Java CSV将数据并排写入,java,android,csv,Java,Android,Csv,我正在将扫描结果写入CSV文件。我目前正在做的是将所有数据一个接一个地写下来。 数据由设备名称、rssi和mac地址组成。例如,CSV文件如下所示- DeviceA -85 DS:DA:AB:2B:B4:AE DeviceB -100 2C:18:0B:2B:96:9E DeviceA -85 DS:DA:AB:2B:B4:AE DeviceA -85 DS:DA:AB:2B:B4:AE DeviceB -100 2C:18:0B:2B:96:9E DeviceA -85 DS:DA:AB:2

我正在将扫描结果写入CSV文件。我目前正在做的是将所有数据一个接一个地写下来。 数据由设备名称、rssi和mac地址组成。例如,CSV文件如下所示-

DeviceA -85 DS:DA:AB:2B:B4:AE
DeviceB -100 2C:18:0B:2B:96:9E
DeviceA -85 DS:DA:AB:2B:B4:AE
DeviceA -85 DS:DA:AB:2B:B4:AE DeviceB -100 2C:18:0B:2B:96:9E
DeviceA -85 DS:DA:AB:2B:B4:AE
我的要求是这样写-

DeviceA -85 DS:DA:AB:2B:B4:AE
DeviceB -100 2C:18:0B:2B:96:9E
DeviceA -85 DS:DA:AB:2B:B4:AE
DeviceA -85 DS:DA:AB:2B:B4:AE DeviceB -100 2C:18:0B:2B:96:9E
DeviceA -85 DS:DA:AB:2B:B4:AE
在设备A的最后一列之后,我需要从设备B的新列开始,而不是写在设备A下面。 同样对于设备C,我想把它写在设备C旁边…等等。这是我写CSV的代码

public final String DATA_SEPARATOR = ",";
    public final String LINE_SEPARATOR = System
            .getProperty("line.separator");

          try {

                fileName = "test.csv";


               path = Environment.getExternalStorageDirectory()
                            + File.separator + "Documents";


                path += File.separatorChar + "SampleApp";
                File file = new File(path, fileName);

                new File(path).mkdirs();
                file.createNewFile();

                fileStream = new OutputStreamWriter(new FileOutputStream(file));

                fileStream.write("sep= " + DATA_SEPARATOR + LINE_SEPARATOR);
            } catch (IOException e) {
                e.printStackTrace();
                fileStream = null;
            }

 private void writeElements(Object... elements) throws IOException {


            if (fileStream != null) {
                for (Object o : elements) {
                    fileStream.write(o.toString());
                    fileStream.write(DATA_SEPARATOR);
                }
                fileStream.write(LINE_SEPARATOR);
            }

 }
writeElements(btDeviceName、btRSSIValue、btMacId)
不时从
bluetoothScan()
方法调用


如何实现旁写?

在编写
行分隔符之前,只需将2放在同一行即可。将
写元素中的内容更改为以下内容:

private void writeElements(Object... elements) throws IOException {
    if (fileStream != null) {
        for (int index = 1; index < elements.length + 1; index++) {
            String address = elements[index - 1].toString();
            fileStream.write(address);
            if(index % 2 == 0) fileStream.write(LINE_SEPARATOR);
            else fileStream.write(DATA_SEPARATOR);
        }
    }
}
打开文件时:

here,are
some,words

还是没有…是sameOk做了最后一次编辑。代码测试和验证,对我有效。还是一样的问题。。。我得到的输出是-DeviceA-85ds:DA:AB:2B:B4:aedecviceb-90,更新后显示了我的测试用例。如果它不起作用,你的代码还有其他问题。还有,为什么你没有逗号?这是一个CSV文件。