Java Doc for FileUtils.java in  » Library » apache-common-IO » org » apache » commons » io » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » apache common IO » org.apache.commons.io 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   org.apache.commons.io.FileUtils

FileUtils
public class FileUtils (Code)
General file manipulation utilities.

Facilities are provided in the following areas:

  • writing to a file
  • reading from a file
  • make a directory including parent directories
  • copying files and directories
  • deleting files and directories
  • converting to and from a URL
  • listing files and directories by filter and extension
  • comparing file content
  • file last changed date
  • calculating a checksum

Origin of code: Excalibur, Alexandria, Commons-Utils
author:
   Kevin A. Burton
author:
   Scott Sanders
author:
   Daniel Rall
author:
   Christoph.Reck
author:
   Peter Donald
author:
   Jeff Turner
author:
   Matthew Hawthorne
author:
   Jeremias Maerki
author:
   Stephen Colebourne
author:
   Ian Springer
author:
   Chris Eldredge
author:
   Jim Harrington
author:
   Niall Pemberton
author:
   Sandy McArthur
version:
   $Id: FileUtils.java 507684 2007-02-14 20:38:25Z bayard $



Field Summary
final public static  File[]EMPTY_FILE_ARRAY
     An empty array of type File.
final public static  longONE_GB
     The number of bytes in a gigabyte.
final public static  longONE_KB
     The number of bytes in a kilobyte.
final public static  longONE_MB
     The number of bytes in a megabyte.

Constructor Summary
public  FileUtils()
     Instances should NOT be constructed in standard programming.

Method Summary
public static  StringbyteCountToDisplaySize(long size)
     Returns a human-readable version of the file size, where the input represents a specific number of bytes.
public static  Checksumchecksum(File file, Checksum checksum)
     Computes the checksum of a file using the specified checksum object.
public static  longchecksumCRC32(File file)
     Computes the checksum of a file using the CRC32 checksum routine.
public static  voidcleanDirectory(File directory)
     Clean a directory without deleting it.
public static  booleancontentEquals(File file1, File file2)
     Compare the contents of two files to determine if they are equal or not.
public static  File[]convertFileCollectionToFileArray(Collection files)
     Converts a Collection containing java.io.File instanced into array representation.
public static  voidcopyDirectory(File srcDir, File destDir)
     Copies a whole directory to a new location preserving the file dates.
public static  voidcopyDirectory(File srcDir, File destDir, boolean preserveFileDate)
     Copies a whole directory to a new location.
public static  voidcopyDirectoryToDirectory(File srcDir, File destDir)
     Copies a directory to within another directory preserving the file dates.
public static  voidcopyFile(File srcFile, File destFile)
     Copies a file to a new location preserving the file date.

This method copies the contents of the specified source file to the specified destination file.

public static  voidcopyFile(File srcFile, File destFile, boolean preserveFileDate)
     Copies a file to a new location.
public static  voidcopyFileToDirectory(File srcFile, File destDir)
     Copies a file to a directory preserving the file date.
public static  voidcopyFileToDirectory(File srcFile, File destDir, boolean preserveFileDate)
     Copies a file to a directory optionally preserving the file date.
public static  voidcopyURLToFile(URL source, File destination)
     Copies bytes from the URL source to a file destination.
public static  voiddeleteDirectory(File directory)
     Recursively delete a directory.
public static  voidforceDelete(File file)
     Delete a file.
public static  voidforceDeleteOnExit(File file)
     Schedule a file to be deleted when JVM exits.
public static  voidforceMkdir(File directory)
     Make a directory, including any necessary but nonexistent parent directories.
public static  booleanisFileNewer(File file, File reference)
     Tests if the specified File is newer than the reference File.
public static  booleanisFileNewer(File file, Date date)
     Tests if the specified File is newer than the specified Date.
public static  booleanisFileNewer(File file, long timeMillis)
     Tests if the specified File is newer than the specified time reference.
public static  booleanisFileOlder(File file, File reference)
     Tests if the specified File is older than the reference File.
public static  booleanisFileOlder(File file, Date date)
     Tests if the specified File is older than the specified Date.
