com.myjavatools.lib
Class Files

java.lang.Object
  extended by com.myjavatools.lib.Files

public abstract class Files
extends java.lang.Object

Files is a utility class that contains static methods for handling files and directories

Since:
5.0
Version:
5.0, 02/08/05

Nested Class Summary
static interface Files.BufferingFilter
          Description: Buffering filter stores some data, and these data can be retrieved later.
static interface Files.ByteFilter
          Description: The interface is used to define filters for filtering data in pipes.
 
Field Summary
static java.io.FileFilter DIRECTORY_FILTER
          directoryFilter is a FileFilter that accepts directories only
static java.io.FileFilter fileFilter
          fileFilter is a FileFilter that accepts files only
static int FIND_ALL
           
static int FIND_DIRECTORY
           
static int FIND_FILE
           
static boolean USE_NIO
          copy copies a file or a directory to another
 
Constructor Summary
Files()
           
 
Method Summary
static int adjustSizeByMooreLaw(int size, int thisYear)
          Adjusts buffer size according to Moore's law
static java.io.File appendBytesToFile(byte[] data, java.lang.String fileTo)
          Appends bytes to a file
static java.io.File appendBytesToFile(char[] data, java.lang.String fileTo)
          Appends bytes to the end of a file
static java.io.File appendToFile(byte[] data, java.lang.String fileTo)
          Appends bytes to the end of a file
static java.io.File appendToFile(char[] data, java.lang.String fileTo)
          Appends chars to the end of a file
static java.io.File appendToFile(java.lang.CharSequence data, java.lang.String fileTo)
          Appends a char sequence to the end of a file
static java.lang.Iterable<java.lang.Byte> bytes(java.io.File file)
          returns an Iterable<Byte> that scans over the bytes in a File
static java.lang.Iterable<java.lang.Byte> bytes(java.io.InputStream is)
          returns an Iterable<Byte> enclosure that scans over the bytes returned by InputStream
static java.lang.Iterable<java.lang.Character> chars(java.io.File file)
          returns an Iterable<Character> that scans over the characters in a File
static java.lang.Iterable<java.lang.Character> chars(java.io.Reader reader)
          returns an Iterable<Character> enclosure that scans over the characters returned by Reader
static int compare(java.io.File left, java.io.File right)
          Compare two files
static boolean copy(java.io.File from, java.io.File to)
           
static boolean copy(java.io.File from, java.io.File to, java.lang.String what)
          copy copies a file or a directory from one directory to another
static boolean copy(java.lang.String from, java.lang.String to)
          copy copies a file or a directory to another
static boolean copy(java.lang.String from, java.lang.String to, java.lang.String what)
          copies a file or a directory from one directory to another
static boolean deleteFile(java.io.File file)
          Deletes a file or a directory (with all its contents, they say it is dangerous)
static boolean deleteFile(java.lang.String filename)
          Deletes a file or a directory (with all its contents, they say it is dangerous)
static java.lang.String dirname(java.io.File file)
          Calculates directory path for a file (like in Perl)
static java.lang.String dirname(java.lang.String path)
          Calculates directory path by file path (like in Perl)
static boolean equal(java.io.File left, java.io.File right)
          compares two files or directories, recursively
static java.lang.String filename(java.lang.String path)
          Calculates filename by file path (like in Perl)
static java.lang.Iterable<java.io.File> files(java.io.File folder)
          Returns an Iterable<File> that scans all files in the folder
static java.util.List<java.lang.String> find(java.io.File subdir, java.util.regex.Pattern pattern)
          Lists recursively files and directories with name matching a regexp
static java.util.List<java.lang.String> find(java.lang.String subdir, java.util.regex.Pattern pattern)
          Lists recursively files and directories with name matching a regexp
static java.util.List<java.lang.String> find(java.lang.String subdir, java.lang.String pattern)
          Lists recursively files and directories with name matching a regexp
static java.lang.String findLatest(java.lang.String subdir, java.lang.String pattern)
          Finds latest file or directory which name matches a pattern
static java.lang.String findLatest(java.lang.String subdir, java.lang.String pattern, int whatExactly)
          Finds latest file or directory or one of these which name matches a pattern
static java.lang.String findLatestDirectory(java.lang.String subdir, java.lang.String pattern)
          Finds latest directory which name matches a pattern
static java.lang.String findLatestFile(java.lang.String subdir, java.lang.String pattern)
          Finds latest file which name matches a pattern
static java.lang.String getcwd()
          Gets current directory path
static java.lang.String getFullPath(java.io.File file)
          Calculates full path of a file
static java.lang.String getFullPath(java.lang.String path)
          Calculates full path of a file by its path
static java.lang.String getPackageName(java.lang.String basePath, java.lang.String currentPath)
          Calculates a java package name by directory name and base directory name
