Package org.zkoss.io

Class Files


  • public class Files
    extends java.lang.Object
    File related utilities.
    Author:
    tomyeh
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int CP_OVERWRITE
      Overwrites the destination file.
      static int CP_PRESERVE
      Preserves the last modified time and other attributes if possible.
      static int CP_SKIP_SVN
      Skips the SVN related files.
      static int CP_UPDATE
      Copy only when the source is newer or when the destination is missing.
      static char DRIVE_SEPARATOR_CHAR
      The separator representing the drive in a path.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected Files()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void close​(java.io.InputStream strm)
      Close an input stream without throwing an exception.
      static void close​(java.io.OutputStream strm)
      Close an output stream without throwing an exception.
      static void close​(java.io.Reader reader)
      Close a reader without throwing an exception.
      static void close​(java.io.Writer writer)
      Close a writer without throwing an exception.
      static void copy​(java.io.File dst, java.io.File src, int flags)
      Copies a file or a directory into another.
      static void copy​(java.io.File dst, java.io.InputStream in)
      Copies an input stream into a file (the original content, if any, are erased).
      static void copy​(java.io.File dst, java.io.Reader reader, java.lang.String charset)
      Copies a reader into a file (the original content, if any, are erased).
      static void copy​(java.io.OutputStream out, java.io.InputStream in)
      Copies an input stream to a output stream.
      static void copy​(java.io.Writer writer, java.io.Reader reader)
      Copies a reader into a writer.
      static java.lang.String correctSeparator​(java.lang.String flnm)
      Corrects the separator from '/' to the system dependent one.
      static boolean deleteAll​(java.io.File file)
      Deletes all files under the specified path.
      static java.lang.String locate​(java.lang.String flnm)
      Locates a file based o the current Locale.
      static java.lang.String normalize​(java.lang.String path)
      Normalizes the specified path.
      static java.lang.String normalize​(java.lang.String parentPath, java.lang.String childPath)
      Normalizes the concatenation of two paths.
      static byte[] readAll​(java.io.InputStream in)
      Returns all bytes in the input stream, never null (but its length might zero).
      static java.lang.StringBuffer readAll​(java.io.Reader reader)
      Returns all characters in the reader, never null (but its length might zero).
      static void write​(java.io.Writer out, java.lang.StringBuffer sb)
      Writes the specified string buffer to the specified writer.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DRIVE_SEPARATOR_CHAR

        public static final char DRIVE_SEPARATOR_CHAR
        The separator representing the drive in a path. In Windows, it is ':', while 0 in other platforms.
      • CP_PRESERVE

        public static int CP_PRESERVE
        Preserves the last modified time and other attributes if possible.
        See Also:
        copy(File, File, int)
      • CP_UPDATE

        public static int CP_UPDATE
        Copy only when the source is newer or when the destination is missing.
        See Also:
        copy(File, File, int)
      • CP_OVERWRITE

        public static int CP_OVERWRITE
        Overwrites the destination file.
        See Also:
        copy(File, File, int)
      • CP_SKIP_SVN

        public static int CP_SKIP_SVN
        Skips the SVN related files.
        Since:
        5.0.0
    • Constructor Detail

      • Files

        protected Files()
    • Method Detail

      • correctSeparator

        public static final java.lang.String correctSeparator​(java.lang.String flnm)
        Corrects the separator from '/' to the system dependent one. Note: always uses '/' even though Windows uses '\\'.
      • readAll

        public static final byte[] readAll​(java.io.InputStream in)
                                    throws java.io.IOException
        Returns all bytes in the input stream, never null (but its length might zero).

        Notice: this method is memory hungry.

        Notice: it doesn't close in

        Throws:
        java.io.IOException
      • readAll

        public static final java.lang.StringBuffer readAll​(java.io.Reader reader)
                                                    throws java.io.IOException
        Returns all characters in the reader, never null (but its length might zero).

        Notice: this method is memory hungry.

        Throws:
        java.io.IOException
      • copy

        public static final void copy​(java.io.Writer writer,
                                      java.io.Reader reader)
                               throws java.io.IOException
        Copies a reader into a writer.

        Notice: it doesn't close reader or writer

        Parameters:
        writer - the destination
        reader - the source
        Throws:
        java.io.IOException
      • copy

        public static final void copy​(java.io.OutputStream out,
                                      java.io.InputStream in)
                               throws java.io.IOException
        Copies an input stream to a output stream.

        Notice: it doesn't close in or out

        Parameters:
        out - the destination
        in - the source
        Throws:
        java.io.IOException
      • copy

        public static final void copy​(java.io.File dst,
                                      java.io.Reader reader,
                                      java.lang.String charset)
                               throws java.io.IOException
        Copies a reader into a file (the original content, if any, are erased). The source and destination files will be closed after copied.
        Parameters:
        dst - the destination
        reader - the source
        charset - the charset; null as default (ISO-8859-1).
        Throws:
        java.io.IOException
      • copy

        public static final void copy​(java.io.File dst,
                                      java.io.InputStream in)
                               throws java.io.IOException
        Copies an input stream into a file (the original content, if any, are erased). The file will be closed after copied.
        Parameters:
        dst - the destination
        in - the source
        Throws:
        java.io.IOException
      • copy

        public static final void copy​(java.io.File dst,
                                      java.io.File src,
                                      int flags)
                               throws java.io.IOException
        Copies a file or a directory into another.

        If neither CP_UPDATE nor CP_OVERWRITE, IOException is thrown if the destination exists.

        Parameters:
        flags - any combination of CP_UPDATE, CP_PRESERVE, CP_OVERWRITE.
        Throws:
        java.io.IOException
      • deleteAll

        public static final boolean deleteAll​(java.io.File file)
        Deletes all files under the specified path.
      • close

        public static final void close​(java.io.InputStream strm)
        Close an input stream without throwing an exception.
      • close

        public static final void close​(java.io.Reader reader)
        Close a reader without throwing an exception.
      • close

        public static final void close​(java.io.OutputStream strm)
        Close an output stream without throwing an exception.
        Since:
        5.0.4
      • close

        public static final void close​(java.io.Writer writer)
        Close a writer without throwing an exception.
        Since:
        5.0.4
      • normalize

        public static final java.lang.String normalize​(java.lang.String parentPath,
                                                       java.lang.String childPath)
        Normalizes the concatenation of two paths.
        Parameters:
        parentPath - the parent's path
        childPath - the child's path If it starts with "/", parentPath is ignored.
        Since:
        5.0.0
      • normalize

        public static final java.lang.String normalize​(java.lang.String path)
        Normalizes the specified path. It removes consecutive slashes, ending slashes, redundant . and ...

        Unlike File, normalize(java.lang.String, java.lang.String) always assumes the separator to be '/', and it cannot handle the device prefix (e.g., c:). However, it handles //.

        Parameters:
        path - the path to normalize. If null, an empty string is returned.
        Since:
        5.0.0
      • write

        public static final void write​(java.io.Writer out,
                                       java.lang.StringBuffer sb)
                                throws java.io.IOException
        Writes the specified string buffer to the specified writer. Use this method instead of out.write(sb.toString()), if sb.length() is large.
        Throws:
        java.io.IOException
        Since:
        5.0.0
      • locate

        public static final java.lang.String locate​(java.lang.String flnm)
        Locates a file based o the current Locale. It never returns null.

        If the filename contains "*", it will be replaced with a proper Locale. For example, if the current Locale is zh_TW and the resource is named "ab*.cd", then it searches "ab_zh_TW.cd", "ab_zh.cd" and then "ab.cd", until any of them is found.

        Note: "*" must be right before ".", or the last character. For example, "ab*.cd" and "ab*" are both correct, while "ab*cd" and "ab*\/cd" are ignored.

        Unlike Locators.locate(java.lang.String, java.util.Locale, org.zkoss.util.resource.Locator), the filename must contain '*', while Locators.locate(java.lang.String, java.util.Locale, org.zkoss.util.resource.Locator) always tries to locate the file by inserting the locale before '.'. In other words, Files.locate("/a/b*.c") is similar to Locators.locate(("/a/b.c", null, a_file_locator);

        Parameters:
        flnm - the filename to locate. If it doesn't contain any '*', it is returned directly. If the file is not found, flnm is returned, too.
        Since:
        5.0.0
        See Also:
        Locales.getCurrent()