public static  booleanisFileOlder(File file, long timeMillis)
     Tests if the specified File is older than the specified time reference.
public static  IteratoriterateFiles(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter)
     Allows iteration over the files in given directory (and optionally its subdirectories).

All files found are filtered by an IOFileFilter.

public static  IteratoriterateFiles(File directory, String[] extensions, boolean recursive)
     Allows iteration over the files in a given directory (and optionally its subdirectories) which match an array of extensions.
public static  LineIteratorlineIterator(File file, String encoding)
     Return an Iterator for the lines in a File.

This method opens an InputStream for the file. When you have finished with the iterator you should close the stream to free internal resources.

public static  LineIteratorlineIterator(File file)
     Return an Iterator for the lines in a File using the default encoding for the VM.
public static  CollectionlistFiles(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter)
     Finds files within a given directory (and optionally its subdirectories).
public static  CollectionlistFiles(File directory, String[] extensions, boolean recursive)
     Finds files within a given directory (and optionally its subdirectories) which match an array of extensions.
Parameters:
  directory - the directory to search in
Parameters:
  extensions - an array of extensions, ex.
public static  FileInputStreamopenInputStream(File file)
     Opens a FileInputStream for the specified file, providing better error messages than simply calling new FileInputStream(file).
public static  FileOutputStreamopenOutputStream(File file)
     Opens a FileOutputStream for the specified file, checking and creating the parent directory if it does not exist.
public static  byte[]readFileToByteArray(File file)
     Reads the contents of a file into a byte array.
public static  StringreadFileToString(File file, String encoding)
     Reads the contents of a file into a String.
public static  StringreadFileToString(File file)
     Reads the contents of a file into a String using the default encoding for the VM.
public static  ListreadLines(File file, String encoding)
     Reads the contents of a file line by line to a List of Strings.
public static  ListreadLines(File file)
     Reads the contents of a file line by line to a List of Strings using the default encoding for the VM.
public static  longsizeOfDirectory(File directory)
     Recursively count size of a directory (sum of the length of all files).
public static  FiletoFile(URL url)
     Convert from a URL to a File.
public static  File[]toFiles(URL[] urls)
     Converts each of an array of URL to a File.
public static  URL[]toURLs(File[] files)
     Converts each of an array of File to a URL.
public static  voidtouch(File file)
     Implements the same behaviour as the "touch" utility on Unix.
public static  booleanwaitFor(File file, int seconds)
     Waits for NFS to propagate a file creation, imposing a timeout.
public static  voidwriteByteArrayToFile(File file, byte[] data)
     Writes a byte array to a file creating the file if it does not exist.
public static  voidwriteLines(File file, String encoding, Collection lines)
     Writes the toString() value of each item in a collection to the specified File line by line.
public static  voidwriteLines(File file, Collection lines)
     Writes the toString() value of each item in a collection to the specified File line by line.
public static  voidwriteLines(File file, String encoding, Collection lines, String lineEnding)
     Writes the toString() value of each item in a collection to the specified File line by line.
public static  voidwriteLines(File file, Collection lines, String lineEnding)
     Writes the toString() value of each item in a collection to the specified File line by line.
public static  voidwriteStringToFile(File file, String data, String encoding)
     Writes a String to a file creating the file if it does not exist.
public static  voidwriteStringToFile(File file, String data)
     Writes a String to a file creating the file if it does not exist using the default encoding for the VM.

Field Detail
EMPTY_FILE_ARRAY
final public static File[] EMPTY_FILE_ARRAY(Code)
An empty array of type File.



ONE_GB
final public static long ONE_GB(Code)
The number of bytes in a gigabyte.



ONE_KB
final public static long ONE_KB(Code)
The number of bytes in a kilobyte.



ONE_MB
final public static long ONE_MB(Code)
The number of bytes in a megabyte.




Constructor Detail
FileUtils
public FileUtils()(Code)
Instances should NOT be constructed in standard programming.




Method Detail
byteCountToDisplaySize
public static String byteCountToDisplaySize(long size)(Code)
Returns a human-readable version of the file size, where the input represents a specific number of bytes.
Parameters:
  size - the number of bytes a human-readable display value (includes units)



