Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.19 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.RandomAccessFile;
  3. import java.nio.file.Files;
  4. import java.nio.file.Path;
  5. import java.nio.file.Paths;
  6. import java.util.*;
  7. import java.util.stream.Collectors;
  8.  
  9. public class Main {
  10.     public static void main(String[] args) throws IOException {
  11.        new Main().printBytesAndNames();
  12.     }
  13.  
  14.     private void printBytesAndNames() throws IOException {
  15.         byte[] data = new byte[1000];
  16.         Map<Integer, String> sizeAndName = new HashMap<>();
  17.         for(Path p : listFiles()) {
  18.             new RandomAccessFile(p.toFile(), "r").readFully(data);
  19.             int sum = 0;
  20.             for(byte b : data) {
  21.                 sum += b;
  22.             }
  23.  
  24.             sizeAndName.put(sum, p.getFileName().toString());
  25.         }
  26.  
  27.         List<Integer> intList = new ArrayList<>(sizeAndName.keySet());
  28.         Collections.sort(intList);
  29.         for(int i : intList) {
  30.             System.out.println(i + ", " + sizeAndName.get(i));
  31.         }
  32.     }
  33.  
  34.      List<Path> listFiles() throws IOException {
  35.         return Files.list(Paths.get("./src/images"))
  36.                 .filter(Files::isRegularFile)
  37.                 .collect(Collectors.toList());
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement