Package org.zkoss.io

Class Files

java.lang.Object
org.zkoss.io.Files

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

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

    Constructors
    Modifier
    Constructor
    Description
    protected
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static final void
    Close an input stream without throwing an exception.
    static final void
    Close an output stream without throwing an exception.
    static final void
    close(Reader reader)
    Close a reader without throwing an exception.
    static final void
    close(Writer writer)
    Close a writer without throwing an exception.
    static final void
    copy(File dst, File src, int flags)
    Copies a file or a directory into another.
    static final void
    copy(File dst, InputStream in)
    Copies an input stream into a file (the original content, if any, are erased).
    static final void
    copy(File dst, Reader reader, String charset)
    Copies a reader into a file (the original content, if any, are erased).
    static final void
    Copies an input stream to a output stream.
    static final void
    copy(Writer writer, Reader reader)
    Copies a reader into a writer.
    static final String
    Corrects the separator from '/' to the system dependent one.
    static final boolean
    Deletes all files under the specified path.
    static final String
    locate(String flnm)
    Locates a file based o the current Locale.
    static final String
    Normalizes the specified path.
    static final String
    normalize(String parentPath, String childPath)
    Normalizes the concatenation of two paths.
    static final byte[]
    Returns all bytes in the input stream, never null (but its length might zero).
    static final StringBuffer
    readAll(Reader reader)
    Returns all characters in the reader, never null (but its length might zero).
    static final void
    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 Details

    • 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:
    • CP_UPDATE

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

      public static int CP_OVERWRITE
      Overwrites the destination file.
      See Also:
    • CP_SKIP_SVN

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

    • Files

      protected Files()
  • Method Details

    • correctSeparator

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

      public static final byte[] readAll(InputStream in) throws 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:
      IOException
    • readAll

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

      Notice: this method is memory hungry.

      Throws:
      IOException
    • copy

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

      Notice: it doesn't close reader or writer

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

      public static final void copy(OutputStream out, InputStream in) throws 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:
      IOException
    • copy

      public static final void copy(File dst, Reader reader, String charset) throws 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:
      IOException
    • copy

      public static final void copy(File dst, InputStream in) throws 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:
      IOException
    • copy

      public static final void copy(File dst, File src, int flags) throws 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:
      IOException
    • deleteAll

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

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

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

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

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

      public static final String normalize(String parentPath, 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 String normalize(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(Writer out, StringBuffer sb) throws 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:
      IOException
      Since:
      5.0.0
    • locate

      public static final String locate(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: