📄 ioutil.java
字号:
* not folders.
* @param src source folder
* @param dest target folder
* @throws IOException if any I/O error has occurred
*
* @see #synchronizeFolders(File, File, FileFilter)
* @see #compareFiles(File, File)
*/
public static void synchronizeFolders(final File src, final File dest)
throws IOException {
synchronizeFolders(src, dest, null);
}
/**
* Performs one-way directories synchronization comparing files only,
* not folders.
* @param src source folder
* @param dest target folder
* @param filter file filter, optional, if <code>null</code> all files will
* be included into synchronization process
* @throws IOException if any I/O error has occurred
*
* @see #compareFiles(File, File)
*/
public static void synchronizeFolders(final File src, final File dest,
final FileFilter filter) throws IOException {
if (!src.isDirectory()) {
throw new IOException(
ResourceManager.getMessage(PACKAGE_NAME,
"notAFolder", src)); //$NON-NLS-1$
}
if (dest.isFile()) {
throw new IOException(
ResourceManager.getMessage(PACKAGE_NAME, "isFile", dest)); //$NON-NLS-1$
}
if (!dest.exists() && !dest.mkdirs()) {
throw new IOException(
ResourceManager.getMessage(PACKAGE_NAME,
"cantMakeFolder", dest)); //$NON-NLS-1$
}
File[] srcFiles = src.listFiles();
for (int i = 0; i < srcFiles.length; i++) {
File srcFile = srcFiles[i];
if ((filter != null) && !filter.accept(srcFile)) {
continue;
}
File destFile = new File(dest, srcFile.getName());
if (srcFile.isDirectory()) {
if (destFile.isFile() && !destFile.delete()) {
throw new IOException(
ResourceManager.getMessage(PACKAGE_NAME,
"cantDeleteFile", destFile)); //$NON-NLS-1$
}
synchronizeFolders(srcFile, destFile, filter);
continue;
}
if (compareFiles(srcFile, destFile)) {
continue;
}
copyFile(srcFile, destFile);
}
File[] destFiles = dest.listFiles();
for (int i = 0; i < destFiles.length; i++) {
File destFile = destFiles[i];
File srcFile = new File(src, destFile.getName());
if (((filter != null) && filter.accept(destFile) && srcFile.exists())
|| ((filter == null) && srcFile.exists())) {
continue;
}
if (destFile.isDirectory() && !emptyFolder(destFile)) {
throw new IOException(
ResourceManager.getMessage(PACKAGE_NAME,
"cantEmptyFolder", destFile)); //$NON-NLS-1$
}
if (!destFile.delete()) {
throw new IOException(
ResourceManager.getMessage(PACKAGE_NAME,
"cantDeleteFile", destFile)); //$NON-NLS-1$
}
}
dest.setLastModified(src.lastModified());
}
/**
* Checks if resource exist and can be opened.
* @param url absolute URL which points to a resource to be checked
* @return <code>true</code> if given URL points to an existing resource
*/
public static boolean isResourceExists(final URL url) {
File file = url2file(url);
if (file != null) {
return file.canRead();
}
if ("jar".equalsIgnoreCase(url.getProtocol())) { //$NON-NLS-1$
return isJarResourceExists(url);
}
return isUrlResourceExists(url);
}
private static boolean isUrlResourceExists(final URL url) {
try {
//url.openConnection().connect();
// Patch from Sebastian Kopsan
InputStream is = url.openStream();
try {
is.close();
} catch (IOException ioe) {
// ignore
}
return true;
} catch (IOException ioe) {
return false;
}
}
private static boolean isJarResourceExists(final URL url) {
try {
String urlStr = url.toExternalForm();
int p = urlStr.indexOf("!/"); //$NON-NLS-1$
if (p == -1) {// this is invalid JAR file URL
return false;
}
URL fileUrl = new URL(urlStr.substring(4, p));
File file = url2file(fileUrl);
if (file == null) {// this is non-local JAR file URL
return isUrlResourceExists(url);
}
if (!file.canRead()) {
return false;
}
if (p == urlStr.length() - 2) {// URL points to the root entry of JAR file
return true;
}
JarFile jarFile = new JarFile(file);
try {
return jarFile.getEntry(urlStr.substring(p + 2)) != null;
} finally {
jarFile.close();
}
} catch (IOException ioe) {
return false;
}
}
/**
* Opens input stream for given resource. This method behaves differently
* for different URL types:
* <ul>
* <li>for <b>local files</b> it returns buffered file input stream;</li>
* <li>for <b>local JAR files</b> it reads resource content into memory
* buffer and returns byte array input stream that wraps those
* buffer (this prevents locking JAR file);</li>
* <li>for <b>common URL's</b> this method simply opens stream to that URL
* using standard URL API.</li>
* </ul>
* It is not recommended to use this method for big resources within JAR
* files.
* @param url resource URL
* @return input stream for given resource
* @throws IOException if any I/O error has occurred
*/
public static InputStream getResourceInputStream(final URL url)
throws IOException {
File file = url2file(url);
if (file != null) {
return new BufferedInputStream(new FileInputStream(file));
}
if (!"jar".equalsIgnoreCase(url.getProtocol())) { //$NON-NLS-1$
return url.openStream();
}
String urlStr = url.toExternalForm();
if (urlStr.endsWith("!/")) { //$NON-NLS-1$
//JAR URL points to a root entry
throw new FileNotFoundException(url.toExternalForm());
}
int p = urlStr.indexOf("!/"); //$NON-NLS-1$
if (p == -1) {
throw new MalformedURLException(url.toExternalForm());
}
String path = urlStr.substring(p + 2);
file = url2file(new URL(urlStr.substring(4, p)));
if (file == null) {// non-local JAR file URL
return url.openStream();
}
JarFile jarFile = new JarFile(file);
try {
ZipEntry entry = jarFile.getEntry(path);
if (entry == null) {
throw new FileNotFoundException(url.toExternalForm());
}
InputStream in = jarFile.getInputStream(entry);
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
copyStream(in, out, 1024);
return new ByteArrayInputStream(out.toByteArray());
} finally {
in.close();
}
} finally {
jarFile.close();
}
}
/**
* Utility method to convert local URL to a {@link File} object.
* @param url an URL
* @return file object for given URL or <code>null</code> if URL is not
* local
*/
public static File url2file(final URL url) {
if (!"file".equalsIgnoreCase(url.getProtocol())) { //$NON-NLS-1$
return null;
}
//NB: this may not work correctly in all situations
//return new File(url.getPath());
// Patch from Wolfgang Ponikwar (po28808) and from Aaron Hamid (aaron)
//return new File(url.getPath().replaceAll("%20", " ")); //$NON-NLS-1$ //$NON-NLS-2$
return new File(url.getFile().replaceAll("%20", " ")); //$NON-NLS-1$ //$NON-NLS-2$
//return new java.io.File(URI.create(url.toExternalForm()));
}
/**
* Utility method to convert a {@link File} object to a local URL.
* @param file a file object
* @return absolute URL that points to the given file
* @throws MalformedURLException if file can't be represented as URL for
* some reason
*/
public static URL file2url(final File file) throws MalformedURLException {
try {
return file.getCanonicalFile().toURI().toURL();
} catch (MalformedURLException mue) {
throw mue;
} catch (IOException ioe) {
throw new MalformedURLException(
ResourceManager.getMessage(PACKAGE_NAME, "file2urlFailed", //$NON-NLS-1$
new Object[] {file, ioe}));
} catch (NoSuchMethodError nsme) {
// ignore, seems the JDK version is less than 1.4,
// will try another way
}
try {
return new URL("file://" //$NON-NLS-1$
+ file.getCanonicalPath().replace('\\', '/')
.replaceAll(" ", "%20")); //$NON-NLS-1$ //$NON-NLS-2$
} catch (MalformedURLException mue) {
throw mue;
} catch (IOException ioe) {
throw new MalformedURLException(
ResourceManager.getMessage(PACKAGE_NAME, "file2urlFailed", //$NON-NLS-1$
new Object[] {file, ioe}));
}
}
private IoUtil() {
// no-op
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -