我正在尝试读取 ArrayList 并将其写入文件,目前正在使用 ObjectOutputStream 写入文件。
fout = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(arraylist);
fout.close();
arraylist
变量是对象的 ArrayList。此方法工作正常,但它以人类不可读的格式打印到文件中。
有没有什么方法可以(相对容易地)实现相同的功能,以便以人类可读的方式将对象写入文件?
最佳答案
您可以使用人类可读的 Json 表示。
例如使用 jackson :
ObjectWriter ow = new ObjectMapper().writer();
ow.writeValue(fout, arraylist);
回读:
List<E> myObjects = new ObjectMapper().readValue(inputStream,
new TypeReference<List<E>>(){});
关于java - ObjectOutputStream 人类可读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32152221/