/** * Deletes a directory recursively. * * @param directory directory to delete * @throws IOException in case deletion is unsuccessful */ public static void deleteDirectory(File directory) throws IOException { if (!directory.exists()) { return; } if (!isSymlink(directory)) { cleanDirectory(directory); } if (!directory.delete()) { String message = "Unable to delete directory " + directory + "."; throw new IOException(message); } }
public static File[] listFiles(File dir,boolean deep){ return listFiles(dir, deep, null); }
/** * Opens a {@link FileOutputStream} for the specified file, checking and * creating the parent directory if it does not exist. * <p> * At the end of the method either the stream will be successfully opened, * or an exception will have been thrown. * <p> * The parent directory will be created if it does not exist. * The file will be created if it does not exist. * An exception is thrown if the file object exists but is a directory. * An exception is thrown if the file exists but cannot be written to. * An exception is thrown if the parent directory cannot be created. * * @param file the file to open for output, must not be {@code null} * @return a new {@link FileOutputStream} for the specified file * @throws IOException if the file object is a directory * @throws IOException if the file cannot be written to * @throws IOException if a parent directory needs creating but that fails * @since 1.3 */ public static FileOutputStream openOutputStream(File file) throws IOException { return openOutputStream(file, false); }
/** * This implementation returns a File reference for the underlying class path * resource, provided that it refers to a file in the file system. * @see bingo.lang.io.Resources.util.ResourceUtils#getFile(java.net.URL, String) */ @Override public File getFile() throws NestedIOException { try { URL url = getURL(); if (url.getProtocol().startsWith(Urls.PROTOCOL_VFS)) { return VfsResourceDelegate.getResource(url).getFile(); } return Files.fromUrl(url, getDescription()); } catch (IOException e) { throw Exceptions.wrap(e); } }
/** * Deletes a file, never throwing an exception. If file is a directory, delete it and all sub-directories. * <p> * The difference between File.delete() and this method are: * <ul> * <li>A directory to be deleted does not have to be empty.</li> * <li>No exceptions are thrown when a file or directory cannot be deleted.</li> * </ul> * * @param file file or directory to delete, can be {@code null} * @return {@code true} if the file or directory was deleted, otherwise * {@code false} */ public static boolean deleteQuietly(File file) { if (file == null) { return false; } try { if (file.isDirectory()) { cleanDirectory(file); } } catch (Exception ignored) { } try { return file.delete(); } catch (Exception ignored) { return false; } }
public static void save(String filename,byte[] data) throws IOException { OutputStream out = null; try{ File file = new File(filename); File dir = getDirecotry(file); if(!dir.exists()){ dir.mkdirs(); } out = new FileOutputStream(file); out.write(data); out.flush(); }finally{ IO.close(out); } }
for (File file : files) { try { forceDelete(file); } catch (IOException ioe) { exception = ioe;
deleteDirectory(file); } else { boolean filePresent = file.exists();
/** * This implementation returns a File reference for the underlying class path * resource, provided that it refers to a file in the file system. */ protected File getFile(URI uri) throws IOException { if (uri.getScheme().startsWith(Urls.PROTOCOL_VFS)) { return VfsResourceDelegate.getResource(uri).getFile(); } return Files.fromUri(uri, getDescription()); }
/** * This implementation determines the underlying File * (or jar file, in case of a resource in a jar/zip). */ @Override protected File getFileForLastModifiedCheck() throws IOException { URL url = getURL(); if (Urls.isJarUrl(url)) { URL actualUrl = Urls.getJarFileURL(url); if (actualUrl.getProtocol().startsWith(Urls.PROTOCOL_VFS)) { return VfsResourceDelegate.getResource(actualUrl).getFile(); } return Files.fromUrl(actualUrl, "Jar URL"); } else { return getFile(); } }
public static File[] listFiles(File dir,boolean deep, FilenameFilter filter){ List<File> list = new ArrayList<File>(); listFiles(dir, deep, list, filter); return list.toArray(new File[list.size()]); }
/** * Implements the same behaviour as the "touch" utility on Unix. It creates * a new file with size 0 or, if the file exists already, it is opened and * closed without modifying it, but updating the file date and time. * * @param file the File to touch * @throws IOException If an I/O problem occurs */ public static void touch(File file) throws IOException { if (!file.exists()) { OutputStream out = openOutputStream(file); IO.close(out); } boolean success = file.setLastModified(System.currentTimeMillis()); if (!success) { throw new IOException("Unable to set the last modification time for " + file); } }
@Override public boolean accept(File f) { if(null != filter && !filter.accept(dir, f.getName())) { return false; } if(f.isFile()){ list.add(f); }else if(deep && f.isDirectory()){ listFiles(f,deep,list,filter); } return false; } });