checksum
public static Checksum checksum(File file, Checksum checksum) throws IOException(Code)
Computes the checksum of a file using the specified checksum object. Multiple files may be checked using one Checksum instance if desired simply by reusing the same checksum object. For example:
 long csum = FileUtils.checksum(file, new CRC32()).getValue();
 

Parameters:
  file - the file to checksum, must not be null
Parameters:
  checksum - the checksum object to be used, must not be null the checksum specified, updated with the content of the file
throws:
  NullPointerException - if the file or checksum is null
throws:
  IllegalArgumentException - if the file is a directory
throws:
  IOException - if an IO error occurs reading the file
since:
   Commons IO 1.3



checksumCRC32
public static long checksumCRC32(File file) throws IOException(Code)
Computes the checksum of a file using the CRC32 checksum routine. The value of the checksum is returned.
Parameters:
  file - the file to checksum, must not be null the checksum value
throws:
  NullPointerException - if the file or checksum is null
throws:
  IllegalArgumentException - if the file is a directory
throws:
  IOException - if an IO error occurs reading the file
since:
   Commons IO 1.3



cleanDirectory
public static void cleanDirectory(File directory) throws IOException(Code)
Clean a directory without deleting it.
Parameters:
  directory - directory to clean
throws:
  IOException - in case cleaning is unsuccessful



contentEquals
public static boolean contentEquals(File file1, File file2) throws IOException(Code)
Compare the contents of two files to determine if they are equal or not.

This method checks to see if the two files are different lengths or if they point to the same file, before resorting to byte-by-byte comparison of the contents.

Code origin: Avalon
Parameters:
  file1 - the first file
Parameters:
  file2 - the second file true if the content of the files are equal or they both don'texist, false otherwise
throws:
  IOException - in case of an I/O error




convertFileCollectionToFileArray
public static File[] convertFileCollectionToFileArray(Collection files)(Code)
Converts a Collection containing java.io.File instanced into array representation. This is to account for the difference between File.listFiles() and FileUtils.listFiles().
Parameters:
  files - a Collection containing java.io.File instances an array of java.io.File



copyDirectory
public static void copyDirectory(File srcDir, File destDir) throws IOException(Code)
Copies a whole directory to a new location preserving the file dates.

This method copies the specified directory and all its child directories and files to the specified destination. The destination is the new location and name of the directory.

The destination directory is created if it does not exist. If the destination directory did exist, then this method merges the source with the destination, with the source taking precedence.
Parameters:
  srcDir - an existing directory to copy, must not be null
Parameters:
  destDir - the new directory, must not be null
throws:
  NullPointerException - if source or destination is null
throws:
  IOException - if source or destination is invalid
throws:
  IOException - if an IO error occurs during copying
since:
   Commons IO 1.1




copyDirectory
public static void copyDirectory(File srcDir, File destDir, boolean preserveFileDate) throws IOException(Code)
Copies a whole directory to a new location.

This method copies the contents of the specified source directory to within the specified destination directory.

The destination directory is created if it does not exist. If the destination directory did exist, then this method merges the source with the destination, with the source taking precedence.
Parameters:
  srcDir - an existing directory to copy, must not be null
Parameters:
  destDir - the new directory, must not be null
Parameters:
  preserveFileDate - true if the file date of the copyshould be the same as the original
throws:
  NullPointerException - if source or destination is null
throws:
  IOException - if source or destination is invalid
throws:
  IOException - if an IO error occurs during copying
since:
   Commons IO 1.1




copyDirectoryToDirectory
public static void copyDirectoryToDirectory(File srcDir, File destDir) throws IOException(Code)
Copies a directory to within another directory preserving the file dates.

This method copies the source directory and all its contents to a directory of the same name in the specified destination directory.

The destination directory is created if it does not exist. If the destination directory did exist, then this method merges the source with the destination, with the source taking precedence.
Parameters:
  srcDir - an existing directory to copy, must not be null
Parameters:
  destDir - the directory to place the copy in, must not be null
throws:
  NullPointerException - if source or destination is null
