to top
public class

FileUtil

extends Object
java.lang.Object
   ↳ com.android.tradefed.util.FileUtil

Class Overview

A helper class for file related operations

Summary

Nested Classes
class FileUtil.LowDiskSpaceException Thrown if usable disk space is below minimum threshold. 
Public Constructors
FileUtil()
Public Methods
static boolean chmod(File file, String perms)
static boolean chmodGroupRW(File file)
Performs a best effort attempt to make given file group readable and writable.
static boolean chmodGroupRWX(File file)
Performs a best effort attempt to make given file group executable, readable, and writable.
static boolean chmodRWXRecursively(File file)
static void closeZip(ZipFile zipFile)
This method is deprecated. Moved to closeZip(ZipFile).
static List<File> collectJars(File dir)
Returns all jar files found in given directory
static boolean compareFileContents(File file1, File file2)
Utility method to do byte-wise content comparison of two files.
static long convertSizeToBytes(String sizeString)
static String convertToReadableSize(long sizeLong)
Convert the given file size in bytes to a more readable format in X.Y[KMGT] format.
static void copyFile(File origFile, File destFile)
A helper method that copies a file's contents to a local file
static File createNamedTempDir(String name)
Helper function to create a named directory inside your temp folder.
static File createTempDir(String prefix)
Helper function to create a temp directory in the system default temporary file directory.
static File createTempDir(String prefix, File parentDir)
Helper function to create a temp directory.
static File createTempFile(String prefix, String suffix, File parentDir)
Helper wrapper function around createTempFile(String, String, File) that audits for potential out of disk space scenario.
static File createTempFile(String prefix, String suffix)
Helper wrapper function around createTempFile(String, String) that audits for potential out of disk space scenario.
static File createTempFileForRemote(String remoteFilePath, File parentDir)
Helper method which constructs a unique file on temporary disk, whose name corresponds as closely as possible to the file name given by the remote file path
static File createZip(File dir)
This method is deprecated. Moved to createZip(File).
static void createZip(File dir, File zipFile)
This method is deprecated. Moved to createZip(File, File).
static void deleteFile(File file)
Try to delete a file.
static File extractFileFromZip(ZipFile zipFile, String filePath)
This method is deprecated. Moved to extractFileFromZip(ZipFile, String).
static void extractZip(ZipFile zipFile, File destDir)
This method is deprecated. Moved to extractZip(ZipFile, File).
static Set<File> findDirsUnder(File rootDir, File relativeParent)
Recursively find all directories under the given rootDir
static File findFile(File dir, String fileName)
Recursively search given directory for first file with given name
static String getBaseName(String fileName)
Gets the base name, without extension, of given file name.
static String getExtension(String fileName)
Gets the extension for given file name.
static File getFileForPath(File parentDir, String... pathSegments)
Helper method to build a system-dependent File
static String getPath(String... pathSegments)
Helper method to build a system-dependent relative path
static void gzipFile(File file, File gzipFile)
This method is deprecated. Moved to gzipFile(File, File).
static void hardlinkFile(File origFile, File destFile)
A helper method that hardlinks a file to another file
static boolean mkdirsRWX(File file)
Method to create a chain of directories, and set them all group execute/read/writable as they are created, by calling chmodGroupRWX(File).
static String readStringFromFile(File sourceFile)
A helper method for reading string data from a file
static void recursiveCopy(File sourceDir, File destDir)
Recursively copy folder contents.
static void recursiveDelete(File rootDir)
Recursively delete given file and all its contents
static void recursiveHardlink(File sourceDir, File destDir)
Recursively hardlink folder contents.
static void setReadableRecursive(File file)
Recursively set read and exec (if folder) permissions for given file.
static void writeToFile(InputStream input, File destFile)
A helper method for writing stream data to file
static void writeToFile(String inputString, File destFile)
A helper method for writing string data to file
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public FileUtil ()

Public Methods

public static boolean chmod (File file, String perms)

public static boolean chmodGroupRW (File file)

