FileDocCategorySizeDatePackage
FileUtils.javaAPI DocAndroid 1.5 API7303Wed May 06 22:41:56 BST 2009android.os

FileUtils

public class FileUtils extends Object
Tools for managing files. Not for public consumption.
hide

Fields Summary
public static final int
S_IRWXU
public static final int
S_IRUSR
public static final int
S_IWUSR
public static final int
S_IXUSR
public static final int
S_IRWXG
public static final int
S_IRGRP
public static final int
S_IWGRP
public static final int
S_IXGRP
public static final int
S_IRWXO
public static final int
S_IROTH
public static final int
S_IWOTH
public static final int
S_IXOTH
private static final Pattern
SAFE_FILENAME_PATTERN
Regular expression for safe filenames: no spaces or metacharacters
Constructors Summary
Methods Summary
public static booleancopyFile(java.io.File srcFile, java.io.File destFile)


               

           

                                         
         
        
    // copy a file from srcFile to destFile, return true if succeed, return
    // false if fail
           
        boolean result = false;
        try {
            InputStream in = new FileInputStream(srcFile);
            try {
                result = copyToFile(in, destFile);
            } finally  {
                in.close();
            }
        } catch (IOException e) {
            result = false;
        }
        return result;
    
public static booleancopyToFile(java.io.InputStream inputStream, java.io.File destFile)
Copy data from a source stream to destFile. Return true if succeed, return false if failed.

        try {
            OutputStream out = new FileOutputStream(destFile);
            try {
                byte[] buffer = new byte[4096];
                int bytesRead;
                while ((bytesRead = inputStream.read(buffer)) >= 0) {
                    out.write(buffer, 0, bytesRead);
                }
            } finally {
                out.close();
            }
            return true;
        } catch (IOException e) {
            return false;
        }
    
public static native intgetFatVolumeId(java.lang.String mountPoint)
returns the FAT file system volume ID for the volume mounted at the given mount point, or -1 for failure

param
mount point for FAT volume
return
volume ID or -1

public static native booleangetFileStatus(java.lang.String path, android.os.FileUtils$FileStatus status)
Get the status for the given path. This is equivalent to the POSIX stat(2) system call.

param
path The path of the file to be stat'd.
param
status Optional argument to fill in. It will only fill in the status if the file exists.
return
true if the file exists and false if it does not exist. If you do not have permission to stat the file, then this method will return false.

public static native intgetPermissions(java.lang.String file, int[] outPermissions)

public static booleanisFilenameSafe(java.io.File file)
Check if a filename is "safe" (no metacharacters or spaces).

param
file The file to check

        // Note, we check whether it matches what's known to be safe,
        // rather than what's known to be unsafe.  Non-ASCII, control
        // characters, etc. are all unsafe by default.
        return SAFE_FILENAME_PATTERN.matcher(file.getPath()).matches();
    
public static java.lang.StringreadTextFile(java.io.File file, int max, java.lang.String ellipsis)
Read a text file into a String, optionally limiting the length.

param
file to read (will not seek, so things like /proc files are OK)
param
max length (positive for head, negative of tail, 0 for no limit)
param
ellipsis to add of the file was truncated (can be null)
return
the contents of the file, possibly truncated
throws
IOException if something goes wrong reading the file

        InputStream input = new FileInputStream(file);
        try {
            if (max > 0) {  // "head" mode: read the first N bytes
                byte[] data = new byte[max + 1];
                int length = input.read(data);
                if (length <= 0) return "";
                if (length <= max) return new String(data, 0, length);
                if (ellipsis == null) return new String(data, 0, max);
                return new String(data, 0, max) + ellipsis;
            } else if (max < 0) {  // "tail" mode: read it all, keep the last N
                int len;
                boolean rolled = false;
                byte[] last = null, data = null;
                do {
                    if (last != null) rolled = true;
                    byte[] tmp = last; last = data; data = tmp;
                    if (data == null) data = new byte[-max];
                    len = input.read(data);
                } while (len == data.length);

                if (last == null && len <= 0) return "";
                if (last == null) return new String(data, 0, len);
                if (len > 0) {
                    rolled = true;
                    System.arraycopy(last, len, last, 0, last.length - len);
                    System.arraycopy(data, 0, last, last.length - len, len);
                }
                if (ellipsis == null || !rolled) return new String(last);
                return ellipsis + new String(last);
            } else {  // "cat" mode: read it all
                ByteArrayOutputStream contents = new ByteArrayOutputStream();
                int len;
                byte[] data = new byte[1024];
                do {
                    len = input.read(data);
                    if (len > 0) contents.write(data, 0, len);
                } while (len == data.length);
                return contents.toString();
            }
        } finally {
            input.close();
        }
    
public static native intsetPermissions(java.lang.String file, int mode, int uid, int gid)