throws:
  IOException - if source or destination is invalid
throws:
  IOException - if an IO error occurs during copying
since:
   Commons IO 1.2




copyFile
public static void copyFile(File srcFile, File destFile) throws IOException(Code)
Copies a file to a new location preserving the file date.

This method copies the contents of the specified source file to the specified destination file. The directory holding the destination file is created if it does not exist. If the destination file exists, then this method will overwrite it.
Parameters:
  srcFile - an existing file to copy, must not be null
Parameters:
  destFile - the new file, must not be null
throws:
  NullPointerException - if source or destination is null
throws:
  IOException - if source or destination is invalid
throws:
  IOException - if an IO error occurs during copying
See Also:   FileUtils.copyFileToDirectory(File,File)




copyFile
public static void copyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException(Code)
Copies a file to a new location.

This method copies the contents of the specified source file to the specified destination file. The directory holding the destination file is created if it does not exist. If the destination file exists, then this method will overwrite it.
Parameters:
  srcFile - an existing file to copy, must not be null
Parameters:
  destFile - the new file, must not be null
Parameters:
  preserveFileDate - true if the file date of the copyshould be the same as the original
throws:
  NullPointerException - if source or destination is null
throws:
  IOException - if source or destination is invalid
throws:
  IOException - if an IO error occurs during copying
See Also:   FileUtils.copyFileToDirectory(File,File,boolean)




copyFileToDirectory
public static void copyFileToDirectory(File srcFile, File destDir) throws IOException(Code)
Copies a file to a directory preserving the file date.

This method copies the contents of the specified source file to a file of the same name in the specified destination directory. The destination directory is created if it does not exist. If the destination file exists, then this method will overwrite it.
Parameters:
  srcFile - an existing file to copy, must not be null
Parameters:
  destDir - the directory to place the copy in, must not be null
throws:
  NullPointerException - if source or destination is null
throws:
  IOException - if source or destination is invalid
throws:
  IOException - if an IO error occurs during copying
See Also:   FileUtils.copyFile(File,File,boolean)




copyFileToDirectory
public static void copyFileToDirectory(File srcFile, File destDir, boolean preserveFileDate) throws IOException(Code)
Copies a file to a directory optionally preserving the file date.

This method copies the contents of the specified source file to a file of the same name in the specified destination directory. The destination directory is created if it does not exist. If the destination file exists, then this method will overwrite it.
Parameters:
  srcFile - an existing file to copy, must not be null
Parameters:
  destDir - the directory to place the copy in, must not be null
Parameters:
  preserveFileDate - true if the file date of the copyshould be the same as the original
throws:
  NullPointerException - if source or destination is null
throws:
  IOException - if source or destination is invalid
throws:
  IOException - if an IO error occurs during copying
See Also:   FileUtils.copyFile(File,File,boolean)
since:
   Commons IO 1.3




copyURLToFile
public static void copyURLToFile(URL source, File destination) throws IOException(Code)
Copies bytes from the URL source to a file destination. The directories up to destination will be created if they don't already exist. destination will be overwritten if it already exists.
Parameters:
  source - the URL to copy bytes from, must not be null
Parameters:
  destination - the non-directory File to write bytes to(possibly overwriting), must not be null
throws:
  IOException - if source URL cannot be opened
throws:
  IOException - if destination is a directory
throws:
  IOException - if destination cannot be written
throws:
  IOException - if destination needs creating but can't be
throws:
  IOException - if an IO error occurs during copying



deleteDirectory
public static void deleteDirectory(File directory) throws IOException(Code)
Recursively delete a directory.
Parameters:
  directory - directory to delete
throws:
  IOException - in case deletion is unsuccessful



forceDelete
public static void forceDelete(File file) throws IOException(Code)
Delete a file. If file is a directory, delete it and all sub-directories.

The difference between File.delete() and this method are:

  • A directory to be deleted does not have to be empty.
  • You get exceptions when a file or directory cannot be deleted. (java.io.File methods returns a boolean)

Parameters:
  file - file or directory to delete, must not be null
throws:
  NullPointerException - if the directory is null
