public class FileUtils extends Object
Modifier and Type | Field and Description |
---|---|
static int |
S_IRGRP |
static int |
S_IROTH |
static int |
S_IRUSR |
static int |
S_IRWXG |
static int |
S_IRWXO |
static int |
S_IRWXU |
static int |
S_IWGRP |
static int |
S_IWOTH |
static int |
S_IWUSR |
static int |
S_IXGRP |
static int |
S_IXOTH |
static int |
S_IXUSR |
Constructor and Description |
---|
FileUtils() |
Modifier and Type | Method and Description |
---|---|
static File |
buildUniqueFile(File parent,
String displayName)
Generates a unique file name under the given parent directory, keeping
any extension intact.
|
static File |
buildUniqueFile(File parent,
String mimeType,
String displayName)
Generates a unique file name under the given parent directory.
|
static String |
buildValidExtFilename(String name)
Mutate the given filename to make it valid for an ext4 filesystem,
replacing any invalid characters with "_".
|
static String |
buildValidFatFilename(String name)
Mutate the given filename to make it valid for a FAT filesystem,
replacing any invalid characters with "_".
|
static long |
checksumCrc32(File file)
Computes the checksum of a file using the CRC32 checksum routine.
|
static boolean |
contains(File[] dirs,
File file)
Test if a file lives under the given directory, either as a direct child
or a distant grandchild.
|
static boolean |
contains(File dir,
File file)
Test if a file lives under the given directory, either as a direct child
or a distant grandchild.
|
static boolean |
copyFile(File srcFile,
File destFile)
Deprecated.
|
static void |
copyFileOrThrow(File srcFile,
File destFile) |
static void |
copyPermissions(File from,
File to) |
static boolean |
copyToFile(InputStream inputStream,
File destFile)
Deprecated.
|
static void |
copyToFileOrThrow(InputStream inputStream,
File destFile)
Copy data from a source stream to destFile.
|
static boolean |
deleteContents(File dir) |
static boolean |
deleteContentsAndDir(File dir) |
static boolean |
deleteOlderFiles(File dir,
int minCount,
long minAge)
Delete older files in a directory until only those matching the given
constraints remain.
|
static int |
getUid(String path)
Return owning UID of given path, otherwise -1.
|
static boolean |
isFilenameSafe(File file)
Check if a filename is "safe" (no metacharacters or spaces).
|
static boolean |
isValidExtFilename(String name)
Check if given filename is valid for an ext4 filesystem.
|
static boolean |
isValidFatFilename(String name)
Check if given filename is valid for a FAT filesystem.
|
static File[] |
listFilesOrEmpty(File dir) |
static File[] |
listFilesOrEmpty(File dir,
FilenameFilter filter) |
static String[] |
listOrEmpty(File dir) |
static File |
newFileOrNull(String path) |
static String |
readTextFile(File file,
int max,
String ellipsis)
Read a text file into a String, optionally limiting the length.
|
static File |
rewriteAfterRename(File beforeDir,
File afterDir,
File file)
Given a path under the "before" directory, rewrite it to live under the
"after" directory.
|
static String |
rewriteAfterRename(File beforeDir,
File afterDir,
String path) |
static String[] |
rewriteAfterRename(File beforeDir,
File afterDir,
String[] paths) |
static int |
setPermissions(FileDescriptor fd,
int mode,
int uid,
int gid)
Set owner and mode of of given
FileDescriptor . |
static int |
setPermissions(File path,
int mode,
int uid,
int gid)
Set owner and mode of of given
File . |
static int |
setPermissions(String path,
int mode,
int uid,
int gid)
Set owner and mode of of given path.
|
static String[] |
splitFileName(String mimeType,
String displayName)
Splits file name into base name and extension.
|
static void |
stringToFile(File file,
String string) |
static void |
stringToFile(String filename,
String string)
Writes string to file.
|
static boolean |
sync(FileOutputStream stream)
Perform an fsync on the given FileOutputStream.
|
static String |
trimFilename(String str,
int maxBytes) |
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
public static int setPermissions(File path, int mode, int uid, int gid)
File
.mode
- to apply through chmod
uid
- to apply through chown
, or -1 to leave unchangedgid
- to apply through chown
, or -1 to leave unchangedpublic static int setPermissions(String path, int mode, int uid, int gid)
mode
- to apply through chmod
uid
- to apply through chown
, or -1 to leave unchangedgid
- to apply through chown
, or -1 to leave unchangedpublic static int setPermissions(FileDescriptor fd, int mode, int uid, int gid)
FileDescriptor
.mode
- to apply through chmod
uid
- to apply through chown
, or -1 to leave unchangedgid
- to apply through chown
, or -1 to leave unchangedpublic static void copyPermissions(File from, File to) throws IOException
IOException
public static int getUid(String path)
public static boolean sync(FileOutputStream stream)
@Deprecated public static boolean copyFile(File srcFile, File destFile)
public static void copyFileOrThrow(File srcFile, File destFile) throws IOException
IOException
@Deprecated public static boolean copyToFile(InputStream inputStream, File destFile)
public static void copyToFileOrThrow(InputStream inputStream, File destFile) throws IOException
IOException
public static boolean isFilenameSafe(File file)
file
- The file to checkpublic static String readTextFile(File file, int max, String ellipsis) throws IOException
file
- to read (will not seek, so things like /proc files are OK)max
- length (positive for head, negative of tail, 0 for no limit)ellipsis
- to add of the file was truncated (can be null)IOException
- if something goes wrong reading the filepublic static void stringToFile(File file, String string) throws IOException
IOException
public static void stringToFile(String filename, String string) throws IOException
filename
- string
- IOException
public static long checksumCrc32(File file) throws FileNotFoundException, IOException
file
- the file to checksum, must not be nullFileNotFoundException
IOException
public static boolean deleteOlderFiles(File dir, int minCount, long minAge)
minCount
- Always keep at least this many files.minAge
- Always keep files younger than this age.public static boolean contains(File[] dirs, File file)
Both files must have been resolved using
File.getCanonicalFile()
to avoid symlink or path traversal
attacks.
public static boolean contains(File dir, File file)
Both files must have been resolved using
File.getCanonicalFile()
to avoid symlink or path traversal
attacks.
public static boolean deleteContentsAndDir(File dir)
public static boolean deleteContents(File dir)
public static boolean isValidExtFilename(String name)
public static String buildValidExtFilename(String name)
public static boolean isValidFatFilename(String name)
public static String buildValidFatFilename(String name)
public static String rewriteAfterRename(File beforeDir, File afterDir, String path)
public static String[] rewriteAfterRename(File beforeDir, File afterDir, String[] paths)
public static File rewriteAfterRename(File beforeDir, File afterDir, File file)
/before/foo/bar.txt
would become
/after/foo/bar.txt
.public static File buildUniqueFile(File parent, String mimeType, String displayName) throws FileNotFoundException
FileNotFoundException
public static File buildUniqueFile(File parent, String displayName) throws FileNotFoundException
FileNotFoundException
public static String[] splitFileName(String mimeType, String displayName)
public static File[] listFilesOrEmpty(File dir, FilenameFilter filter)