2014-03-30 45 views
0

我有一些結果,我將它們存儲在ArrayList中。我想將這些txt文件保存(輸出)到我的SD卡上。但我不知道。我嘗試一個代碼,但文件不是。如果文件被創建,我會寫入下一個活動。我想知道如何將我的ArrayList寫入下一個Activity。如何寫入我的ArrayList的txt文件?

分:列表,存儲谷歌地圖標記位置 szoveg:一個字符串ArrayList。

if(points.size()>1 ) { 

       for(int i=0;i<points.size();i++){ 
        for(int j=i+1;j<points.size();j++){ 
         Location LocA = new Location("A"); 
         LocA.setLatitude(points.get(i).latitude); 
         LocA.setLongitude(points.get(i).longitude); 

         Location LocB= new Location("B"); 
         LocB.setLatitude(points.get(j).latitude); 
         LocB.setLongitude(points.get(j).longitude); 

         double distance = LocA.distanceTo(LocB); 

         String rad = ""; 
         rad=sharedPreferences.getString("rad"+i, "0"); 
         double radius=Double.parseDouble(rad); 

         String rad2 = ""; 
         rad2=sharedPreferences.getString("rad"+j, "0"); 
         double radius2=Double.parseDouble(rad2); 

         if(distance<radius && distance < radius2) 
         { 
          szoveg.add("Kommunikál: " +(i+1)+ " -> " +(j+1)+ " && " +(j+1)+ " -> " +(i+1)+ " - " + distance + " m"); 
          Toast.makeText(getBaseContext(), "Kommunikál: " +(i+1)+ " -> " +(j+1)+ " && " +(j+1)+ " -> " +(i+1)+ " - " + distance + " m" , Toast.LENGTH_SHORT).show(); 
          // Drawing Polyline on the map 
          //drawPolyline(point);          

         } 
         else if (distance>radius && distance<radius2) 
         { 
          szoveg.add("Kommunikál: " +(i+1)+ " -> " +(j+1)+ " && " +(j+1)+ " -> " +(i+1)+ " - " + distance + " m"); 
          Toast.makeText(getBaseContext(), "Kommunikál: " + (j+1) + " -> " + (i+1)+ " - " + distance + " m" , Toast.LENGTH_SHORT).show(); 
          // Drawing Polyline on the map 
          //drawPolyline(point); 
         } 
         else if (distance<radius && distance>radius2) 
         { 
          szoveg.add("Kommunikál: " +(i+1)+ " -> " +(j+1)+ " && " +(j+1)+ " -> " +(i+1)+ " - " + distance + " m"); 
          Toast.makeText(getBaseContext(), "Kommunikál: " +(i+1)+ " -> " +(j+1)+ " - " + distance + " m" , Toast.LENGTH_SHORT).show(); 
          // Drawing Polyline on the map 
          //drawPolyline(point); 
         } 
         //else Toast.makeText(getBaseContext(), "Nem kommunikál - " + distance + " m" , Toast.LENGTH_SHORT).show(); 
        } 
       } 
      } 
      else Toast.makeText(getBaseContext(), "Marker is added to the Map", Toast.LENGTH_SHORT).show(); 

     } 

我嘗試另一個應用程序這一點,但不工作(我用的onCreate writeTest()):

@SuppressLint("SdCardPath") 
private void writeTest() { 
    lista = new ArrayList<String>(); 
    int i= 0; 
    int j=0; 
    int distance = 100; 
    lista.add(("Kommunikál: " +(i+1)+ " -> " +(j+1)+ " && " +(j+1)+ " -> " +(i+1)+ " - " + distance + " m")); 
    lista.add("Kommunikál: " +(i+1)+ " -> " +(j+1)+ " && " +(j+1)+ " -> " +(i+1)+ " - " + distance + " m"); 

    try { 
     FileOutputStream fileOut = new FileOutputStream("/mnt/sdcard/elso.txt"); 
     ObjectOutputStream oos = new ObjectOutputStream (fileOut); 
     oos.writeObject(lista); 
     fileOut.close(); 
    } catch (Exception e) { 
     System.err.println(e.getMessage()); 
    } 
} 
+0

你可以隔離你的問題,只發布相關的代碼,而不是張貼那麼長的代碼。 –

回答

0

如果您需要保存目錄爲文本,工作與foreach循環或Iterator,運行所有元素並將每個字符串賦予FileWriter並將其保存爲SD卡上的文本。

import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

public class ListDemo { 

    public static void main(String[] args) { 
     FileWriter fileWriter = null; 
     try { 
      List<String> lista = new ArrayList<>(); 
      int i= 0; 
      int j=0; 
      int distance = 100; 
      lista.add("Kommunikál: " +(i+1)+ " -> " +(j+1)+ " && " +(j+1)+ " -> " +(i+1)+ " - " + distance + " m\n"); 
      lista.add("Kommunikál: " +(i+1)+ " -> " +(j+1)+ " && " +(j+1)+ " -> " +(i+1)+ " - " + distance + " m"); 

      fileWriter = new FileWriter(new File("listOutput.txt")); 
      for(String s : lista) 
       fileWriter.write(s); 

      fileWriter.flush(); 
     } catch (IOException ex) { 
      Logger.getLogger(ListDemo.class.getName()).log(Level.SEVERE, null, ex); 
     } finally { 
      try { 
       fileWriter.close(); 
      } catch (IOException ex) { 
       Logger.getLogger(ListDemo.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } 
    } 
} 

如果你使用一個ObjectOutputStream,你可以寫簡單類型和複雜也是Java對象到一個文件,你可以閱讀這個對象從文件備份並重建它。

Patrick Patrick