throws:
  IOException - in case deletion is unsuccessful



forceDeleteOnExit
public static void forceDeleteOnExit(File file) throws IOException(Code)
Schedule a file to be deleted when JVM exits. If file is directory delete it and all sub-directories.
Parameters:
  file - file or directory to delete, must not be null
throws:
  NullPointerException - if the file is null
throws:
  IOException - in case deletion is unsuccessful



forceMkdir
public static void forceMkdir(File directory) throws IOException(Code)
Make a directory, including any necessary but nonexistent parent directories. If there already exists a file with specified name or the directory cannot be created then an exception is thrown.
Parameters:
  directory - directory to create, must not be null
throws:
  NullPointerException - if the directory is null
throws:
  IOException - if the directory cannot be created



isFileNewer
public static boolean isFileNewer(File file, File reference)(Code)
Tests if the specified File is newer than the reference File.
Parameters:
  file - the File of which the modification date mustbe compared, must not be null
Parameters:
  reference - the File of which the modification dateis used, must not be null true if the File exists and has been modified morerecently than the reference File
throws:
  IllegalArgumentException - if the file is null
throws:
  IllegalArgumentException - if the reference file is null or doesn't exist



isFileNewer
public static boolean isFileNewer(File file, Date date)(Code)
Tests if the specified File is newer than the specified Date.
Parameters:
  file - the File of which the modification datemust be compared, must not be null
Parameters:
  date - the date reference, must not be null true if the File exists and has been modifiedafter the given Date.
throws:
  IllegalArgumentException - if the file is null
throws:
  IllegalArgumentException - if the date is null



isFileNewer
public static boolean isFileNewer(File file, long timeMillis)(Code)
Tests if the specified File is newer than the specified time reference.
Parameters:
  file - the File of which the modification date mustbe compared, must not be null
Parameters:
  timeMillis - the time reference measured in milliseconds since theepoch (00:00:00 GMT, January 1, 1970) true if the File exists and has been modified afterthe given time reference.
throws:
  IllegalArgumentException - if the file is null



isFileOlder
public static boolean isFileOlder(File file, File reference)(Code)
Tests if the specified File is older than the reference File.
Parameters:
  file - the File of which the modification date mustbe compared, must not be null
Parameters:
  reference - the File of which the modification dateis used, must not be null true if the File exists and has been modified beforethe reference File
throws:
  IllegalArgumentException - if the file is null
throws:
  IllegalArgumentException - if the reference file is null or doesn't exist



isFileOlder
public static boolean isFileOlder(File file, Date date)(Code)
Tests if the specified File is older than the specified Date.
Parameters:
  file - the File of which the modification datemust be compared, must not be null
Parameters:
  date - the date reference, must not be null true if the File exists and has been modifiedbefore the given Date.
throws:
  IllegalArgumentException - if the file is null
throws:
  IllegalArgumentException - if the date is null



isFileOlder
public static boolean isFileOlder(File file, long timeMillis)(Code)
Tests if the specified File is older than the specified time reference.
Parameters:
  file - the File of which the modification date mustbe compared, must not be null
Parameters:
  timeMillis - the time reference measured in milliseconds since theepoch (00:00:00 GMT, January 1, 1970) true if the File exists and has been modified beforethe given time reference.
throws:
  IllegalArgumentException - if the file is null



iterateFiles
public static Iterator iterateFiles(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter)(Code)
Allows iteration over the files in given directory (and optionally its subdirectories).

All files found are filtered by an IOFileFilter. This method is based on FileUtils.listFiles(File,IOFileFilter,IOFileFilter) .
Parameters:
  directory - the directory to search in
Parameters:
  fileFilter - filter to apply when finding files.
Parameters:
  dirFilter - optional filter to apply when finding subdirectories.If this parameter is null, subdirectories will not be included in thesearch. Use TrueFileFilter.INSTANCE to match all directories. an iterator of java.io.File for the matching files
See Also:   org.apache.commons.io.filefilter.FileFilterUtils
See Also:   org.apache.commons.io.filefilter.NameFileFilter
since:
   Commons IO 1.2




