[英]Read/Write objects and lists to file
I want to write/read from a .txt
file the attributes of my object. 我想从
.txt
文件写入/读取对象的属性。 My main struggle is that my object has a Map<String, Object>
which I'd like to write aswell. 我的主要工作是我的对象有一个
Map<String, Object>
我也想写。 I'll try to explain it with examples: 我将尝试通过示例进行解释:
I've got a Hospital, which has Departments, and every Department has Doctors. 我有一家医院,有部门,每个部门都有医生。 I want to be able to save the Hospital, but that also includes saving it's departments and the doctors in those departments, so the
.txt
file will be something like this: 我希望能够保存医院,但这还包括保存医院的部门以及这些部门中的医生,因此
.txt
文件将如下所示:
So a Hospital has a Map<String, Department>
and a Deparment has a Map<Integer, Doctor>
因此,医院有一个
Map<String, Department>
而一个部门有一个Map<Integer, Doctor>
Hospital1 医院1
-Department1 -部门1
--Doctors1 -医生1
--Doctors2 -医生2
-Department2 -部门2
Which Hospital1 would write it's parameters like HospitalName-200-100
, and below that every Department from that Hospital, same with the Dpts and Docs. 哪个Hospital1会写出它的参数,例如
HospitalName-200-100
,并且在该医院以下的每个部门中都与Dpts和Docs相同。
To do so I've implemented those methods to write/read to a file. 为此,我已经实现了这些方法来写入/读取文件。 But I don't really know how could I implement the reading and storing to the objects.
但是我真的不知道如何实现对对象的读取和存储。
I might not be clear enough, but I don't really know how to explain it in words. 我可能不够清楚,但我真的不知道该如何用语言解释。 It's a problem when it comes to Reading what I've got in a txt file, since writting it in some way its fairly easy.
读取我在txt文件中拥有的内容时会遇到问题,因为以某种方式编写它非常容易。 So I guess, it would solve the problem if I could read it line by line?
所以我想,如果我能逐行阅读它会解决问题吗? I don't really know, kinda lost here.
我真的不知道,有点在这里迷路了。
//WRITE
public void escriureText(String linia)
{
try (BufferedWriter bw = new BufferedWriter(new FileWriter(f,true)))
{
bw.write(linia);
bw.write("\r\n");
bw.close();
}
catch (Exception e)
{
throw new IllegalArgumentException(e);
}
}
//READ
public String llegirText()
{
String linia = "";
String line = "";
try (BufferedReader reader = new BufferedReader(new FileReader(f)))
{
while ((line = reader.readLine()) != null)
{
linia = linia + line + "\n";
}
}
catch (Exception e)
{
throw new IllegalArgumentException(e);
}
return linia;
}
One solution would be to use xml format. 一种解决方案是使用xml格式。 The file would look something like this then.
然后该文件看起来像这样。
<Hospital>
<Department>
<Doctor>
</Doctor>
<Doctor>
</Doctor>
</Department>
<Department>
<Doctor>
</Doctor>
<Doctor>
</Doctor>
</Department>
<Department>
<Doctor>
</Doctor>
<Doctor>
</Doctor>
</Department>
</Hospital>
To save the xml format you can use http://www.w3.org/2003/01/dom2-javadoc/org/w3c/dom/Document.html 要保存xml格式,您可以使用http://www.w3.org/2003/01/dom2-javadoc/org/w3c/dom/Document.html
code example for using xml can you find here. 您可以在此处找到使用xml的代码示例。 http://www.mkyong.com/java/how-to-create-xml-file-in-java-dom/
http://www.mkyong.com/java/how-to-create-xml-file-in-java-dom/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.