我有一些结果,我将它们存储在ArrayList中。我想将这些txt文件保存(输出)到我的SD卡。但我不知道。我尝试了一个代码,但文件还没有制作完成。如果文件生成,我会写入下一个活动。我想知道如何将ArrayList写入下一个Activity。
点数:列表,存储谷歌地图标记位置 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)
如果您需要将List保存为文本,请使用ForEach循环或Iterator,运行所有Elements并将每个String提供给FileWriter并将其另存为SD卡上的Text。
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对象写入文件,然后可以从文件中读取此对象并重新构建它。
帕特里克