iterateFiles
public static Iterator iterateFiles(File directory, String[] extensions, boolean recursive)(Code)
Allows iteration over the files in a given directory (and optionally its subdirectories) which match an array of extensions. This method is based on FileUtils.listFiles(File,String[],boolean) .
Parameters:
  directory - the directory to search in
Parameters:
  extensions - an array of extensions, ex. {"java","xml"}. If thisparameter is null, all files are returned.
Parameters:
  recursive - if true all subdirectories are searched as well an iterator of java.io.File with the matching files
since:
   Commons IO 1.2



lineIterator
public static LineIterator lineIterator(File file, String encoding) throws IOException(Code)
Return an Iterator for the lines in a File.

This method opens an InputStream for the file. When you have finished with the iterator you should close the stream to free internal resources. This can be done by calling the LineIterator.close or LineIterator.closeQuietly(LineIterator) method.

The recommended usage pattern is:

 LineIterator it = FileUtils.lineIterator(file, "UTF-8");
 try {
 while (it.hasNext()) {
 String line = it.nextLine();
 /// do something with line
 }
 } finally {
 LineIterator.closeQuietly(iterator);
 }
 

If an exception occurs during the creation of the iterator, the underlying stream is closed.
Parameters:
  file - the file to open for input, must not be null
Parameters:
  encoding - the encoding to use, null means platform default an Iterator of the lines in the file, never null
throws:
  IOException - in case of an I/O error (file closed)
since:
   Commons IO 1.2




lineIterator
public static LineIterator lineIterator(File file) throws IOException(Code)
Return an Iterator for the lines in a File using the default encoding for the VM.
Parameters:
  file - the file to open for input, must not be null an Iterator of the lines in the file, never null
throws:
  IOException - in case of an I/O error (file closed)
since:
   Commons IO 1.3
See Also:   FileUtils.lineIterator(File,String)



listFiles
public static Collection listFiles(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter)(Code)
Finds files within a given directory (and optionally its subdirectories). All files found are filtered by an IOFileFilter.

If your search should recurse into subdirectories you can pass in an IOFileFilter for directories. You don't need to bind a DirectoryFileFilter (via logical AND) to this filter. This method does that for you.

An example: If you want to search through all directories called "temp" you pass in FileFilterUtils.NameFileFilter("temp")

Another common usage of this method is find files in a directory tree but ignoring the directories generated CVS. You can simply pass in FileFilterUtils.makeCVSAware(null).
Parameters:
  directory - the directory to search in
Parameters:
  fileFilter - filter to apply when finding files.
Parameters:
  dirFilter - optional filter to apply when finding subdirectories.If this parameter is null, subdirectories will not be included in thesearch. Use TrueFileFilter.INSTANCE to match all directories. an collection of java.io.File with the matching files
See Also:   org.apache.commons.io.filefilter.FileFilterUtils
See Also:   org.apache.commons.io.filefilter.NameFileFilter




listFiles
public static Collection listFiles(File directory, String[] extensions, boolean recursive)(Code)
Finds files within a given directory (and optionally its subdirectories) which match an array of extensions.
Parameters:
  directory - the directory to search in
Parameters:
  extensions - an array of extensions, ex. {"java","xml"}. If thisparameter is null, all files are returned.
Parameters:
  recursive - if true all subdirectories are searched as well an collection of java.io.File with the matching files



openInputStream
public static FileInputStream openInputStream(File file) throws IOException(Code)
Opens a FileInputStream for the specified file, providing better error messages than simply calling new FileInputStream(file).

At the end of the method either the stream will be successfully opened, or an exception will have been thrown.

An exception is thrown if the file does not exist. An exception is thrown if the file object exists but is a directory. An exception is thrown if the file exists but cannot be read.
Parameters:
  file - the file to open for input, must not be null a new FileInputStream for the specified file
throws:
  FileNotFoundException - if the file does not exist
throws:
  IOException - if the file object is a directory
throws:
  IOException - if the file cannot be read
since:
   Commons IO 1.3




openOutputStream
public static FileOutputStream openOutputStream(File file) throws IOException(Code)
Opens a FileOutputStream for the specified file, checking and creating the parent directory if it does not exist.

