/** * Copy the source file to the target file. * @param sourceFile The source file to copy from * @param targetFile The target file to copy to * @throws FileAlreadyExistsException Thrown if the target file already * exists. This exception is a subclass of IOException, so catching * an IOException is enough if you don't care about this specific reason. * @throws IOException Thrown if an error during the copy */ public static void copy(File sourceFile, File targetFile) throws FileAlreadyExistsException, IOException { copy(sourceFile, targetFile, false); }
/** * Creates a new instance of a <code>FileExtensionMatcher</code> that will * perform a match to this array of file extensions. * @param extension The array of file extensions to match against a File * @param caseSensitive If true the extension must match the case of the * provided extension, otherwise a case insensitive match will occur. * @throws IllegalArgumentException Thrown if the file extension is not * formatted correctly such as containing a period. */ public FileExtensionFilter(String[] extensions, boolean caseSensitive) throws IllegalArgumentException { // check each extension for (String ext : extensions) { if (!FileUtil.isValidFileExtension(ext)) { throw new IllegalArgumentException("Invalid file extension '" + ext + "' cannot be matched"); } } this.caseSensitive = caseSensitive; this.extensions = extensions; }
/** * Accepts a File by its file extension. * @param file The file to match * @return True if the File matches this the array of acceptable file * extensions. */ public boolean accept(File file) { // extract this file's extension String fileExt = FileUtil.parseFileExtension(file.getName()); // a file extension might not have existed if (fileExt == null) { // if no file extension extracted, this definitely is not a match return false; } // does it match our list of acceptable file extensions? for (String extension : extensions) { if (caseSensitive) { if (fileExt.equals(extension)) { return true; } } else { if (fileExt.equalsIgnoreCase(extension)) { return true; } } } // if we got here, then no match was found return false; }
/** * Tests whether the contents of two files equals each other by performing * a byte-by-byte comparison. Each byte must match each other in both files. * @param file1 The file to compare * @param file2 The other file to compare * @return True if file contents are equal, otherwise false. * @throws IOException Thrown if there is an underlying IO error while * attempt to compare the bytes. */ public static boolean equals(File file1, File file2) throws IOException { // file lengths must match if (file1.length() != file2.length()) { return false; } InputStream is1 = null; InputStream is2 = null; try { is1 = new FileInputStream(file1); is2 = new FileInputStream(file2); return equals(is1, is2); } finally { // make sure input streams are closed if (is1 != null) { try { is1.close(); } catch (Exception e) { } } if (is2 != null) { try { is2.close(); } catch (Exception e) { } } } }
/** * Accepts a File by its file extension. * @param file The file to match * @return True if the File matches this the array of acceptable file * extensions. */ public boolean accept(File file) { // extract this file's extension String fileExt = FileUtil.parseFileExtension(file.getName()); // a file extension might not have existed if (fileExt == null) { // if no file extension extracted, this definitely is not a match return false; } // does it match our list of acceptable file extensions? for (String extension : extensions) { if (caseSensitive) { if (fileExt.equals(extension)) { return true; } } else { if (fileExt.equalsIgnoreCase(extension)) { return true; } } } // if we got here, then no match was found return false; }
/** * Tests whether the contents of two files equals each other by performing * a byte-by-byte comparison. Each byte must match each other in both files. * @param file1 The file to compare * @param file2 The other file to compare * @return True if file contents are equal, otherwise false. * @throws IOException Thrown if there is an underlying IO error while * attempt to compare the bytes. */ public static boolean equals(File file1, File file2) throws IOException { // file lengths must match if (file1.length() != file2.length()) { return false; } InputStream is1 = null; InputStream is2 = null; try { is1 = new FileInputStream(file1); is2 = new FileInputStream(file2); return equals(is1, is2); } finally { // make sure input streams are closed if (is1 != null) { try { is1.close(); } catch (Exception e) { } } if (is2 != null) { try { is2.close(); } catch (Exception e) { } } } }
String fileExt = FileUtil.parseFileExtension(sourceFile.getName());
/** * Copy the source file to the target file. * @param sourceFile The source file to copy from * @param targetFile The target file to copy to * @throws FileAlreadyExistsException Thrown if the target file already * exists. This exception is a subclass of IOException, so catching * an IOException is enough if you don't care about this specific reason. * @throws IOException Thrown if an error during the copy */ public static void copy(File sourceFile, File targetFile) throws FileAlreadyExistsException, IOException { copy(sourceFile, targetFile, false); }
/** * Creates a new instance of a <code>FileExtensionMatcher</code> that will * perform a match to this array of file extensions. * @param extension The array of file extensions to match against a File * @param caseSensitive If true the extension must match the case of the * provided extension, otherwise a case insensitive match will occur. * @throws IllegalArgumentException Thrown if the file extension is not * formatted correctly such as containing a period. */ public FileExtensionFilter(String[] extensions, boolean caseSensitive) throws IllegalArgumentException { // check each extension for (String ext : extensions) { if (!FileUtil.isValidFileExtension(ext)) { throw new IllegalArgumentException("Invalid file extension '" + ext + "' cannot be matched"); } } this.caseSensitive = caseSensitive; this.extensions = extensions; }
String fileExt = FileUtil.parseFileExtension(sourceFile.getName());