Question

I have some results and I store these in an ArrayList. I would to save (output) these a txt file to my sdcard. But I don't have idea. I try a code, but the file isn't made. If the file is made I would write to the next activity. And I would like to know how I write my ArrayList to the next Activity.

points: a list, store Google Map Marker positions szoveg: a 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();

        }

And I try another application this but doesn't work (I use writeTest() in onCreate):

@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());
    }
}
Was it helpful?

Solution

If you need to save the List as Text, work with an ForEach Loop or Iterator, run over all Elements and give every String to the FileWriter and save it as Text on your SD Card.

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

If you use an ObjectOutputStream, you can write primitive Types and also complex Java Objects to an file and you could read this object back from file and reconstitute it.

Patrick

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top