2017-05-22 5 views
0

私はCSVファイルにBLEスキャン結果を書き込みます。私が現在やっていることは、すべてのデータを一つ下に書き込むことです。 データは、デバイス名、rssi、およびMACアドレスで構成されています。たとえば、CSVファイルは次のようになります -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 

マイrequireemntは次のように書くことです - デバイスAの最後の列の後

DeviceA -85 DS:DA:AB:2B:B4:AE DeviceB -100 2C:18:0B:2B:96:9E 
DeviceA -85 DS:DA:AB:2B:B4:AE 

、私は、デバイスBの新しい列を開始する必要がありますデバイス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()メソッドから呼び出されます。

どのように私は横に書くことができますか?

答えて

0

LINE_SEPARATORを書き込む前に、同じ行に2を入れるだけです。このような何かにあなたのwriteElementsに何の変更:

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

テスト:

Object[] elements = new Object[4]; 
elements[0] = "here"; 
elements[1] = "are"; 
elements[2] = "some"; 
elements[3] = "words"; 

writeElements(elements); 

ファイル開くとき:

here,are 
some,words 
+0

doesntのを編集し、もう一度試してください – Del905241

+0

:(動作するようには思え –

+0

。もう一度やり直してください。 –

関連する問題