gpt4 book ai didi

java - 将 ArrayList 写入/读取文件的奇怪问题

转载 作者:行者123 更新时间:2023-11-29 05:29:03 25 4
gpt4 key购买 nike

我的 Activity 中有这段代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_top_jokes_body);

final GlobalsHolder globals = (GlobalsHolder)getApplication();

TextView text = (TextView) findViewById(R.id.txtJoke);
text.setText(globals.getList().get(globals.clickedPosition));

ArrayList<String> jokeBodyList = new ArrayList<String>();


jokeBodyList.addAll(readFromFile());
jokeBodyList.add(text.getText().toString());

System.out.println("List content(jokeBodyList): " + jokeBodyList.toString());
writeToFile(jokeBodyList);
System.out.println("The file content(new)" + readFromFile().toString());

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.top_jokes_body, menu);
return true;
}

/* Write content to a file */
private void writeToFile(ArrayList<String> list) {

FileOutputStream fos;
if(list != null){
try {
fos = openFileOutput("jokesBody2.bjk",Context.MODE_PRIVATE);
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(list.toString());
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else{
try {
fos = openFileOutput("jokesBody2.bjk",Context.MODE_PRIVATE);
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject("");
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

/* Read file's content */
private ArrayList<String> readFromFile() {
File file = new File("jokesBody.bjk");
String ret = "";
ArrayList<String> list = new ArrayList<String>();
try {
InputStream inputStream = openFileInput(file.toString());

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

while ((receiveString = bufferedReader.readLine()) != null) {
list.add(receiveString);
}

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

return list;
}

主要思想是将文件的内容传递给 ArrayList,然后将另一个值添加到相同的 ArrayList,然后将列表写回文件。我使用两种方法来读取和写入文件,问题是写入和读取有问题。我的意思是这2个打印

System.out.println("List content(jokeBodyList): " + jokeBodyList.toString());
System.out.println("The file content(new)" + readFromFile().toString());

正在返回一些奇怪的值。这是输出:

02-13 09:10:22.482: I/System.out(2286): List content(jokeBodyList): [����tT[���sr�java.util.ArrayListx����a��I�sizexp���w���, t�����sr�java.util.ArrayListx����a��I�sizexp���w���   t��t��t��t�.joke 13joke 12Joke 8 ,joke 14 ,joke 13 ,Joke 9t� ,Joke 9t� ,Joke 8t�t� ,Ceco's joket�    ,joke 13xt�     ,joke 12x,  ,joke 12], joke 10]
02-13 09:10:22.542: I/System.out(2286): The file content(new)[����tT[���sr�java.util.ArrayListx����a��I�sizexp���w���, t�����sr�java.util.ArrayListx����a��I�sizexp���w��� t��t��t��t�.joke 13joke 12Joke 8 ,joke 14 ,joke 13 ,Joke 9t� ,Joke 9t� ,Joke 8t�t� ,Ceco's joket� ,joke 13xt� ,joke 12x, ,joke 12]]

即使文本文件为空也会发生这种情况。我缺少什么?我知道它真的很小,但我找不到它。

最佳答案

out.writeObject(list.toString()); 是完全错误的!使用 out.writeObject(list); 这就是 ObjectOutputStream 的用途。

阅读是相反的:

ObjectInputStream ois = new ObjectInputStream( new FileInputStream( file ) );
yourArrayList = (ArrayList)ois.readObject();
ois.close();

关于java - 将 ArrayList 写入/读取文件的奇怪问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21756613/

25 4 0