At the end of the method either the stream will be successfully opened, or an exception will have been thrown.

The parent directory will be created if it does not exist. The file will be created if it does not exist. An exception is thrown if the file object exists but is a directory. An exception is thrown if the file exists but cannot be written to. An exception is thrown if the parent directory cannot be created.
Parameters:
  file - the file to open for output, must not be null a new FileOutputStream for the specified file
throws:
  IOException - if the file object is a directory
throws:
  IOException - if the file cannot be written to
throws:
  IOException - if a parent directory needs creating but that fails
since:
   Commons IO 1.3




readFileToByteArray
public static byte[] readFileToByteArray(File file) throws IOException(Code)
Reads the contents of a file into a byte array. The file is always closed.
Parameters:
  file - the file to read, must not be null the file contents, never null
throws:
  IOException - in case of an I/O error
since:
   Commons IO 1.1



readFileToString
public static String readFileToString(File file, String encoding) throws IOException(Code)
Reads the contents of a file into a String. The file is always closed.
Parameters:
  file - the file to read, must not be null
Parameters:
  encoding - the encoding to use, null means platform default the file contents, never null
throws:
  IOException - in case of an I/O error
throws:
  java.io.UnsupportedEncodingException - if the encoding is not supported by the VM



readFileToString
public static String readFileToString(File file) throws IOException(Code)
Reads the contents of a file into a String using the default encoding for the VM. The file is always closed.
Parameters:
  file - the file to read, must not be null the file contents, never null
throws:
  IOException - in case of an I/O error
since:
   Commons IO 1.3.1



readLines
public static List readLines(File file, String encoding) throws IOException(Code)
Reads the contents of a file line by line to a List of Strings. The file is always closed.
Parameters:
  file - the file to read, must not be null
Parameters:
  encoding - the encoding to use, null means platform default the list of Strings representing each line in the file, never null
throws:
  IOException - in case of an I/O error
throws:
  java.io.UnsupportedEncodingException - if the encoding is not supported by the VM
since:
   Commons IO 1.1



readLines
public static List readLines(File file) throws IOException(Code)
Reads the contents of a file line by line to a List of Strings using the default encoding for the VM. The file is always closed.
Parameters:
  file - the file to read, must not be null the list of Strings representing each line in the file, never null
throws:
  IOException - in case of an I/O error
since:
   Commons IO 1.3



sizeOfDirectory
public static long sizeOfDirectory(File directory)(Code)
Recursively count size of a directory (sum of the length of all files).
Parameters:
  directory - directory to inspect, must not be null size of directory in bytes, 0 if directory is security restricted
throws:
  NullPointerException - if the directory is null



toFile
public static File toFile(URL url)(Code)
Convert from a URL to a File.

From version 1.1 this method will decode the URL. Syntax such as file:///my%20docs/file.txt will be correctly decoded to /my docs/file.txt.
Parameters:
  url - the file URL to convert, null returns null the equivalent File object, or nullif the URL's protocol is not file
throws:
  IllegalArgumentException - if the file is incorrectly encoded




toFiles
public static File[] toFiles(URL[] urls)(Code)
Converts each of an array of URL to a File.

Returns an array of the same size as the input. If the input is null, an empty array is returned. If the input contains null, the output array contains null at the same index.

This method will decode the URL. Syntax such as file:///my%20docs/file.txt will be correctly decoded to /my docs/file.txt.
Parameters:
  urls - the file URLs to convert, null returns empty array a non-null array of Files matching the input, with a null itemif there was a null at that index in the input array
throws:
  IllegalArgumentException - if any file is not a URL file
throws:
  IllegalArgumentException - if any file is incorrectly encoded
since:
   Commons IO 1.1




toURLs
public static URL[] toURLs(File[] files) throws IOException(Code)
Converts each of an array of File to a URL.

Returns an array of the same size as the input.
Parameters:
  files - the files to convert an array of URLs matching the input
throws:
  IOException - if a file cannot be converted




