0

我有一ArrayList堂课并试图将其保存到文件中。我正在编写一个小测试方法来将字符串保存到文件中,然后加载它们。

我在FileWriter类中找到了一个写字符串的方法。FileReader我在字符串中找不到任何读取方法。有没有办法做到这一点?

4

2 回答 2

3

BufferedReader与方法一起使用readLine()

于 2013-04-02T22:14:00.307 回答
1

ArrayList将 an 存储在文件中的最简单方法是使用ObjectOutputStream

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(yourfile));
oos.writeObject(list);

要恢复您使用的数组列表ObjectInputStream

ObjectInputStream ois = new ObjectInputStream(new FileInputStream(yourfile));
ArrayList list = (ArrayList) ois.readObject();

这些类的序列化格式确保您可以安全地存储具有任何字符类的字符串,而不必担心破坏输出。基于行的存储,例如在使用时BufferedReader.readLine不允许您恢复带有嵌入换行符的字符串。

于 2013-04-02T22:14:49.003 回答