java get icon for file type

java get icon for file type

To get the icon for a file type in Java, you can use the FileSystemView class and the getSystemIcon method.

Here is an example of how to get the icon for a file with a specific extension:

String extension = "txt";
File file = new File("/path/to/file." + extension);
Icon icon = FileSystemView.getFileSystemView().getSystemIcon(file);
S‮w:ecruo‬ww.lautturi.com

The getSystemIcon method takes a File object as an argument and returns an Icon object representing the icon for the file. In this case, we create a File object for a file with the desired extension (e.g. ".txt") and pass it to the getSystemIcon method.

You can then use the getImage method of the Icon class to get the icon as an Image object, like this:

Image image = icon.getImage();

Note that the FileSystemView class only works for files on the local file system. If you need to get the icon for a file on a remote file system (e.g. over a network), you may need to use a different approach.

Created Time:2017-11-03 22:21:10  Author:lautturi