static boolean install(java.lang.Class clazz, java.lang.String resourceArchiveName, java.io.File location)
          installs files from a resource archive Reads a specified resource for aspecified class, unzips it to a specified directory
static boolean install(java.lang.Class clazz, java.lang.String resourceArchiveName, java.lang.String folderName)
          installs files from a resource archive Reads a specified resource for aspecified class, unzips it to a specified directory
static boolean install(java.lang.String urlString, java.lang.String directoryName)
          installs files from a URL Reads the contents of the specified URL, unzips it to a specified directory
static boolean install(java.net.URL url, java.io.File location)
          installs files from a URL Reads the contents of the specified URL, unzips it to a specified directory
static java.lang.String lastModified(java.io.File file)
          Gets file modification date/time as a string
static java.lang.Iterable<java.lang.String> lines(java.io.File file)
          returns an Iterable<String> that scans over the lines in a File
static java.lang.Iterable<java.lang.String> lines(java.io.Reader reader)
          returns an Iterable<String> enclosure that scans over the lines returned by Reader
static java.io.File[] listFiles(java.io.File dir)
          Lists files in a directory
static java.io.File[] listSubdirectories(java.io.File dir)
          Lists subdirectories of a directory
static java.io.FileOutputStream makeFile(java.io.File file)
          Creates or opens a file for output.
static java.io.FileOutputStream makeFile(java.io.File file, boolean append)
          Creates or opens a file for output.
static java.io.FileOutputStream makeFile(java.lang.String... path)
          Creates or opens a file for output.
static java.io.FileOutputStream makeFile(java.lang.String path)
          Creates or opens a file for output.
static java.io.FileOutputStream makeFile(java.lang.String[] path, boolean append)
          Creates or opens a file for output.
static java.io.FileOutputStream makeFile(java.lang.String path, boolean append)
          Creates or opens a file for output.
static java.io.FileOutputStream makeFile(java.lang.String dir, java.lang.String filename)
          Creates or opens a file for output.
static java.io.FileOutputStream makeFile(java.lang.String dirname, java.lang.String filename, boolean append)
          Creates or opens a file for output.
static java.io.OutputStreamWriter makeFileWriter(java.lang.String path, java.lang.String encoding)
          Creates or opens a file for output.
static java.lang.String path(java.lang.String currentDir, java.lang.String filepath)
          Having a directory and file path (relative or absolute) calculates full path
static void pipe(java.io.InputStream in, java.io.OutputStream out, boolean isBlocking)
          pipes data from input stream to output stream
static void pipe(java.io.InputStream in, java.io.OutputStream out, boolean isBlocking, Files.ByteFilter filter)
          pipes data from input stream to output stream, possibly pumping them through the filter (if any)
static boolean pipe(java.io.Reader in, java.io.Writer out)
          pipes data from input stream to output stream
static byte[] readBytesFromFile(java.lang.String filename)
          Reads the whole input stream into a byte array
static byte[] readBytesFromStream(java.io.InputStream is)
          Reads the whole input stream into a byte array
static java.lang.String readString(java.io.Reader reader)
           
static java.lang.String readStringFromFile(java.io.File file)
          Reads the whole file into a string
static java.lang.String readStringFromFile(java.io.File file, java.lang.String encoding)
          Reads the whole file into a string
static java.lang.String readStringFromFile(java.lang.String filename)
          Reads the whole file into a string
static java.lang.String relPath(java.lang.String dir, java.lang.String path)
          Calculates relative path
static boolean setLastModified(java.io.File file, long time)
          Sets the last-modified time of the file or directory named by this abstract pathname.
static java.lang.String[] splitPath(java.lang.String path)
          Splits a path into directory name and file name
static boolean synchronize(java.io.File left, java.io.File right)
          synchronizes two directories
static boolean synchronize(java.io.File left, java.io.File right, java.lang.String what)
          synchronizes two directories, left/what and right/what
static java.lang.Iterable<java.io.File> tree(java.io.File folder)
          Returns an Iterable<File> that scans, recursively, through the directory structure.
static java.lang.Iterable<java.io.File> tree(java.io.File folder, java.io.FileFilter filter)
          Returns an Iterable<File> that scans, recursively, through the directory structure.
static java.lang.Iterable<java.io.File> treePostorder(java.io.File folder)
          Returns an Iterable<File> that scans, recursively, through the directory structure.
static boolean unzip(java.util.zip.ZipInputStream zis, java.io.File location)
          unzips an input stream to a specified folder
static java.io.File writeBytesToFile(byte[] data, java.lang.String fileTo)
          Writes bytes to a file
static java.io.File writeToFile(byte[] data, java.lang.String fileTo)
          Creates a file and writes bytes into it
static java.io.File writeToFile(char[] data, java.lang.String fileTo)
          Creates a file and writes chars into it
static java.io.File writeToFile(java.lang.CharSequence data, java.lang.String fileTo)
          Creates a file and writes a char sequence into it
