1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| /**
* Returns the extension of the given file.
*
* @param file
* @return the extension
*/
public static String getExtension(File file) {
return getExtension(file.getName());
}
/**
* Returns the extension of the given filename.
*
* @param filename
* @return the extension
*/
public static String getExtension(String filename) {
int index = filename.lastIndexOf('.');
return index == -1 ? "" : filename.substring(index + 1).toLowerCase();
}
/**
* Returns the file system icon for given file extension (<code>fileExt</code>).
*
* @param fileExt The file extension.
* @return The icon corresponding to extension.
*/
public static Icon getIconForExtension(String fileExt) {
if (s_fileTypeIcons.get(fileExt) != null) {
return s_fileTypeIcons.get(fileExt);
}
File file = null;
Icon icon = null;
try {
// Create a temporary file with the specified extension
file = File.createTempFile("icon", "." + fileExt);
// Get file system view
FileSystemView view = FileSystemView.getFileSystemView();
icon = view.getSystemIcon(file);
// Delete the temporary file
delete(file, 0);
} catch (IOException ioe) {
s_logger.error(ioe.getMessage(), ioe);
}
s_fileTypeIcons.put(fileExt, icon);
return icon;
}
/**
* Returns the file system description for given file extension (<code>fileExt</code>).
*
* @param fileExt The file extension.
* @return The description corresponding to extension.
*/
public static String getFileTypeDescription(String fileExt) {
if (s_fileTypeDescs.get(fileExt) != null) {
return s_fileTypeDescs.get(fileExt);
}
File file = null;
String desc = null;
try {
// Create a temporary file with the specified extension
file = File.createTempFile("desc", "." + fileExt);
// Get file system view
FileSystemView view = FileSystemView.getFileSystemView();
desc = view.getSystemTypeDescription(file);
// Delete the temporary file
delete(file, 0);
} catch (IOException ioe) {
s_logger.error(ioe.getMessage(), ioe);
}
s_fileTypeDescs.put(fileExt, desc);
return desc;
} |