touch
public static void touch(File file) throws IOException(Code)
Implements the same behaviour as the "touch" utility on Unix. It creates a new file with size 0 or, if the file exists already, it is opened and closed without modifying it, but updating the file date and time.

NOTE: As from v1.3, this method throws an IOException if the last modified date of the file cannot be set. Also, as from v1.3 this method creates parent directories if they do not exist.
Parameters:
  file - the File to touch
throws:
  IOException - If an I/O problem occurs




waitFor
public static boolean waitFor(File file, int seconds)(Code)
Waits for NFS to propagate a file creation, imposing a timeout.

This method repeatedly tests File.exists until it returns true up to the maximum time specified in seconds.
Parameters:
  file - the file to check, must not be null
Parameters:
  seconds - the maximum time in seconds to wait true if file exists
throws:
  NullPointerException - if the file is null




writeByteArrayToFile
public static void writeByteArrayToFile(File file, byte[] data) throws IOException(Code)
Writes a byte array to a file creating the file if it does not exist.

NOTE: As from v1.3, the parent directories of the file will be created if they do not exist.
Parameters:
  file - the file to write to
Parameters:
  data - the content to write to the file
throws:
  IOException - in case of an I/O error
since:
   Commons IO 1.1




writeLines
public static void writeLines(File file, String encoding, Collection lines) throws IOException(Code)
Writes the toString() value of each item in a collection to the specified File line by line. The specified character encoding and the default line ending will be used.

NOTE: As from v1.3, the parent directories of the file will be created if they do not exist.
Parameters:
  file - the file to write to
Parameters:
  encoding - the encoding to use, null means platform default
Parameters:
  lines - the lines to write, null entries produce blank lines
throws:
  IOException - in case of an I/O error
throws:
  java.io.UnsupportedEncodingException - if the encoding is not supported by the VM
since:
   Commons IO 1.1




writeLines
public static void writeLines(File file, Collection lines) throws IOException(Code)
Writes the toString() value of each item in a collection to the specified File line by line. The default VM encoding and the default line ending will be used.
Parameters:
  file - the file to write to
Parameters:
  lines - the lines to write, null entries produce blank lines
throws:
  IOException - in case of an I/O error
since:
   Commons IO 1.3



writeLines
public static void writeLines(File file, String encoding, Collection lines, String lineEnding) throws IOException(Code)
Writes the toString() value of each item in a collection to the specified File line by line. The specified character encoding and the line ending will be used.

NOTE: As from v1.3, the parent directories of the file will be created if they do not exist.
Parameters:
  file - the file to write to
Parameters:
  encoding - the encoding to use, null means platform default
Parameters:
  lines - the lines to write, null entries produce blank lines
Parameters:
  lineEnding - the line separator to use, null is system default
throws:
  IOException - in case of an I/O error
throws:
  java.io.UnsupportedEncodingException - if the encoding is not supported by the VM
since:
   Commons IO 1.1




writeLines
public static void writeLines(File file, Collection lines, String lineEnding) throws IOException(Code)
Writes the toString() value of each item in a collection to the specified File line by line. The default VM encoding and the specified line ending will be used.
Parameters:
  file - the file to write to
Parameters:
  lines - the lines to write, null entries produce blank lines
Parameters:
  lineEnding - the line separator to use, null is system default
throws:
  IOException - in case of an I/O error
since:
   Commons IO 1.3



writeStringToFile
public static void writeStringToFile(File file, String data, String encoding) throws IOException(Code)
Writes a String to a file creating the file if it does not exist. NOTE: As from v1.3, the parent directories of the file will be created if they do not exist.
Parameters:
  file - the file to write
Parameters:
  data - the content to write to the file
Parameters:
  encoding - the encoding to use, null means platform default
throws:
  IOException - in case of an I/O error
throws:
  java.io.UnsupportedEncodingException - if the encoding is not supported by the VM



writeStringToFile
public static void writeStringToFile(File file, String data) throws IOException(Code)
Writes a String to a file creating the file if it does not exist using the default encoding for the VM.
Parameters:
  file - the file to write
Parameters:
  data - the content to write to the file
throws:
  IOException - in case of an I/O error



Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.