static java.io.File writeToFile(java.io.InputStream is, java.lang.String fileTo)
          Creates a file and copies into it bytes from an input stream
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FIND_FILE

public static final int FIND_FILE
See Also:
Constant Field Values

FIND_DIRECTORY

public static final int FIND_DIRECTORY
See Also:
Constant Field Values

FIND_ALL

public static final int FIND_ALL
See Also:
Constant Field Values

DIRECTORY_FILTER

public static java.io.FileFilter DIRECTORY_FILTER
directoryFilter is a FileFilter that accepts directories only


fileFilter

public static java.io.FileFilter fileFilter
fileFilter is a FileFilter that accepts files only


USE_NIO

public static boolean USE_NIO
copy copies a file or a directory to another

Constructor Detail

Files

public Files()
Method Detail

getFullPath

public static final java.lang.String getFullPath(java.io.File file)
Calculates full path of a file

Parameters:
file -
Returns:
full path

getFullPath

public static final java.lang.String getFullPath(java.lang.String path)
Calculates full path of a file by its path

Parameters:
path -
Returns:
full path

relPath

public static java.lang.String relPath(java.lang.String dir,
                                       java.lang.String path)
Calculates relative path

Parameters:
dir - directory path that is expected to start the file path
path - file path, relativer or not
Returns:
if path starts with dir, then the rest of the path, else path

