longlat 文件:
101.2425101.2334103.345
编码有这样的算法:
ArrayList<String> getDifferenceList(String filePath) {
File f = new File("longlat.txt");
String line, x1 = null, x2= null, x3 = null;
BufferedReader buffreader = null;
try {
buffreader = new BufferedReader(new FileReader(f));
while (( line = buffreader.readLine()) != null) {
String[] parts = line.split("");
x1 = (parts[0]);
x2 = (parts[1]);
x3 = (parts[2]);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
ArrayList<String> ret = new ArrayList<String>();
FileWriter writer;
if (x1 == null || x1.isEmpty()
|| x2 == null || x2.isEmpty()
|| x3 == null || x3.isEmpty()) {
ret.add(x1);
ret.add(x2);
ret.add(x3);
return ret;
}
int index = 0;
while (index < x1.length()
&& index < x2.length()
&& index < x3.length()) {
if (x1.charAt(index) != x2.charAt(index)
|| x1.charAt(index) != x3.charAt(index)) {
break;
}
index++;
}
ret.add(x1.substring(index, x1.length()));
ret.add(x2.substring(index, x2.length()));
ret.add(x3.substring(index, x3.length()));
return ret;
}
需要存储到文本文件中的值在 ret 的数组列表中。
我试过了 :
FileWriter writer = new FileWriter("lalala.txt");
for(String str: ret) {
writer.write(str);
} writer.close();
我把上面的编码放在了顶部:
ret.add(x1.substring(index, x1.length()));
问题:当我单击“显示 lalala 文本”按钮时没有显示任何内容。
并尝试过:
try {
OutputStreamWriter out = new OutputStreamWriter(openFileOutput ("lalala.txt",MODE_APPEND));
String text =(ret);
out.write(text);
out.write('\n');
out.close();
}
catch (java.io.IOException e) {
Toast.makeText(this,"Sorry Text could't be added",Toast.LENGTH_LONG).show
();}
}
我把上面的编码放在了顶部:
ret.add(x1.substring(index, x1.length()));
问题: 'ret' 处出错。它说“类型不匹配:无法从 ArrayList 转换为 String”
我不知道如何获取数组列表并将它们存储到文本文件中。请给我一些想法,谢谢。