how to show the hex detail of a file in java

Solutions on MaxInterview for how to show the hex detail of a file in java by the best coders in the world

showing results for - "how to show the hex detail of a file in java"
Hajar
18 Nov 2020
1//Just some image on my hard drive
2File file = new File("C:\\Users\\%username%\\Pictures\\Memes\\What_If_I_Told_You.jpg");
3StringBuilder builder = new StringBuilder();
4try {
5    FileInputStream fin = new FileInputStream(file);
6    byte[] buffer = new byte[1024];
7    int bytesRead = 0;
8    while((bytesRead = fin.read(buffer)) > -1)
9        for(int i = 0; i < bytesRead; i++)
10            builder.append(String.format("%02x", buffer[i] & 0xFF)).append(i != bytesRead - 1 ? " " : "");
11} catch (IOException e) {
12    e.printStackTrace();
13}
14System.out.println(builder.toString());