1. import java.io.File;
  2. import java.io.IOException;
  3. import java.nio.file.Files;
  4. import java.nio.file.Path;
  5. import java.nio.file.Paths;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. public class BinaryFileMetadata {
  9. public static void main(String[] args) {
  10. // Example usage:
  11. File file = new File("example.bin"); // Replace with your binary file
  12. if (file.exists()) {
  13. Map<String, Object> metadata = getMetadata(file);
  14. System.out.println(metadata);
  15. } else {
  16. System.out.println("File not found.");
  17. }
  18. }
  19. public static Map<String, Object> getMetadata(File file) {
  20. Map<String, Object> metadata = new HashMap<>();
  21. try {
  22. metadata.put("file_name", file.getName());
  23. metadata.put("file_path", file.getAbsolutePath());
  24. metadata.put("file_size_bytes", file.length());
  25. metadata.put("last_modified", file.lastModified());
  26. // Check if the file is empty
  27. metadata.put("is_empty", file.length() == 0);
  28. //Basic file type detection based on extension (can be improved)
  29. String extension = file.substring(file.getName().lastIndexOf(".")).toLowerCase();
  30. metadata.put("file_extension", extension);
  31. // Example: Check if it's a readable file
  32. metadata.put("is_readable", Files.isReadable(Paths.get(file.getAbsolutePath())));
  33. } catch (IOException e) {
  34. System.err.println("Error getting metadata: " + e.getMessage());
  35. }
  36. return metadata;
  37. }
  38. }

Add your comment