Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.70 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.*;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import java.util.Scanner;
  7.  
  8. public class Umang {
  9.  
  10.     private static final Scanner scan = new Scanner(System.in);
  11.  
  12.     public static void main(String[] args) {
  13.         String filename;
  14.         filename = scan.nextLine();
  15.         //Create output map.
  16.         HashMap<String, Integer> map = new HashMap<>();
  17.         BufferedReader br = null;
  18.         String log;
  19.         try {
  20.             File input = new File(filename);
  21.             br = new BufferedReader(new FileReader(input));
  22.             while ((log = br.readLine()) != null) {
  23.                 String[] data = log.split(" ");
  24.                 map.put(data[0], map.getOrDefault(data[0], 0) + 1);
  25.             }
  26.         } catch (IOException e) {
  27.             e.printStackTrace();
  28.         } finally {
  29.             if (br != null) {
  30.                 try {
  31.                     br.close();
  32.                 } catch (IOException e) {
  33.                     e.printStackTrace();
  34.                 }
  35.             }
  36.         }
  37.         //File output = new File("record_" + filename);
  38.         FileWriter writer = null;
  39.         try {
  40.             writer = new FileWriter("record_" + filename);
  41.             for (Map.Entry<String, Integer> entry : map.entrySet()) {
  42.                 writer.write(entry.getKey() + " " + entry.getValue() + "\n");
  43.             }
  44.         } catch (IOException e) {
  45.             e.printStackTrace();
  46.         } finally {
  47.             if (writer != null) {
  48.                 try {
  49.                     writer.close();
  50.                 } catch (IOException e) {
  51.                     e.printStackTrace();
  52.                 }
  53.             }
  54.         }
  55.  
  56.     }
  57.  
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement