Java:存储hashmap的理想文件格式?
我需要将哈希映射的arraylist保存到外部文件。我可以使用除文本文件以外的任何格式,因为程序设置为忽略文本文件,特别是任何扩展名为.txt的文件。hashmaps非常简单,只包含那些单词的计数。理想的文件格式是什么?您可以使用 你可以稍后通过电子邮件阅读Java:存储hashmap的理想文件格式?,java,file,hashmap,Java,File,Hashmap,我需要将哈希映射的arraylist保存到外部文件。我可以使用除文本文件以外的任何格式,因为程序设置为忽略文本文件,特别是任何扩展名为.txt的文件。hashmaps非常简单,只包含那些单词的计数。理想的文件格式是什么?您可以使用 你可以稍后通过电子邮件阅读 Properties properties = new Properties(); try (InputStream input = new FileInputStream("/foo.properties")) { proper
Properties properties = new Properties();
try (InputStream input = new FileInputStream("/foo.properties")) {
properties.load(input);
}
// ... (Properties implements Map, you could just treat it like a Map)
另见:
你可以用
你可以稍后通过电子邮件阅读
Properties properties = new Properties();
try (InputStream input = new FileInputStream("/foo.properties")) {
properties.load(input);
}
// ... (Properties implements Map, you could just treat it like a Map)
另见:
您可以使用序列化:
ObjectOutputStream stream = null;
try
{
File f = new File(filename);
stream = new ObjectOutputStream(new FileOutputStream(f));
stream.writeObject(your_arraylist);
}
catch (IOException e)
{
// Handle error
}
finally
{
if (stream != null)
{
try
{
stream.close();
}
catch (Exception e) {}
}
}
并使用以下内容阅读:
ObjectInputStream stream = null;
try
{
stream = new ObjectInputStream(new FileInputStream(f));
your_arrayList = (your_arrayList type here)stream.readObject();
}
catch (Throwable t)
{
// Handle error
}
finally
{
if (stream != null)
{
try
{
stream.close();
}
catch (Exception e) {}
}
}
您可以使用序列化:
ObjectOutputStream stream = null;
try
{
File f = new File(filename);
stream = new ObjectOutputStream(new FileOutputStream(f));
stream.writeObject(your_arraylist);
}
catch (IOException e)
{
// Handle error
}
finally
{
if (stream != null)
{
try
{
stream.close();
}
catch (Exception e) {}
}
}
并使用以下内容阅读:
ObjectInputStream stream = null;
try
{
stream = new ObjectInputStream(new FileInputStream(f));
your_arrayList = (your_arrayList type here)stream.readObject();
}
catch (Throwable t)
{
// Handle error
}
finally
{
if (stream != null)
{
try
{
stream.close();
}
catch (Exception e) {}
}
}
+当然,你应该确保关闭这些溪流+当然,你应该确保关闭这些溪流;请记住,Java并不关心扩展,除非您让它关心扩展,例如,通过过滤扩展。Windows关心它,但Java不关心您是否将其命名为Java.txt或Java.reallyandomextension,如果它包含文本数据,Java可以将其作为文本文件读取,而不考虑其扩展名。@Brian-我不知道这一点。谢谢这实际上解决了我的问题。记住,Java不关心扩展,除非你让它关心扩展,例如,通过过滤扩展。Windows关心它,但Java不关心您是否将其命名为Java.txt或Java.reallyandomextension,如果它包含文本数据,Java可以将其作为文本文件读取,而不考虑其扩展名。@Brian-我不知道这一点。谢谢这实际上解决了我的问题。