import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
public class BinaryFileMetadata {
public static void main(String[] args) {
// Example usage:
File file = new File("example.bin"); // Replace with your binary file
if (file.exists()) {
Map<String, Object> metadata = getMetadata(file);
System.out.println(metadata);
} else {
System.out.println("File not found.");
}
}
public static Map<String, Object> getMetadata(File file) {
Map<String, Object> metadata = new HashMap<>();
try {
metadata.put("file_name", file.getName());
metadata.put("file_path", file.getAbsolutePath());
metadata.put("file_size_bytes", file.length());
metadata.put("last_modified", file.lastModified());
// Check if the file is empty
metadata.put("is_empty", file.length() == 0);
//Basic file type detection based on extension (can be improved)
String extension = file.substring(file.getName().lastIndexOf(".")).toLowerCase();
metadata.put("file_extension", extension);
// Example: Check if it's a readable file
metadata.put("is_readable", Files.isReadable(Paths.get(file.getAbsolutePath())));
} catch (IOException e) {
System.err.println("Error getting metadata: " + e.getMessage());
}
return metadata;
}
}
Add your comment