📄 fileutil.java
字号:
// NetWare volume: if (System.getProperty("os.name").startsWith("NetWare") && path.length() >=3 && path.indexOf(':') > 0) return true; return false; } // Used in few places. public static String getCanonicalPath(String name ) { if( name==null ) return null; File f = new File(name); try { return f.getCanonicalPath(); } catch (IOException ioe) { ioe.printStackTrace(); return name; // oh well, we tried... } } /** method to return the Localized version of the file whose * name is passed as an argument. * * The method performs a resource lookup in a manner similar to the * one specified by java.util.ResourceBundle. * * In the case of 'typed' files (files whose name is [file].[ftype]) * search for localized versions of the file are looked for: * * file + "_" + language1 + "_" + country1 + "_" + variant1 + "." + ftype * file + "_" + language1 + "_" + country1 + "." + ftype * file + "_" + language1 + "." + ftype * file + "_" + language2 + "_" + country2 + "_" + variant2 "." + ftype * file + "_" + language2 + "_" + country2 "." + ftype * file + "_" + language2 + "." + ftype * file + "." + ftype * * Where language1, country1, variant1 are associated with the Locale * passed as an argument and language2, country2, variant2 are associated * with the default Locale passed as argument. * * For example, if the preferred Locale is <CODE>es_AR_POSIX</CODE> and * the default Locale passed is <CODE>fr_CA_WIN</CODE>, and the requested * pathname is <CODE>/foo/bar/index.html</CODE>, then a search for * the following localized versions of that file will be done, in order: *<UL> *<LI>/foo/bar/index_es_AR_POSIX.html</LI> *<LI>/foo/bar/index_es_AR.html</LI> *<LI>/foo/bar/index_es_AR.html</LI> *<LI>/foo/bar/index_es.html</LI> *<LI>/foo/bar/index_fr_CA_WIN.html</LI> *<LI>/foo/bar/index_fr_CA.html</LI> *<LI>/foo/bar/index_fr.html</LI> *<LI>/foo/bar/index.html</LI> *</UL> * * If the resource passed has no 'ftype' component, then the same * rules above apply, with the exception that '.' + ftype are not * concatenated. * * @param path the pathname for the resource whose localized version * we are seeking * @param loc the Locale we are interested in. * @param fbLoc the fallback Locale to use if unsuccessful * * @return a String with the path of the "best localized match" for * the file whose path has been passed as argument. */ public static String getLocalizedResource (String path, Locale loc, Locale fbLoc) { String locRes = null; if (null != loc) locRes = getLocalizedResource (path, loc); if (null != locRes) return locRes; if (null != fbLoc) locRes= getLocalizedResource (path, fbLoc); if (null != locRes) return locRes; return path; } /** utility method to return the name of the localized file whose * name best-matches the Locale passed as an argument * * @param base the directory that is the Document Base for the context * in which the 'path' passed is expected to be located * @param path the pathname for the file that is being searched for its * best-matched Localized version * @param loc the requested Locale to match * @param fbloc the fallback Locale if the requested one not found * * @return the name of the file that best-matched the Locale requested */ public static String getLocalizedFile (String base, String path, Locale loc, Locale fbloc) { String locRes = null; if (null != loc) locRes = getLocalizedFile (base, path, loc); if (null != locRes) return locRes; if (null != fbloc) locRes= getLocalizedFile (base, path, fbloc); if (null != locRes) return locRes; return safePath (base, path); } /** utility method to return the name of the localized file whose * name best-matches the Locale passed as an argument * * @param base the directory that is the Document Base for the context * in which the 'path' passed is expected to be located * @param path the pathname for the file that is being searched for its * best-matched Localized version * @param loc the requested Locale to match * * @return the name of the file that best-matched the Locale requested */ public static String getLocalizedFile (String base, String path, Locale loc) { return getLocalizedResource (base, path, loc, true); } /** utility method to get the best-match for getting the Localized * version of the file whose 'path' is passed as an argument, using * the 'loc' Locale, and performing 'safePath'checks * * The method performs a resource lookup in a manner similar to the * one specified by java.util.ResourceBundle. * * In the case of 'typed' files (files whose name is [file].[ftype]) * search for localized versions of the file are looked for: * * file + "_" + language1 + "_" + country1 + "_" + variant1 + "." + ftype * file + "_" + language1 + "_" + country1 + "." + ftype * file + "_" + language1 + "." + ftype * file + "." + ftype * * Where language1, country1, variant1 are associated with the Locale * passed as an argument. * * For example, if the preferred Locale is <CODE>es_AR_POSIX</CODE> * pathname is <CODE>/foo/bar/index.html</CODE>, then a search for * the following localized versions of that file will be done, in order: *<UL> *<LI>/foo/bar/index_es_AR_POSIX.html</LI> *<LI>/foo/bar/index_es_AR.html</LI> *<LI>/foo/bar/index_es_AR.html</LI> *<LI>/foo/bar/index_es.html</LI> *</UL> * * @param base the document base for the path passed * @param rPath the path to the file relative to the base * @param loc the Locale preferred * @param safechek perform safePath checks * * @return the name of the file that best-matched the Locale requested * null, if no localization matching could be done. */ public static String getLocalizedResource (String base, String rPath, Locale loc, boolean safechek) { if (null == rPath) return null; String path = (safechek ? safePath (base, rPath) : rPath); return getLocalizedResource (path, loc); } /** utility method to get the best-match for getting the Localized * version of the file whose 'path' is passed as an argument, using * the 'loc' Locale, and performing 'safePath'checks * * The method performs a resource lookup in a manner similar to the * one specified by java.util.ResourceBundle. * * See above explanation of lookup order. * * @param path the path to the file * @param loc Locale with which to match. * * @return the name of the file that best-matched the Locale requested * null if no Locale match existed. Invokers of the method * need to test whether null is returned, and then decide * what to do (whether to default to the original path * or not). */ public static String getLocalizedResource (String path, Locale loc) { if (null == path) return null; String wPath = null; String pPath = null; // parent path String gpPath = null; // grand-parent path String rLang = loc.getLanguage(); String rCoun = loc.getCountry(); String rVar = loc.getVariant(); int pathLen = path.length(); int lastParen = path.lastIndexOf (File.separator); int lastDot = path.lastIndexOf ('.'); // evaluate the following conditions // // a. path passed includes the File.separator // the path specified is a 'dotted' file if: // lastParen < lastDot // // b. path passed has not File.separator // the path specified is a 'dotted' file if: // -1 < lastDot // // c. path is not a dotted file if: // -1 == lastDot // // // 'pathBase' is anything to the left of the 'lastDot' position // (if 'lastDot' is -1, 'pathBase' is 'path') // String pathBase = null; String dotTail = null; if (lastParen < lastDot && lastDot > 0) { pathBase = path.substring (0, lastDot); dotTail = path.substring (lastDot); } else pathBase = path; File file = null; gpPath = pathBase + '_' + rLang; // first, try the language+country+variant match // if (null != rCoun && ! ("".equals (rCoun)) ) { pPath = gpPath + '_' + rCoun; if (null != rVar && ! ("".equals (rVar)) ) { wPath = pPath + '_' + rVar; // append the dotTail if necessary // if (null != dotTail) wPath += dotTail; // perform existence test // if ((new File(wPath).exists())) return wPath; } // append the dotTail if necessary // if (null != dotTail) pPath += dotTail; if ((new File(pPath)).exists()) return pPath; } // append the dotTail if necessary // if (null != dotTail) gpPath += dotTail; if ((new File(gpPath)).exists()) return gpPath; return null; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -