/** * Normalize a file path to a clean form. * This method may for example make sure that the returned file uses * the natural case on Windows; that old Windows 8.3 filenames are changed to the long form; * that relative paths are changed to be * absolute; that <code>.</code> and <code>..</code> sequences are removed; etc. * Unlike {@link File#getCanonicalFile} this method will not traverse symbolic links on Unix. * <p>This method involves some overhead and should not be called frivolously. * Generally it should be called on <em>incoming</em> pathnames that are gotten from user input * (including filechoosers), configuration files, Ant properties, etc. <em>Internal</em> * calculations should not need to renormalize paths since {@link File#listFiles}, * {@link File#getParentFile}, etc. will not produce abnormal variants. * @param file file to normalize * @return normalized file * @since 4.48 */ public static File normalizeFile(final File file) { File ret = normalizeFileCached(file); assert assertNormalized(ret); return ret; }
static void addFileChangeListenerImpl(Logger logger, FileChangeListener listener, File path) { assert FileUtil.assertNormalized(path); logger.log(Level.FINE, "addFileChangeListener {0} @ {1}", new Object[]{listener, path}); synchronized (holders) { Map<File, FileChangeImpl> f2H = holders.get(listener); if (f2H == null) { f2H = new HashMap<File, FileChangeImpl>(); holders.put(listener, f2H); } final FileChangeImpl prev = f2H.get(path); if (prev != null) { StringBuilder sb = new StringBuilder(); sb.append("Already listening to ").append(path); sb.append("\nnew listener : ").append(listener); sb.append("\nholder listener: ").append(prev.get()); throw new IllegalArgumentException(sb.toString()); } final FileChangeImpl holder = new FileChangeImpl(listener, path); f2H.put(path, holder); holder.locateCurrent(); } }
/** Finds appropriate java.io.File to FileObject if possible. * If not possible then null is returned. * This is the inverse operation of {@link #toFileObject}. * @param fo FileObject whose corresponding File will be looked for * @return java.io.File or null if no corresponding File exists. * @since 1.29 */ public static File toFile(FileObject fo) { File retVal = (File) fo.getAttribute("java.io.File"); // NOI18N; if (retVal == null) { URL fileURL = URLMapper.findURL(fo, URLMapper.INTERNAL); if (fileURL == null || !"file".equals(fileURL.getProtocol())) { //NOI18N fileURL = URLMapper.findURL(fo, URLMapper.EXTERNAL); } if ((fileURL != null) && "file".equals(fileURL.getProtocol())) { retVal = Utilities.toFile(URI.create(fileURL.toExternalForm())); } } if (retVal != null) { retVal = normalizeFile(retVal); } assert assertNormalized(retVal); return retVal; }