FileUtilspublic class FileUtils extends Object Tools for managing files. Not for public consumption. |
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_PATTERNRegular expression for safe filenames: no spaces or metacharacters |
Methods Summary |
---|
public static boolean | copyFile(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 boolean | copyToFile(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 int | getFatVolumeId(java.lang.String mountPoint)returns the FAT file system volume ID for the volume mounted
at the given mount point, or -1 for failure
| public static native boolean | getFileStatus(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.
| public static native int | getPermissions(java.lang.String file, int[] outPermissions)
| public static boolean | isFilenameSafe(java.io.File file)Check if a filename is "safe" (no metacharacters or spaces).
// 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.String | readTextFile(java.io.File file, int max, java.lang.String ellipsis)Read a text file into a String, optionally limiting the length.
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 int | setPermissions(java.lang.String file, int mode, int uid, int gid)
|
|