Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Scanner scan = new Scanner(System.in);
- //Read file name.
- String filename = scan.nextLine();
- //Create output map.
- HashMap<String, Integer> map = new HashMap<>();
- BufferedReader br = null;
- String log;
- try {
- File input = new File(filename);
- br = new BufferedReader(new FileReader(input));
- while ((log = br.readLine()) != null) {
- String[] data = log.split(" ");
- map.put(data[0], map.getOrDefault(data[0], 0) + 1);
- }
- }catch(IOException e){
- e.printStackTrace();;
- }finally {
- if(br != null) {
- try {
- br.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- File output = new File( "record_" + filename);
- FileWriter writer = null;
- try {
- writer = new FileWriter("record_" + filename);
- for(Map.Entry<String, Integer> entry: map.entrySet()){
- writer.write(entry.getKey() + " " + entry.getValue() + "\n");
- }
- } catch (IOException e) {
- e.printStackTrace();
- }finally {
- if(writer != null){
- try {
- writer.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement