gpt4 book ai didi

java - 在android中保存/读取文件

转载 作者:行者123 更新时间:2023-12-01 12:55:32 25 4
gpt4 key购买 nike

我想保存到 android 中的一个文件,我的一些 arrayList 之后将被删除。我已经有两种从 android 文件写入/读取的方法 here但问题是我希望这两种方法可以做到这一点:

  • 第一个方法必须保存 arraylist 的元素,然后如果我再次调用它,它不会将新元素写入同一行,而是写入另一行

    • 第二个必须读取一行(例如,我向方法提供哪一行,它返回该行包含的内容)

文件看起来像这样:

firstelem
secondelem
thridelem

anotherelem
another ..

这可以在 android java 中实现吗?PS:我不需要数据库。

更新这是我的方法:

 private void writeToFile(String data) {
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("config.txt", Context.MODE_PRIVATE));
outputStreamWriter.write(data);
outputStreamWriter.close();
}
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}



private String readFromFile() {

String ret = "";

try {
InputStream inputStream = openFileInput("config.txt");

if ( inputStream != null ) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();

while ( (receiveString = bufferedReader.readLine()) != null ) {
stringBuilder.append(receiveString);
// stringBuilder.append("\\n");
}

inputStream.close();
ret = stringBuilder.toString();
}
}
catch (FileNotFoundException e) {
Log.e("login activity", "File not found: " + e.toString());
} catch (IOException e) {
Log.e("login activity", "Can not read file: " + e.toString());
}

return ret;
}

最佳答案

使用您链接到的保存方法,您可以创建要使用StringBuilder保存的文本:

public String makeArrayListFlatfileString(List<List<String>> listOfLists)
{
StringBuilder sb = new StringBuilder();
if (!listOfLists.isEmpty()) {
// this assumes all lists are the same length
int listLengths = listOfLists.get(0).size();
for (int i=0; i<listLengths; i++)
{
for (List<String> list : listOfLists)
{
sb.append(list.get(i)).append("\n");
}
sb.append("\n"); // blank line after column grouping
}
}
return sb.toString();
}

解析同一文件中的内容(再次假设相同长度的列表和字符串输入):

public List<List<String>> getListOfListsFromFlatfile(String data)
{
// split into lines
String[] lines = data.split("\\n");
// first find out how many Lists we'll need
int numberOfLists = 0;
for (String line : lines){
if (line.trim().equals(""))
{
// blank line means new column grouping so stop counting
break;
}
else
{
numberOfLists++;
}
}
// make enough empty lists to hold the info:
List<List<String>> listOfLists = new ArrayList<List<String>>();
for (int i=0; i<numberOfLists; i++)
{
listOfLists.add(new ArrayList<String>());
}
// keep track of which list we should be adding to, and populate the lists
int listTracker = 0;
for (String line : lines)
{
if (line.trim().equals(""))
{
// new block so add next item to the first list again
listTracker = 0;
continue;
}
else
{
listOfLists.get(listTracker).add(line);
listTracker++;
}
}
return listOfLists;
}

关于java - 在android中保存/读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23938667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com