/** * This class of tool classes for working with Project resources * <p> * Note that project resources must be placed in the project catalog SRC, or you can apply the project external resources to indicate the absolute path */public class Fileutil {// The project file must be in the SRC directory under one of the following 3 subfolders private static final string file = "file/";//store ordinary file private static final string IMAGE = "images/"; Store picture file private static final String audio = "music/";//store audio file private static final hashmap<string, string> MAP = new Hashmap<string, string> (), Static {map.put ("file", file); Map.put ("image", image); Map.put ("audio", audio);} /** * Returns the URL address of the resource file * @param type resource file types, including files, pictures, and audio * @param path resource file paths to external files with an absolute path (such as c:/me.jpg) if an internal file is a file name (such as: me.jpg) * The URL address of the @return resource file */public static URL GetURL (string type, string path) {string dir = Map.get (type); Return Urlclassloader.getsystemclassloader (). getresource (dir + path); }//Gets the file resource public static "file GetFile (String path) {URL url = GetURL (" file ", path); if (url = = null) {return null; } return new File (Url.getfile ()); }//Get Picture resource public static image GetImage (StRing path) {URL url = GetURL ("image", path); if (url = = null) {return null; } return Toolkit.getdefaulttoolkit (). GetImage (URL); }//Get audio resource public static AudioClip Getaudio (String path) {URL url = GetURL ("audio", path); if (url = = null) {return null; } return Applet.newaudioclip (URL); } }
The usage is simple and clear. Need a friend to pick up.
[Java Swing tycoon] Java Common Files, pictures, audio = = = Generic Tool class