2014-03-30 7 views
0

몇 가지 결과가 있으며이를 ArrayList에 저장합니다. 나는 (출력) 이러한 txt 파일을 내 sdcard에 저장하고 싶습니다. 그러나 나는 모른다. 코드를 시도했지만 파일이 만들어지지 않았습니다. 파일이 만들어지면 다음 활동에 쓸 것입니다. 그리고 어떻게 ArrayList를 다음 Activity에 쓰는지 알고 싶습니다.내 ArrayList를 txt 파일에 쓰는 방법은 무엇입니까?

포인트 : 목록, 저장 Google지도 표시 자 위치 szoveg : String 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 카드에 텍스트로 저장합니다. 당신은 ObjectOutputStream에를 사용하는 경우

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

, 당신은 파일에 원시 유형 및 복잡한 자바 객체를 작성할 수 있으며 다시 파일에서이 개체를 읽고 그것을 재구성 할 수있다.

패트릭

관련 문제