Examples:
  • relPath("c:\\MyHome\\dev", "c:\\MyHome\\dev\\src\\java") returns "src\\java";
  • relPath("/home/zaphod", "/home/zaphod/jbuilder8/samples/welcome") returns "jbuilder8/samples/welcome";
  • relPath("/home/zaphod", "/home/ford/jbuilder8") returns "/home/ford/jbuilder8".

  • path

    public static java.lang.String path(java.lang.String currentDir,
                                        java.lang.String filepath)
    Having a directory and file path (relative or absolute) calculates full path

    Parameters:
    currentDir - directory path
    filepath - file path, relative or not
    Returns:
    full file path

    Examples:
  • path("c:\\MyHome\\dev", "src\\java") returns "c:\\MyHome\\dev\\src\\java";
  • path("/root/inetd", "/home/zaphod/jbuilder8/samples/welcome") returns "/home/zaphod/jbuilder8/samples/welcome";
  • path("\\Program Files", "c:\\MyHome\\dev") returns "c:\\MyHome\\dev".

  • splitPath

    public static java.lang.String[] splitPath(java.lang.String path)
    Splits a path into directory name and file name

    Parameters:
    path -
    Returns:
    String array consisting of two elements

    Examples:
  • splitPath("/home/zaphod/jbuilder8/samples/welcome") returns {"/home/zaphod/jbuilder8/samples", "welcome"};
  • splitPath("src.java") returns {".", "src.java"};
  • splitPath("MyHome\\dev") returns {"MyHome", "dev"}.

  • dirname

    public static java.lang.String dirname(java.io.File file)
    Calculates directory path for a file (like in Perl)

    Parameters:
    file -
    Returns:
    directory path Unlike java.io.File.getParent(), never returns null (see example 2 below).

    Examples:
  • dirname(new File("/home/zaphod/jbuilder11/samples/welcome")) returns "/home/zaphod/jbuilder8/samples";
  • dirname(new File("src.java")) returns ".";
  • dirname(new File("MyHome\\dev")) returns "MyHome".

  • dirname

    public static java.lang.String dirname(java.lang.String path)
    Calculates directory path by file path (like in Perl)

    Parameters:
    path -
    Returns:
    directory path Unlike java.io.File.getParent(), never returns null (see example 2 below).

    Examples:
  • dirname("/home/zaphod/jbuilder11/samples/welcome") returns "/home/zaphod/jbuilder8/samples";
  • dirname("src.java") returns ".";
  • dirname("MyHome\\dev") returns "MyHome".

  • filename

    public static java.lang.String filename(java.lang.String path)
    Calculates filename by file path (like in Perl)

    Parameters:
    path -
    Returns:
    file name

    Example:
  • filename("/home/zaphod/jbuilder11/samples/welcome") returns "welcome".

  • find

    public static java.util.List<java.lang.String> find(java.io.File subdir,
                                                        java.util.regex.Pattern pattern)
    Lists recursively files and directories with name matching a regexp

    Parameters:
    subdir - where to look
    pattern - to match
    Returns:
    List<String> of absolute filepaths

    Example:
  • find(new File("."), Pattern.compile(".*les\\.java$"))) returns Arrays.asList(new String[] {new File("Files.java").getCanonicalPath()}).

  • find

    public static java.util.List<java.lang.String> find(java.lang.String subdir,
                                                        java.util.regex.Pattern pattern)
    Lists recursively files and directories with name matching a regexp

    Parameters:
    subdir - where to look
    pattern - to match
    Returns:
    List<String> of absolute filepaths

    Example:
  • find(".", Pattern.compile(".*les\\.java$"))) returns Arrays.asList(new String[] {"Files.java"}).

  • find

    public static java.util.List<java.lang.String> find(java.lang.String subdir,
                                                        java.lang.String pattern)
    Lists recursively files and directories with name matching a regexp

    Parameters:
    subdir - where to look
    pattern - to match
    Returns:
    List<String> of absolute filepaths

    Example:
  • find(".", ".*les\\.java$") returns Arrays.asList(new String[] {"Files.java"}).

  • findLatest

    public static java.lang.String findLatest(java.lang.String subdir,
                                              java.lang.String pattern,
                                              int whatExactly)
    Finds latest file or directory or one of these which name matches a pattern

    Parameters:
    subdir - where to look
    pattern - to match
    whatExactly - can be FIND_FILE or FIND_DIRECTORY or FIND_ALL
    Returns:
    the path found

    findLatest

    public static java.lang.String findLatest(java.lang.String subdir,
                                              java.lang.String pattern)
    Finds latest file or directory which name matches a pattern

    Parameters:
    subdir - where to look
    pattern - to match
    Returns:
    the path found

    findLatestFile

    public static java.lang.String findLatestFile(java.lang.String subdir,
                                                  java.lang.String pattern)
    Finds latest file which name matches a pattern

    Parameters:
    subdir - where to look
    pattern - to match
    Returns:
    the path found

    findLatestDirectory

    public static java.lang.String findLatestDirectory(java.lang.String subdir,
                                                       java.lang.String pattern)
    Finds latest directory which name matches a pattern

    Parameters:
    subdir - where to look
    pattern - to match
    Returns:
    the path found

    listSubdirectories

    public static final java.io.File[] listSubdirectories(java.io.File dir)
    Lists subdirectories of a directory

    Parameters:
    dir - directory name
    Returns:
    an array of subdirectores

    listFiles

    public static final java.io.File[] listFiles(java.io.File dir)
    Lists files in a directory

    Parameters:
    dir - directory name
    Returns:
    an array of files

    lastModified

    public static final java.lang.String lastModified(java.io.File file)
    Gets file modification date/time as a string

    Parameters:
    file -
    Returns:
    file modification time as a string

    Example:
  • lastModified(new File("src/com/javatools/util/Objects.java")) returns "something".

  • getcwd

    public static java.lang.String getcwd()
    Gets current directory path

    Returns:
    the current directory path

    deleteFile

    public static boolean deleteFile(java.lang.String filename)
    Deletes a file or a directory (with all its contents, they say it is dangerous)

    Parameters:
    filename -
    Returns:
    true if successful

    Bad Example:
  • deleteFile("/etc") returns true if the program runs as root.

  • deleteFile

    public static boolean deleteFile(java.io.File file)
    Deletes a file or a directory (with all its contents, they say it is dangerous)

    Parameters:
    file - to delete
    Returns:
    true if successful

    makeFile

    public static java.io.FileOutputStream makeFile(java.lang.String dirname,
                                                    java.lang.String filename,
                                                    boolean append)
                                             throws java.io.IOException
    Creates or opens a file for output. If subdirectories in the path do not exist, they are created too. If the file exists, it is overwritten, unless append is true. append determines whether to open in append mode

    Parameters:
    dirname - file location
    filename - the name of the file
    append - true if open in append mode
    Returns:
    file output stream
    Throws:
    java.io.IOException

    makeFile

    public static java.io.FileOutputStream makeFile(java.lang.String dir,
                                                    java.lang.String filename)
                                             throws java.io.IOException
    Creates or opens a file for output. If subdirectories in the path do not exist, they are created too. If the file exists, it is overwritten.

    Parameters:
    dir - file location
    filename - the name of the file
    Returns:
    file output stream
    Throws:
    java.io.IOException

    makeFile

    public static java.io.FileOutputStream makeFile(java.lang.String[] path,
                                                    boolean append)
                                             throws java.io.IOException
    Creates or opens a file for output. If subdirectories in the path do not exist, they are created too. If the file exists, it is overwritten, unless append is true. append determines whether to open in append mode

    Parameters:
    path - [0] is directory name, [1] is file name
    append - true if open in append mode
    Returns:
    file output stream
    Throws:
    java.io.IOException

    makeFile

    public static java.io.FileOutputStream makeFile(java.lang.String... path)
                                             throws java.io.IOException
    Creates or opens a file for output. If subdirectories in the path do not exist, they are created too. If the file exists, it is overwritten.

    Parameters:
    path - String[] - a compound file path, ending with file name
    Returns:
    file output stream
    Throws:
    java.io.IOException

    makeFile

    public static java.io.FileOutputStream makeFile(java.lang.String path,
                                                    boolean append)
                                             throws java.io.IOException
    Creates or opens a file for output. If subdirectories in the path do not exist, they are created too. If the file exists, it is overwritten, unless append is true. append determines whether to open in append mode

    Parameters:
    path - file path
    append - true if open in append mode
    Returns:
    file output stream
    Throws:
    java.io.IOException

    makeFile

    public static java.io.FileOutputStream makeFile(java.lang.String path)
                                             throws java.io.IOException
    Creates or opens a file for output. If subdirectories in the path do not exist, they are created too. If the file exists, it is overwritten.

    Parameters:
    path - file path
    Returns:
    file output stream
    Throws:
    java.io.IOException

    makeFile

    public static java.io.FileOutputStream makeFile(java.io.File file,
                                                    boolean append)
                                             throws java.io.IOException
    Creates or opens a file for output. If subdirectories in the path do not exist, they are created too. If the file exists, it is overwritten, unless append is true. append determines whether to open in append mode

    Parameters:
    file - the file to open
    append - true if open in append mode
    Returns:
    file output stream
    Throws:
    java.io.IOException

    makeFile

    public static java.io.FileOutputStream makeFile(java.io.File file)
                                             throws java.io.IOException
    Creates or opens a file for output. If subdirectories in the path do not exist, they are created too. If the file exists, it is overwritten.

    Parameters:
    file - the file to open
    Returns:
    file output stream
    Throws:
    java.io.IOException

    makeFileWriter

    public static final java.io.OutputStreamWriter makeFileWriter(java.lang.String path,
                                                                  java.lang.String encoding)
                                                           throws java.io.IOException
    Creates or opens a file for output. If subdirectories in the path do not exist, they are created too. If the file exists, it is overwritten.

    Parameters:
    path - the file to open
    encoding - the encoding to use
    Returns:
    output stream writer
    Throws:
    java.io.IOException

    adjustSizeByMooreLaw

    public static final int adjustSizeByMooreLaw(int size,
                                                 int thisYear)
    Adjusts buffer size according to Moore's law

    Parameters:
    size - int original buffer size
    thisYear - int the year this specific size was chosen
    Returns:
    int buffer size adjusted by Moore's law: "double it every three years"

    readString

    public static final java.lang.String readString(java.io.Reader reader)

    readStringFromFile

    public static final java.lang.String readStringFromFile(java.io.File file)
    Reads the whole file into a string

    Parameters:
    file - the file to read
    Returns:
    file contents as a string, or null if error occurred

    Example:
  • readStringFromFile("../src/com/myjavatools/utils/Files.java") returns a string starting with "/**\r\n * <p>Title: MyJavaTools: Files handling Tools</p>\r\n*".

  • readStringFromFile

    public static final java.lang.String readStringFromFile(java.io.File file,
                                                            java.lang.String encoding)
    Reads the whole file into a string

    Parameters:
    file - the file to read
    encoding - the expected encoding
    Returns:
    file contents as a string, or null if error occurred

    readStringFromFile

    public static final java.lang.String readStringFromFile(java.lang.String filename)
    Reads the whole file into a string

    Parameters:
    filename - the file to read
    Returns:
    file contents as a string, or null if error occurred

    Example:
  • readStringFromFile("../src/com/myjavatools/utils/Files.java") returns a string starting with "/**\r\n * <p>Title: MyJavaTools: Files handling Tools</p>\r\n*".

  • readBytesFromStream

    public static final byte[] readBytesFromStream(java.io.InputStream is)
    Reads the whole input stream into a byte array

    Parameters:
    is - input stream to read
    Returns:
    file contents as byte array, or null if error occurred

    Example:
  • readBytesFromStream(new ByteArrayInputStream(new byte[] {1, 2, 3, 4, 5})) returns new byte[] {1, 2, 3, 4, 5}.

  • readBytesFromFile

    public static final byte[] readBytesFromFile(java.lang.String filename)
    Reads the whole input stream into a byte array

    Parameters:
    filename - file to read
    Returns:
    file contents as byte array, or null if error occurred

    Example:
  • readBytesFromFile("../src/com/myjavatools/utils/Files.java") returns a byte array starting with {51, 50, 50, 13, 10, 32, 50}.

  • writeToFile

    public static final java.io.File writeToFile(java.lang.CharSequence data,
                                                 java.lang.String fileTo)
    Creates a file and writes a char sequence into it

    Parameters:
    data - the char sequence to write to file
    fileTo - file path
    Returns:
    file

    writeToFile

    public static final java.io.File writeToFile(char[] data,
                                                 java.lang.String fileTo)
    Creates a file and writes chars into it

    Parameters:
    data - array of characters to write to file
    fileTo - file path
    Returns:
    file

    writeToFile

    public static final java.io.File writeToFile(byte[] data,
                                                 java.lang.String fileTo)
    Creates a file and writes bytes into it

    Parameters:
    data - array of bytes to append to the end of file
    fileTo - file path
    Returns:
    file

    writeBytesToFile

    public static final java.io.File writeBytesToFile(byte[] data,
                                                      java.lang.String fileTo)
    Writes bytes to a file

    Returns:
    file
    See Also:
    writeToFile(byte[],String)

    writeToFile

    public static final java.io.File writeToFile(java.io.InputStream is,
                                                 java.lang.String fileTo)
                                          throws java.io.IOException
    Creates a file and copies into it bytes from an input stream

    Parameters:
    is - input stream
    fileTo - file path
    Returns:
    file
    Throws:
    java.io.IOException

    appendToFile

    public static final java.io.File appendToFile(java.lang.CharSequence data,
                                                  java.lang.String fileTo)
    Appends a char sequence to the end of a file

    Parameters:
    data - char sequence to append to the end of file
    fileTo - file path
    Returns:
    file

    appendToFile

    public static final java.io.File appendToFile(char[] data,
                                                  java.lang.String fileTo)
    Appends chars to the end of a file

    Parameters:
    data - array of characters to append to the end of file
    fileTo - file path
    Returns:
    file

    appendToFile

    public static final java.io.File appendToFile(byte[] data,
                                                  java.lang.String fileTo)
    Appends bytes to the end of a file

    Parameters:
    data - array of characters to append to the end of file
    fileTo - file path
    Returns:
    file

    appendBytesToFile

    public static final java.io.File appendBytesToFile(char[] data,
                                                       java.lang.String fileTo)
    Appends bytes to the end of a file

    Parameters:
    data - chars to append are converted to bytes
    fileTo -
    Returns:
    file

    appendBytesToFile

    public static final java.io.File appendBytesToFile(byte[] data,
                                                       java.lang.String fileTo)
    Appends bytes to a file

    See Also:
    appendToFile(byte[],String)

    getPackageName

    public static final java.lang.String getPackageName(java.lang.String basePath,
                                                        java.lang.String currentPath)
    Calculates a java package name by directory name and base directory name

    Parameters:
    basePath - the base path for code source
    currentPath - the path of current directory
    Returns:
    package name

    Examples:
  • getPackageName("c:\\home\\myjavatools\\src", "c:\\home\\myjavatools\\src\\com\\myjavatools\\util") returns "com.myjavatools.util".
  • getPackageName("c:\\home\\myjavatools\\src\\java", "c:\\home\\myjavatools\\src\\com\\myjavatools\\util") returns null.

  • pipe

    public static void pipe(java.io.InputStream in,
                            java.io.OutputStream out,
                            boolean isBlocking,
                            Files.ByteFilter filter)
                     throws java.io.IOException
    pipes data from input stream to output stream, possibly pumping them through the filter (if any)

    Parameters:
    in - InputStream the source of data
    out - OutputStream where the output goes, filtered if filter is present, or unfiltered otherwise
    isBlocking - boolean whether input is blocking (in this case the maximum amount is read in one operation; for nonblocking in.available() determines how many bytes can be read)
    filter - ByteFilter the filter that applies to data; can be null
    Throws:
    java.io.IOException - when input or output fails see the test for examples

    pipe

    public static void pipe(java.io.InputStream in,
                            java.io.OutputStream out,
                            boolean isBlocking)
                     throws java.io.IOException
    pipes data from input stream to output stream

    Parameters:
    in - InputStream the source of data
    out - OutputStream where the output goes, filtered if filter is present, or unfiltered otherwise
    isBlocking - boolean whether input is blocking (in this case the maximum amount is read in one operation; for nonblocking in.available() determines how many bytes can be read)
    Throws:
    java.io.IOException - when input or output fails see the test for examples

    pipe

    public static boolean pipe(java.io.Reader in,
                               java.io.Writer out)
    pipes data from input stream to output stream

    Parameters:
    in - Reader the source of data
    out - Writer where the output goes, filtered if filter is present, or unfiltered otherwise
    Returns:
    boolean true if successful, false otherwise see the test for examples

    copy

    public static boolean copy(java.lang.String from,
                               java.lang.String to,
                               java.lang.String what)
    copies a file or a directory from one directory to another

    Parameters:
    from - directory from where to copy
    to - directory where to copy
    what - what to copy (file or directory, recursively)
    Returns:
    true if successful

    Example:
  • copy("c:\\home\\vlad\\dev", "c:\\home\\vlad\\rtm", "contents.xml")

  • copy

    public static boolean copy(java.io.File from,
                               java.io.File to,
                               java.lang.String what)
    copy copies a file or a directory from one directory to another

    Parameters:
    from - directory from where to copy
    to - directory where to copy
    what - what to copy (file or directory, recursively)
    Returns:
    true if successful

    Example:
  • copy(new File(myHomeDir, "dev"), new File(myHomeDir, "rtm"), "contents.xml")

  • copy

    public static boolean copy(java.lang.String from,
                               java.lang.String to)
    copy copies a file or a directory to another

    Parameters:
    from -
    to -
    Returns:
    true if successful

    Example:
  • copy("c:\\home\\vlad\\dev\\contents.xml", "c:\\home\\vlad\\rtm\\contents.rss")

  • copy

    public static boolean copy(java.io.File from,
                               java.io.File to)

    setLastModified

    public static boolean setLastModified(java.io.File file,
                                          long time)
    Sets the last-modified time of the file or directory named by this abstract pathname. The reason for this specific method is Java bug 4243868: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4243868 It does not always work without the trick...

    Parameters:
    file - File
    time - long
    Returns:
    true if and only if the operation succeeded; false otherwise
    Throws:
    java.lang.IllegalArgumentException - If the argument is negative
    java.lang.SecurityException - If a security manager exists and its SecurityManager.checkWrite(java.lang.String) method denies write access to the named file
    Since:
    5.0
    See Also:
    File.setLastModified(long)

    equal

    public static boolean equal(java.io.File left,
                                java.io.File right)
    compares two files or directories, recursively

    Parameters:
    left - File
    right - File
    Returns:
    boolean

    compare

    public static int compare(java.io.File left,
                              java.io.File right)
                       throws java.io.IOException
    Compare two files

    Parameters:
    left -
    right -
    Returns:
    -1 if left file is older or shorter or "smaller" than right 0 if files are equal 1 if right file is older or shorter or "smaller" than left

    Example:
  • copy(new File(myHomeDir, "contents.xml"), new File(mySite, "contents.rss");
    compare(new File(myHomeDir, "contents.xml"), new File(mySite, "contents.rss")
    returns -1
  • Throws:
    java.io.IOException

    synchronize

    public static boolean synchronize(java.io.File left,
                                      java.io.File right,
                                      java.lang.String what)
    synchronizes two directories, left/what and right/what

    Parameters:
    left - File first directory that contains directory what
    right - File second directory that contains directory what
    what - String name of directory which contents is being synchronized
    Returns:
    boolean true if success

    Example:
  • synchronize(new File(myHomeDir), new File(mySite), "myjavatools.com") will leave subdirectories named myjavatools.com in these two directories absolutely identical.

  • synchronize

    public static boolean synchronize(java.io.File left,
                                      java.io.File right)
    synchronizes two directories

    Parameters:
    left - File first directory
    right - File second directory
    Returns:
    boolean true if success

    Example:
  • synchronize(new File(myHomeDir), new File(myBackupDir)) will leave the contents of directories myHomeDIr and myBackupDir absolutely identical.

  • unzip

    public static boolean unzip(java.util.zip.ZipInputStream zis,
                                java.io.File location)
                         throws java.io.IOException
    unzips an input stream to a specified folder

    Parameters:
    zis - ZipInputStream the source of zipped files
    location - File the folder (directory) where to unzip the files
    Returns:
    boolean true if success, false otherwise

    Example:
  • unzip(Web.getUrlInputStream(new URL(synchronize(new File(myHomeDir), new File(myBackupDir)) will leave the contents of directories myHomeDIr and myBackupDir absolutely identical.
  • Throws:
    java.io.IOException - when something went wrong

    install

    public static boolean install(java.lang.Class clazz,
                                  java.lang.String resourceArchiveName,
                                  java.io.File location)
                           throws java.io.IOException
    installs files from a resource archive Reads a specified resource for aspecified class, unzips it to a specified directory

    Parameters:
    clazz - Class the class whose package contains the archive as a resource
    resourceArchiveName - String the name of resource containing the archive
    location - File directory where the archive is unzipped
    Returns:
    boolean true if success, false if failed
    Throws:
    java.io.IOException - if something goes wrong

    install

    public static boolean install(java.net.URL url,
                                  java.io.File location)
                           throws java.io.IOException
    installs files from a URL Reads the contents of the specified URL, unzips it to a specified directory

    Parameters:
    URL - url the url containing an archive to install
    location - File directory where the archive is unzipped
    Returns:
    boolean true if success, false if failed
    Throws:
    java.io.IOException - if something goes wrong

    install

    public static boolean install(java.lang.String urlString,
                                  java.lang.String directoryName)
                           throws java.io.IOException
    installs files from a URL Reads the contents of the specified URL, unzips it to a specified directory

    Parameters:
    String - urlString the string with the url containing an archive to install
    location - File directory where the archive is unzipped
    Returns:
    boolean true if success, false if failed
    Throws:
    java.io.IOException - if something goes wrong

    install

    public static boolean install(java.lang.Class clazz,
                                  java.lang.String resourceArchiveName,
                                  java.lang.String folderName)
                           throws java.io.IOException
    installs files from a resource archive Reads a specified resource for aspecified class, unzips it to a specified directory

    Parameters:
    clazz - Class the class whose package contains the archive as a resource
    resourceArchiveName - String the name of resource containing the archive
    folderName - String name of directory where the archive is unzipped
    Returns:
    boolean true if success, false if failed
    Throws:
    java.io.IOException - if something goes wrong

    bytes

    public static java.lang.Iterable<java.lang.Byte> bytes(java.io.InputStream is)
    returns an Iterable<Byte> enclosure that scans over the bytes returned by InputStream

    Parameters:
    is - InputStream the stream to scan
    Returns:
    Iterable<Byte> the enclosure The Iterator returned by the Iterable is a singleton; you cannot expect to get a fresh Iterator by calling iterator() several times.

    Example:

    for(byte b : bytes(new java.net.URL("http://yahoo.com").openSream())) { System.out.println(b); }

    bytes

    public static java.lang.Iterable<java.lang.Byte> bytes(java.io.File file)
    returns an Iterable<Byte> that scans over the bytes in a File

    Parameters:
    file - File the file to scan
    Returns:
    Iterable<Byte> the Iterable

    Example:

        for(byte b : bytes(new File("notepad.exe"))) {
          System.out.println(b);
        }
     

    chars

    public static java.lang.Iterable<java.lang.Character> chars(java.io.Reader reader)
    returns an Iterable<Character> enclosure that scans over the characters returned by Reader

    Parameters:
    reader - Reader the reader to scan
    Returns:
    Iterable<Character> the Iterable enclosure The Iterator returned by the Iterable is a singleton; you cannot expect to get a fresh Iterator by calling iterator() several times.
    Usage example:

    for(char c : chars(new InputStreamReader(new java.net.URL("http://yahoo.com").openSream()))) { System.out.println("[" + c + "]"); }

    chars

    public static java.lang.Iterable<java.lang.Character> chars(java.io.File file)
    returns an Iterable<Character> that scans over the characters in a File

    Parameters:
    file - File the file to scan
    Returns:
    Iterable<Character> the Iterable
    Usage example:

    for(char c : bytes(new File("readme.html"))) { System.out.println("[" + c + "]"); }

    lines

    public static java.lang.Iterable<java.lang.String> lines(java.io.Reader reader)
    returns an Iterable<String> enclosure that scans over the lines returned by Reader

    Parameters:
    reader - Reader the reader to scan
    Returns:
    Iterable<String> the Iterable enclosure The Iterator returned by the Iterable is a singleton; you cannot expect to get a fresh Iterator by calling iterator() several times.
    Usage example:

    for(String line : chars(new LineNumberReader(new java.net.URL("http://yahoo.com").openSream()))) { System.out.println(">" + line); }

    lines

    public static java.lang.Iterable<java.lang.String> lines(java.io.File file)
    returns an Iterable<String> that scans over the lines in a File

    Parameters:
    file - File the file to scan
    Returns:
    Iterable<String> the Iterable Usage example: for(String line : lines(new File("readme.txt"))) { System.out.println(">" + line); }

    tree

    public static java.lang.Iterable<java.io.File> tree(java.io.File folder)
    Returns an Iterable<File> that scans, recursively, through the directory structure. Traversal order is depth-first, preorder

    Parameters:
    folder - File starting directory
    Returns:
    Iterable<File> the tree scanner Usage examples: for(File subfolder : tree(new File("."))) { System.out.println(subfolder.getCanonicalPath()); } for(File folder : tree(new File("."))) { System.out.println(file.getCanonicalPath()); for (File file : files(folder)) { System.out.println(" " + file.getName()); } }

    tree

    public static java.lang.Iterable<java.io.File> tree(java.io.File folder,
                                                        java.io.FileFilter filter)
    Returns an Iterable<File> that scans, recursively, through the directory structure. Traversal order is depth-first, preorder

    Parameters:
    folder - File starting directory
    Returns:
    Iterable<File> the tree scanner Usage examples: for(File subfolder : tree(new File("."))) { System.out.println(subfolder.getCanonicalPath()); } for(File folder : tree(new File("."))) { System.out.println(file.getCanonicalPath()); for (File file : files(folder)) { System.out.println(" " + file.getName()); } }

    treePostorder

    public static java.lang.Iterable<java.io.File> treePostorder(java.io.File folder)
    Returns an Iterable<File> that scans, recursively, through the directory structure. Traversal order is depth-first, postorder

    Parameters:
    folder - File starting directory
    Returns:
    Iterable<File> the tree scanner Usage examples: for(File subfolder : treePostorder(new File("."))) { System.out.println(subfolder.getCanonicalPath()); } for(File folder : treePostorder(new File("."))) { System.out.println(file.getCanonicalPath()); for (File file : files(folder)) { System.out.println(" " + file.getName()); } }

    files

    public static java.lang.Iterable<java.io.File> files(java.io.File folder)
    Returns an Iterable<File> that scans all files in the folder

    Parameters:
    folder - File directory to scan
    Returns:
    Iterable<File> the scanner
    Usage example:

    for (File file : files(new File(".")) { System.out.println(file.getName()); }