| //------------------------------------------------------------------------------ |
| //Copyright (c) 2004, 2006 IBM Corporation. All Rights Reserved. |
| //------------------------------------------------------------------------------ |
| package org.eclipse.epf.web.search.utils; |
| |
| import java.io.File; |
| import java.io.FileOutputStream; |
| import java.io.FileWriter; |
| import java.io.IOException; |
| import java.io.InputStream; |
| import java.io.InputStreamReader; |
| import java.io.OutputStream; |
| import java.io.OutputStreamWriter; |
| import java.io.PrintWriter; |
| import java.net.URL; |
| |
| import com.ibm.icu.util.ULocale; |
| |
| /** |
| * Implements a utility class for managing directories and files. |
| * |
| * @author Kelvin Low |
| * @since 6.0 |
| */ |
| public class FileUtil { |
| |
| /** |
| * Platform-specific line separator. |
| */ |
| public static final String LINE_SEP = System.getProperty("line.separator"); //$NON-NLS-1$ |
| |
| /** |
| * Private constructor to prevent this class from being instantiated. All |
| * methods in this class should be static. |
| */ |
| private FileUtil() { |
| } |
| |
| /** |
| * Returns the absolute path for the given file or directory. |
| * |
| * @param file |
| * The given file or directory. |
| * @return The absolute path of the given file or directory. |
| */ |
| public static String getAbsolutePath(File file) { |
| return file.getAbsolutePath().replace('\\', '/'); |
| } |
| |
| /** |
| * Returns the absolute path for the given file or directory. |
| * |
| * @param file |
| * The given file or directory name. |
| * @return The absolute path of the given file or directory. |
| */ |
| public static String getAbsolutePath(String file) { |
| return getAbsolutePath(new File(file)); |
| } |
| |
| /** |
| * Returns the absolute path for the given URL. |
| * |
| * @param url |
| * The given URL. |
| * @return The absolute path of the given URL. |
| */ |
| public static String getAbsolutePath(URL url) { |
| String pathName = url.getFile().substring(1); |
| String result = NetUtil.decodeUrl(pathName, null); |
| return result; |
| } |
| |
| /** |
| * Returns the parent directory of the given path. |
| * |
| * @param path |
| * The path name. |
| * @return The name of the parent directory. |
| */ |
| public static String getParentDirectory(String path) { |
| return (new File(path)).getParent(); |
| } |
| |
| /** |
| * Returns the file name and extension from the given path. |
| * |
| * @param path |
| * The path name. |
| * @return The file name and the file extension. |
| */ |
| public static String getFileName(String path) { |
| return getFileName(path, true); |
| } |
| |
| /** |
| * Returns the file name from the given path, with or without the file |
| * extension. |
| * |
| * @param path |
| * The path name. |
| * @param withExtension |
| * If true, include the file extension in the result. |
| * @return The file name with or without the file extension. |
| */ |
| public static String getFileName(String path, boolean withExtension) { |
| String normalizedPath = path.replace('\\', '/'); |
| |
| int prefixLength = 0; |
| if (normalizedPath.startsWith(NetUtil.URI_FILE_PREFIX)) { |
| prefixLength = NetUtil.URI_FILE_PREFIX_SIZE; |
| } else if (normalizedPath.startsWith(NetUtil.URI_HTTP_PREFIX)) { |
| prefixLength = NetUtil.URI_HTTP_PREFIX_SIZE; |
| } |
| |
| String fileName; |
| int index = normalizedPath.lastIndexOf("/"); //$NON-NLS-1$ |
| if (index < prefixLength) { |
| fileName = normalizedPath.substring(prefixLength); |
| } else { |
| fileName = path.substring(index + 1); |
| } |
| |
| if (withExtension) { |
| return fileName; |
| } |
| |
| index = fileName.indexOf("."); //$NON-NLS-1$ |
| return (index > 0) ? fileName.substring(0, index) : fileName; |
| } |
| |
| /** |
| * Appends the platform specific path separator to the end of the given |
| * path. |
| * |
| * @param path |
| * The path name. |
| * @return The path name appended with the platform specific path separator. |
| */ |
| public static String appendSeparator(String path) { |
| return appendSeparator(path, File.separator); |
| } |
| |
| /** |
| * Appends the given path separator to the end of the given path. |
| * |
| * @param path |
| * The path name. |
| * @param separator |
| * The path separator. |
| * @return The path name appended with the given separator. |
| */ |
| public static String appendSeparator(String path, String separator) { |
| return path.endsWith(separator) ? path : path + separator; |
| } |
| |
| /** |
| * Removes the ending path separator from the given path. |
| * |
| * @param path |
| * The path name. |
| * @return The path name minus the platform specific path separator. |
| */ |
| public static String removeSeparator(String path) { |
| return path.endsWith(File.separator) ? path.substring(0, |
| path.length() - 1) : path; |
| } |
| |
| /** |
| * Removes the ending path separator from the given path. |
| * |
| * @param path |
| * The path name. |
| * @return The path name minus the path separator "\\" or "/". |
| */ |
| public static String removeAllSeparator(String path) { |
| return path.endsWith("/") || path.endsWith("\\") ? path.substring(0, path.length() - 1) : path; //$NON-NLS-1$ //$NON-NLS-2$ |
| } |
| |
| /** |
| * Removes the ending path separator from the given path. |
| * |
| * @param path |
| * The path name. |
| * @param separator |
| * The path separator. |
| * @return The path name minus the separator. |
| */ |
| public static String removeSeparator(String path, String separator) { |
| return path.endsWith(separator) ? path.substring(0, path.length() - 1) |
| : path; |
| } |
| |
| /** |
| * Replaces the file name with another in the given path. |
| * |
| * @param path |
| * The path name. |
| * @param oldFileName |
| * The old file name. |
| * @param newFileName |
| * The new file name. |
| * @return The new path name with the new file name. |
| */ |
| public static String replaceFileName(String path, String oldFileName, |
| String newFileName) { |
| int index = path.lastIndexOf(oldFileName); |
| return path.substring(0, index) + newFileName; |
| } |
| |
| /** |
| * Replaces the file extension with another in the given path. |
| * |
| * @param path |
| * The path name. |
| * @param oldFileExt |
| * The old file extension. |
| * @param newFileExt |
| * The new file extension. |
| * @return The new path with the new file extension. |
| */ |
| public static String replaceExtension(String path, String oldExt, |
| String newExt) { |
| int index = path.lastIndexOf(oldExt); |
| return path.substring(0, index) + newExt; |
| } |
| |
| /** |
| * Returns the locale-specific path of a base path. |
| * |
| * @param path |
| * The base path name. |
| * @param localeStr |
| * The locale string. |
| * @return The locale-specific path. |
| */ |
| public static String getLocalePath(String path, String localeStr) { |
| if (StrUtil.isBlank(localeStr)) { |
| return path; |
| } |
| String fileName = getFileName(path); |
| return replaceFileName(path, fileName, localeStr + "/" + fileName); //$NON-NLS-1$ |
| } |
| |
| /** |
| * Returns the locale-specific path of a base path. |
| * |
| * @param path |
| * The base path name. |
| * @param locale |
| * The locale object. |
| * @return The locale-specific path. |
| */ |
| public static String getLocalePath(String path, ULocale locale) { |
| return locale == null ? path : getLocalePath(path, locale.toString()); |
| } |
| |
| /** |
| * Write the given text to a file. |
| * |
| * @param fileName |
| * The target file name. |
| * @param text |
| * The text to write. |
| * @return true if the given text is written successfully to file. |
| */ |
| public static boolean writeFile(String filename, String text) { |
| FileWriter writer = null; |
| try { |
| writer = new FileWriter(filename); |
| writer.write(text); |
| writer.flush(); |
| } catch (IOException e) { |
| } finally { |
| if (writer != null) { |
| try { |
| writer.close(); |
| return true; |
| } catch (Exception e) { |
| } |
| } |
| } |
| return false; |
| } |
| |
| /** |
| * Write the given text to a file. |
| * |
| * @param fileName |
| * The target file name. |
| * @param text |
| * The text to write. |
| * @return true if the given text is written successfully to file. |
| */ |
| public static boolean writeUTF8File(String filename, String text) { |
| OutputStreamWriter writer = null; |
| FileOutputStream fileOut = null; |
| try { |
| fileOut = new FileOutputStream(filename); |
| writer = new OutputStreamWriter(fileOut, "UTF-8"); //$NON-NLS-1$ |
| writer.write(text); |
| writer.flush(); |
| fileOut.flush(); |
| } catch (IOException e) { |
| } finally { |
| if (writer != null) { |
| try { |
| writer.close(); |
| return true; |
| } catch (Exception e) { |
| } |
| } |
| if (fileOut != null) { |
| try { |
| fileOut.close(); |
| return true; |
| } catch (Exception e) { |
| } |
| } |
| } |
| return false; |
| } |
| |
| /** |
| * Write the content of the given URI to the given output stream. |
| * |
| * @param uri |
| * The source URI. |
| * @param output |
| * The output stream. |
| */ |
| public static void writeFile(String uri, OutputStream output) |
| throws IOException { |
| if (uri == null) { |
| return; |
| } |
| |
| InputStream input = null; |
| try { |
| input = NetUtil.getInputStream(uri); |
| int bytesRead; |
| byte[] buf = new byte[4096]; |
| while ((bytesRead = input.read(buf, 0, 4096)) > 0) { |
| output.write(buf, 0, bytesRead); |
| } |
| output.flush(); |
| } finally { |
| if (input != null) { |
| try { |
| input.close(); |
| } catch (Exception e) { |
| } |
| } |
| } |
| } |
| |
| /** |
| * Write the content of the given URI to the given PrintWriter. |
| * |
| * @param uri |
| * The source URI. |
| * @param writer |
| * The PrintWriter obejct. |
| */ |
| public static void writeFile(String uri, PrintWriter pw) throws IOException { |
| if (uri == null) { |
| return; |
| } |
| |
| InputStreamReader input = null; |
| try { |
| input = new InputStreamReader(NetUtil.getInputStream(uri)); |
| int charsRead; |
| char[] buf = new char[4096]; |
| while ((charsRead = input.read(buf, 0, 4096)) > 0) { |
| pw.write(buf, 0, charsRead); |
| } |
| pw.flush(); |
| } finally { |
| if (input != null) { |
| try { |
| input.close(); |
| } catch (Exception e) { |
| } |
| } |
| } |
| } |
| |
| /** |
| * Delete all sub-directories and files in the given directory. |
| * |
| * @param dir |
| * The directory containing the sub-directories and files. |
| */ |
| public static void delete(File dir) { |
| File[] files = dir.listFiles(); |
| if (files != null) { |
| for (int i = 0; i < files.length; i++) { |
| File file = files[i]; |
| if (file.isFile()) { |
| file.delete(); |
| } else { |
| delete(file); |
| } |
| } |
| } |
| dir.delete(); |
| } |
| } |