This example shows how to write HashMap object to a text file in Java such that each line of the text file contains a key and a value separated by a colon or any other separator of your choice.
How to write a HashMap object to a text file in Java?
The HashMap class in Java implements the Serializable interface so that its objects can be written or serialized to a file using the ObjectOutputStream. However, the output file it produces is not in the human-readable format and may contain junk characters.
If you want to write a HashMap object to a plain text file, you need to write custom code. In the below given example, I am going to write all the keys and values of a map object to a file such that each line of the file will contain a key-value pair separated by a colon (“:”).
The method is simple, iterate the HashMap entries and write the key followed by a colon followed by the value as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class WriteHashMapToTextFileExample { /*** Change the path ***/ final static String outputFilePath = "E:/hashmap-data.txt"; public static void main(String[] args){ //create new HashMap HashMap<Integer, String> hMapNumbers = new HashMap<Integer, String>(); //key-value pairs hMapNumbers.put(1, "One"); hMapNumbers.put(2, "Two"); hMapNumbers.put(3, "Three"); //new file object File file = new File(outputFilePath); BufferedWriter bf = null;; try{ //create new BufferedWriter for the output file bf = new BufferedWriter( new FileWriter(file) ); //iterate map entries for(Map.Entry<Integer, String> entry : hMapNumbers.entrySet()){ //put key and value separated by a colon bf.write( entry.getKey() + ":" + entry.getValue() ); //new line bf.newLine(); } bf.flush(); }catch(IOException e){ e.printStackTrace(); }finally{ try{ //always close the writer bf.close(); }catch(Exception e){} } } } |
Output file content
1 2 3 |
1:One 2:Two 3:Three |
Important Note:
The above example does not work for all types of objects including objects of a custom class. If you want to write a map object having the key or value of a custom class, you can first convert the object to JSON and then write JSON object to a text file.
If you want to read the text file back to the HashMap object, please refer to how to read text file into HashMap example.
If you want to write a HashMap object to a file and later want to retrieve the file contents in a HashMap object, you should use the ObjectOutputStream to serialize it instead of using the above code. The only difference is that the output file will not be in the human-readable format.
This example is a part of the HashMap in Java tutorial.
Please let me know your views in the comments section below.
Thank you, it works.
Glad I could help. Do checkout the quiz section.