ハッシュマップの配列リストを外部ファイルに保存する必要があります。.txtプログラムはテキスト ファイル (特に、拡張子を持つもの) を無視するように設定されているため、テキスト ファイル以外の任意の形式を使用できます。ハッシュマップは非常に単純で、それらの単語の数を含む単語だけです。これを保存するのに理想的なファイル形式は何ですか?
2 に答える
            6        
        
		
を使用できますjava.util.Properties。
Properties properties = new Properties();
properties.putAll(yourMap); // You could also just use Properties in first place.
try (OutputStream output = new FileOutputStream("/foo.properties")) {
    properties.store(output, null);
}
後で読むことができます
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)
以下も参照してください。
于 2012-10-15T23:43:01.523   に答える
    
    
            1        
        
		
シリアル化を使用できます:
    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) {}
        }
    }
    于 2012-10-15T23:52:49.073   に答える