Performs a best effort attempt to make given file group readable and writable.

Note that the execute permission is required to make directories accessible. See chmodGroupRWX(File).

If 'chmod' system command is not supported by underlying OS, will set file to writable by all.

Parameters
file the File to make owner and group writable
Returns
  • true if file was successfully made group writable, false otherwise

public static boolean chmodGroupRWX (File file)

Performs a best effort attempt to make given file group executable, readable, and writable.

If 'chmod' system command is not supported by underlying OS, will attempt to set permissions for all users.

Parameters
file the File to make owner and group writable
Returns
  • true if permissions were set successfully, false otherwise

public static boolean chmodRWXRecursively (File file)

public static void closeZip (ZipFile zipFile)

This method is deprecated.
Moved to closeZip(ZipFile).

Close an open ZipFile, ignoring any exceptions.

Parameters
zipFile the file to close

public static List<File> collectJars (File dir)

Returns all jar files found in given directory

public static boolean compareFileContents (File file1, File file2)

Utility method to do byte-wise content comparison of two files.

Returns
  • true if file contents are identical
Throws
IOException

public static long convertSizeToBytes (String sizeString)

The inverse of convertToReadableSize(long). Converts the readable format described in convertToReadableSize(long) to a byte value.

Parameters
sizeString the string description of the size.
Returns
  • the size in bytes
Throws
IllegalArgumentException if cannot recognize size

public static String convertToReadableSize (long sizeLong)

Convert the given file size in bytes to a more readable format in X.Y[KMGT] format.

Parameters
sizeLong file size in bytes
Returns
  • descriptive string of file size

public static void copyFile (File origFile, File destFile)

A helper method that copies a file's contents to a local file

Parameters
origFile the original file to be copied
destFile the destination file
Throws
IOException if failed to copy file

public static File createNamedTempDir (String name)

Helper function to create a named directory inside your temp folder.

This directory will not have it's name randomized. If the directory already exists it will be returned.

Parameters
name The name of the directory to create in your tmp folder.
Returns
  • the created directory
Throws
IOException

public static File createTempDir (String prefix)

Helper function to create a temp directory in the system default temporary file directory.

Parameters
prefix The prefix string to be used in generating the file's name; must be at least three characters long
Returns
  • the created directory
Throws
IOException if file could not be created

public static File createTempDir (String prefix, File parentDir)

Helper function to create a temp directory.

Parameters
prefix The prefix string to be used in generating the file's name; must be at least three characters long
parentDir The parent directory in which the directory is to be created. If null the system default temp directory will be used.
Returns
  • the created directory
Throws
IOException if file could not be created

public static File createTempFile (String prefix, String suffix, File parentDir)

Helper wrapper function around createTempFile(String, String, File) that audits for potential out of disk space scenario.

Throws
FileUtil.LowDiskSpaceException if disk space on partition is lower than minimum allowed
IOException

public static File createTempFile (String prefix, String suffix)

Helper wrapper function around createTempFile(String, String) that audits for potential out of disk space scenario.

Throws
FileUtil.LowDiskSpaceException if disk space on temporary partition is lower than minimum allowed
IOException

public static File createTempFileForRemote (String remoteFilePath, File parentDir)

Helper method which constructs a unique file on temporary disk, whose name corresponds as closely as possible to the file name given by the remote file path

Parameters
remoteFilePath the '/' separated remote path to construct the name from
parentDir the parent directory to create the file in. null to use the default temporary directory
Throws
IOException

public static File createZip (File dir)

This method is deprecated.
Moved to createZip(File).

Utility method to create a temporary zip file containing the given directory and all its contents.

Parameters
dir the directory to zip
Returns
  • a temporary zip File containing directory contents
Throws
IOException if failed to create zip file

public static void createZip (File dir, File zipFile)

This method is deprecated.
Moved to createZip(File, File).

Utility method to create a zip file containing the given directory and all its contents.

