How do you write Map?<file, integer=""> in .txt Java</file,>



  • There is a method that removes intermediate values:

      public void  IntermediateResultWrite(Map<File, Integer> hm){
        Set<Map.Entry<File, Integer>> set = hm.entrySet();
    
        for (Map.Entry&lt;File, Integer&gt; me : set) {
            System.out.print(Thread.currentThread().getName() + ":"+me.getKey() + ": ");
            System.out.println(me.getValue());
        }
    

    I'm gonna have to write down the intermediary values in .txt.



  • Map<File, Integer> map = new HashMap<>();
    map.put(new File("somefile1"), 1);
    map.put(new File("somefile2"), 2);
    
    try ( PrintWriter writer = new PrintWriter(new File("/path/to/output.txt")) ) {
        for ( Map.Entry<File, Integer> entry : map.entrySet() ) {
            writer.write(Thread.currentThread().getName() + ":" + entry.getKey() + ": " + entry.getValue() + "\n");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    

    If you need to write in a file that is the key to the mob, remove try-with-resource inside the cycle.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2