Java - How to change icon of folder

2020-02-06 16:34发布

问题:

So how do I change the icon of a folder in Java (Windows system) is there a class or something cause I have searched and I can't find anything...

回答1:

Based on the comments, the folder icon that you are talking about is specified in a hidden "ini" file in the folder itself.

You could create / modify the file by reading it as text, etcera, but it is simpler to use an existing 3rd-party Java library. I've had success using the open-source ini4j Java library.



回答2:

I did that using ini4J and It is working with me under only one condition : Folder path shouldn't have any space.

The Code:

// Create destop.ini file 
writer = new BufferedWriter("your folder path without any spaces");
writer.write("");
writer.close();

// Set file attributes hidden and system and set folder as system folder and not hidden
Wini ini = new Wini("your folder path without any spaces");
String field = "icon Path" + ",0";
ini.put(".ShellClassInfo", "IconResource", field);
ini.store();
Process processCreateFile = Runtime.getRuntime().exec("attrib +h +s " + "desktop.ini file path");
Process processCreateFolder = Runtime.getRuntime().exec("attrib -h +s " + "your folder path without any spaces");