Parameters
dir the directory to zip
zipFile the zip file to create - it should not already exist
Throws
IOException if failed to create zip file

public static void deleteFile (File file)

Try to delete a file. Intended for use when cleaning up in finally stanzas. may be null.

public static File extractFileFromZip (ZipFile zipFile, String filePath)

This method is deprecated.
Moved to extractFileFromZip(ZipFile, String).

Utility method to extract one specific file from zip file into a tmp file

Parameters
zipFile the ZipFile to extract
filePath the filePath of to extract
Returns
  • the File or null if not found
Throws
IOException if failed to extract file

public static void extractZip (ZipFile zipFile, File destDir)

This method is deprecated.
Moved to extractZip(ZipFile, File).

Utility method to extract entire contents of zip file into given directory

Parameters
zipFile the ZipFile to extract
destDir the local dir to extract file to
Throws
IOException if failed to extract file

public static Set<File> findDirsUnder (File rootDir, File relativeParent)

Recursively find all directories under the given rootDir

Parameters
rootDir the root directory to search in
relativeParent An optional parent for all Files returned. If not specified, all Files will be relative to rootDir.
Returns
  • An set of Files, representing all directories under rootDir, including rootDir itself. If rootDir is null, an empty set is returned.

public static File findFile (File dir, String fileName)

Recursively search given directory for first file with given name

Parameters
dir the directory to search
fileName the name of the file to search for
Returns
  • the File or null if it could not be found

public static String getBaseName (String fileName)

Gets the base name, without extension, of given file name.

e.g. getBaseName("file.txt") will return "file"

Returns
  • the base name

public static String getExtension (String fileName)

Gets the extension for given file name.

Returns
  • the extension or empty String if file has no extension

public static File getFileForPath (File parentDir, String... pathSegments)

Helper method to build a system-dependent File

Parameters
parentDir the parent directory to use.
pathSegments the relative path segments to use
Returns

public static String getPath (String... pathSegments)

Helper method to build a system-dependent relative path

Parameters
pathSegments the relative path segments to use
Returns

public static void gzipFile (File file, File gzipFile)

This method is deprecated.
Moved to gzipFile(File, File).

Helper method to create a gzipped version of a single file.

Parameters
file the original file
gzipFile the file to place compressed contents in
Throws
IOException
IOException

public static void hardlinkFile (File origFile, File destFile)

A helper method that hardlinks a file to another file

Parameters
origFile the original file
destFile the destination file
Throws
IOException if failed to hardlink file

public static boolean mkdirsRWX (File file)

Method to create a chain of directories, and set them all group execute/read/writable as they are created, by calling chmodGroupRWX(File). Essentially a version of mkdirs() that also runs chmod(File, String).

Parameters
file the name of the directory to create, possibly with containing directories that don't yet exist.
Returns
  • true if file exists and is a directory, false otherwise.

public static String readStringFromFile (File sourceFile)

A helper method for reading string data from a file

Parameters
sourceFile the file to read from
Throws
IOException
FileNotFoundException
IOException

public static void recursiveCopy (File sourceDir, File destDir)

Recursively copy folder contents.

Only supports copying of files and directories - symlinks are not copied.

Parameters
sourceDir the folder that contains the files to copy
destDir the destination folder
Throws
IOException

public static void recursiveDelete (File rootDir)

Recursively delete given file and all its contents

public static void recursiveHardlink (File sourceDir, File destDir)

Recursively hardlink folder contents.

Only supports copying of files and directories - symlinks are not copied.

Parameters
sourceDir the folder that contains the files to copy
destDir the destination folder
Throws
IOException

public static void setReadableRecursive (File file)

Recursively set read and exec (if folder) permissions for given file.

public static void writeToFile (InputStream input, File destFile)

A helper method for writing stream data to file

Parameters
input the unbuffered input stream
destFile the dest file to write to
Throws
IOException

public static void writeToFile (String inputString, File destFile)

A helper method for writing string data to file

Parameters
inputString the input String
destFile the dest file to write to
Throws
IOException