/** * Deletes the directory and all files inside it. If any file delete fails, they will be scheduled for deletion using {@link java.io.File#deleteOnExit()} */ public void close() { try { FileUtils.delete(true,(File) this); } catch (IOException e) { // } }
public static byte[] toByteArray(File file) throws IOException { return FileUtils.toByteArray(file); }
public static String toString(File file) throws IOException { return toString(file, "UTF-8"); }
/** * Copy the files in the specified directory to another directory * @param dir Source directory * @param destination Destination Directory */ public static void copyFilesInDir(File dir, File destination) throws IOException { for (File file : listFileInDir(dir)) { File destFile = new File(destination, file.getName()); if( file.isDirectory() ) { mkdir(destFile); copyFilesInDir(file,destFile); } else { copy(file,destFile); } } }
/** * Write data to a file * * @param file File to write data to * @param data Data to write */ public static void write(File file, byte[] data) throws IOException { FileUtils.write(file,data); }
/** * List all files/directories in a directory * * @param directory Directory to list files in * @param recursive If the search should be done recursively * @param includeFiles If files should be included in the list * @param includeDirs If directories should be included in the list * @return List of files/directories * @throws IOException If an error listing the file occurs */ public static Set<String> listAllFilesNames(File directory, boolean recursive, boolean includeFiles, boolean includeDirs) throws IOException { HashSet<String> filepaths = new HashSet<String>(); recursiveFileNameList(filepaths, directory, null, recursive, includeFiles, includeDirs); return filepaths; }
public static SplitPath splitFileNameFromParentPath(String fullPath, char fileSeparator) { final Pattern p; if( fileSeparator == File.separatorChar ) { p = defaultPathPattern; } else { p = compilePathSplitPattern(fileSeparator); } Matcher m = p.matcher(fullPath); if( ! m.find() ) { throw new IllegalArgumentException("Path pattern cannot be parsed: "+fullPath); } return new SplitPath(m.group(1),m.group(2)); }
/** * Copy the files in the specified directory to another directory * @param dir Source directory * @param destination Destination Directory */ public static void copyFilesInDir(File dir, File destination) throws IOException { for (File file : listFileInDir(dir)) { File destFile = new File(destination, file.getName()); if( file.isDirectory() ) { mkdir(destFile); copyFilesInDir(file,destFile); } else { copy(file,destFile); } } }
/** * Write data to a file * * @param file File to write data to * @param data Data to write */ public static void write(File file, byte[] data) throws IOException { FileUtils.write(file,data); } }
/** * List all files/directories in a directory * * @param directory Directory to list files in * @param recursive If the search should be done recursively * @param includeFiles If files should be included in the list * @param includeDirs If directories should be included in the list * @return List of files/directories * @throws IOException If an error listing the file occurs */ public static Set<String> listAllFilesNames(File directory, boolean recursive, boolean includeFiles, boolean includeDirs) throws IOException { HashSet<String> filepaths = new HashSet<String>(); recursiveFileNameList(filepaths, directory, null, recursive, includeFiles, includeDirs); return filepaths; }
/** * Same as invoking {@link #delete(boolean, File...)} with deleteOnExit set to true)} * * @param file File to delete */ public static void delete(File... file) throws IOException { delete(true, file); }
/** * Write string to a file using UTF-8 encoding * * @param file File to write data to * @param text Text to write */ public static void write(File file, String text) throws IOException { write(file, text, "UTF-8"); }
public static String toString(File file) throws IOException { return FileUtils.toString(file); }
private static void recursiveFileNameList(HashSet<String> filepaths, File file, String path, boolean recursive, boolean includeFiles, boolean includeDirs) throws IOException { if (file.isDirectory()) { if (path == null || recursive) { File[] files = file.listFiles(); if (files == null) { throw new IOException("Unable to list files in directory: " + file.getPath()); } for (File f : files) { recursiveFileNameList(filepaths, f, path != null ? path + File.separator + f.getName() : f.getName(), recursive, includeFiles, includeDirs); } } if (path != null && includeDirs) { filepaths.add(path); } } else { if (includeFiles) { filepaths.add(path); } } }
public static byte[] toByteArray(File file) throws IOException { return FileUtils.toByteArray(file); }
/** * Deletes the directory and all files inside it. If any file delete fails, they will be scheduled for deletion using {@link java.io.File#deleteOnExit()} */ public void close() { try { FileUtils.delete(true,(File) this); } catch (IOException e) { // } }
/** * Write string to a file using UTF-8 encoding * * @param file File to write data to * @param text Text to write */ public static void write(File file, String text) throws IOException { write(file, text, "UTF-8"); }
public static String toString(File file) throws IOException { return FileUtils.toString(file); }
private static void recursiveFileNameList(HashSet<String> filepaths, File file, String path, boolean recursive, boolean includeFiles, boolean includeDirs) throws IOException { if (file.isDirectory()) { if (path == null || recursive) { File[] files = file.listFiles(); if (files == null) { throw new IOException("Unable to list files in directory: " + file.getPath()); } for (File f : files) { recursiveFileNameList(filepaths, f, path != null ? path + File.separator + f.getName() : f.getName(), recursive, includeFiles, includeDirs); } } if (path != null && includeDirs) { filepaths.add(path); } } else { if (includeFiles) { filepaths.add(path); } } }
/** * Same as invoking {@link #delete(boolean, File...)} with deleteOnExit set to true)} * * @param file File to delete */ public static void delete(File... file) throws IOException { delete(true, file); }