This tool class contains several basic file operations for Android Application Development. It is also my first blog to share with you what I wrote.
If you have any supplement or modification, you are welcome to provide valuable suggestions.
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileNotFoundException;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. InputStream;
Import java. io. OutputStream;
Import java. util. ArrayList;
Import java. util. List;
Import com. example. test. ApplicationContext;
Import com. example. test. utils. assist. MyFileFilter;
/*
* This class is used to perform basic operations on files, including copying, moving, creating, and deleting files.
*/
Public class FileUtils {
Public final static int DEFAULT_FILE_OPERATE_MODE = 0; // when creating a File, the File object is an absolute path. If a File exists, it overwrites
Public final static int IGNORE_NOT_RECREATE_MODE = 1; // when creating a File, the File object is the path of the relative storage device. If a File exists, it indicates it already exists and no operation is performed.
Public final static int IGNORE_AND_RECREATE_MODE = 2; // when creating a File, the File object is the path of the relative storage device. If a File exists, it indicates it already exists and no operation is performed.
Public final static int NOT_IGNORE_RECREATE_MODE = 3; // when creating a File, the File object is an absolute path. If a File exists, it overwrites
Private final static boolean DEFAULT_IGNORE_STYLE = false;
Private final static boolean DEFAULT_AUTO_CREATE_DIRECTORY = true; // whether to create a directory automatically if the directory does not exist
/**
* Create a File with the File object as the parameter
* @ Param file
* @ Param ignore
* If mode is equal to IGNORE_NOT_RECREATE_MODE or IGNORE_AND_RECREATE_MODE, the input file is a relative path. If mode is the other two modes, the input file is an absolute path.
* Added sdcard dirctory.
* @ Throws IOException
*/
Public static void createFile (File file, int mode) throws IOException
{
If (file = null | StringUtils. isEmpty (file. getAbsolutePath ())){
Return;
}
If (mode = IGNORE_NOT_RECREATE_MODE
| Mode = IGNORE_AND_RECREATE_MODE ){
File = new File (StorageUtils. getStorageFile (),
File. getAbsolutePath ());
}
If (mode = DEFAULT_FILE_OPERATE_MODE
| Mode = IGNORE_AND_RECREATE_MODE ){
DeleteFile (file );
}
File. createNewFile ();
}
/**
* Create a file with the String object as the parameter
* @ Param filePath
* @ Param mode
* @ Throws IOException
*/
Public static void createFile (String filePath, int mode) throws IOException
{
CreateFile (new File (filePath), mode );
}
/**
* Create a file by default
* @ Param filePath
* @ Throws IOException
*/
Public static void createFile (File filePath) throws IOException
{
CreateFile (filePath, DEFAULT_FILE_OPERATE_MODE );
}
/**
* @ Param filePath
* @ Throws IOException
*/
Public static void createFile (String filePath) throws IOException
{
CreateFile (new File (filePath ));
}
/**
* Create a folder
*
* @ Param folder
* @ Param mode
*/
Public static void createFolder (File folder, int mode)
{
If (folder = null | StringUtils. isEmpty (folder. getAbsolutePath ())){
Return;
}
If (! Folder. isDirectory ()){
Return;
}
If (mode = IGNORE_NOT_RECREATE_MODE
| Mode = IGNORE_AND_RECREATE_MODE ){
Folder = new File (StorageUtils. getStorageFile (),
Folder. getAbsolutePath ());
}
If (mode = DEFAULT_FILE_OPERATE_MODE
| Mode = IGNORE_AND_RECREATE_MODE ){
DeleteFolder (folder );
}
Folder. mkdirs ();
}
/**
* @ Param folder
* @ Param mode
*/
Public static void createFolder (String folder, int mode)
{
CreateFolder (new File (folder), mode );
}
/**
* @ Param folder
*/
Public static void createFolder (File folder)
{
CreateFolder (folder, DEFAULT_FILE_OPERATE_MODE );
}
/**
* @ Param folder
*/
Public static void createFolder (String folder)
{
CreateFolder (new File (folder ));
}
/**
* Delete an object
*
* @ Param file
*/
Public static void deleteFile (File file)
{
If (file = null | StringUtils. isEmpty (file. getAbsolutePath ())){
Return;
}
If (file. exists ()){
If (! File. isDirectory ()){
File. delete ();
}
}
}
/**
* @ Param filePath
*/
Public static void deleteFile (String filePath)
{
If (! StringUtils. isEmpty (filePath )){
DeleteFile (new File (filePath ));
}
}
/**
* Delete A directory
*
* @ Param folder
*/
Public static void deleteFolder (File folder)
{
If (folder = null | StringUtils. isEmpty (folder. getAbsolutePath ())){
Return;
}
If (folder. exists ()){
If (folder. isDirectory ()){
File [] files = folder. listFiles ();
If (files! = Null)
{
For (File file: files ){
DeleteFolder (file );
}
}
}
Else {
DeleteFile (folder );
}
}
}
/**
* @ Param folderPath
*/
Public static void deleteFolder (String folderPath)
{
If (! StringUtils. isEmpty (folderPath )){
DeleteFile (new File (folderPath ));
}
}
/**
* Search for objects ending with extensions in the specified directory
*
* @ Param end
* @ Return
*/
Public static List <File> getAllWithEnd (File file File, boolean ignore,
String... extensions)
{
If (StringUtils. isEmpty (file. getAbsolutePath ())){
Return null;
}
For (String extension: extensions ){
If (StringUtils. isEmpty (extension )){
Return null;
}
}
If (ignore ){
File = new File (StorageUtils. getStorageFile (),
File. getAbsolutePath ());
}
If ((! File. exists () & file. isDirectory ()){
Return null;
}
List <File> files = new ArrayList <File> ();
FileFilter (file, files, extensions );
Return files;
}
/**
* @ Param path
* @ Param extensions
* @ Param ignore
* @ Return
*/
Public static List <File> getAllWithEnd (String path, boolean ignore,
String... extensions)
{
Return getAllWithEnd (new File (path), ignore, extensions );
}
/**
* @ Param file
* @ Param extensions
* @ Return
*/
Public static List <File> getAllWithEnd (File file, String... extensions)
{
Return getAllWithEnd (file, DEFAULT_IGNORE_STYLE, extensions );
}
/**
* @ Param file
* @ Param extensions
* @ Return
*/
Public static List <File> getAllWithEnd (String file, String... extensions)
{
Return getAllWithEnd (new File (file), DEFAULT_IGNORE_STYLE, extensions );
}
/**
* Filter files
*
* @ Param file
* @ Param extensions
* @ Param files
*/
Public static void fileFilter (File file, List <File> files,
String... extensions)
{
If (! File. isDirectory ()){
Return;
}
File [] allFiles = file. listFiles ();
File [] allExtensionFiles = file. listFiles (new MyFileFilter (extensions ));
If (allExtensionFiles! = Null ){
For (File single: allExtensionFiles ){
Files. add (single );
}
}
If (allFiles! = Null ){
For (File single: allFiles ){
If (single. isDirectory ()){
FileFilter (single, files, extensions );
}
}
}
}
/**
* Copy the files in the Assets Directory to the specified location.
*
* @ Param strAssetsFilePath
* @ Param strDesFilePath
* @ Return
*/
Public boolean assetsCopyData (String strAssetsFilePath,
String strDesFilePath)
{
Boolean bIsSuc = true;
InputStream inputStream = null;
OutputStream outputStream = null;
File file = new File (strDesFilePath );
If (! File. exists ()){
Try {
File. createNewFile ();
Runtime.getruntime(cmd.exe c ("chmod 766" + file );
}
Catch (IOException e ){
BIsSuc = false;
}
}
Else {// exists
Return true;
}
Try {
InputStream = ApplicationContext. mContext. getAssets (). open (
StrAssetsFilePath );
OutputStream = new FileOutputStream (file );
Int nLen = 0;
Byte [] buff = new byte [1, 1024*1];
While (nLen = inputStream. read (buff)> 0 ){
OutputStream. write (buff, 0, nLen );
}
}
Catch (IOException e ){
BIsSuc = false;
}
Finally {
Try {
If (outputStream! = Null ){
OutputStream. close ();
}
If (inputStream! = Null ){
InputStream. close ();
}
}
Catch (IOException e ){
BIsSuc = false;
}
}
Return bIsSuc;
}
/**
* Copy an object
*
* @ Param src
* @ Param dst
* @ Return
* @ Throws IOException
*/
Public static boolean copyFile (File src, File dst) throws IOException
{
If ((! Src. exists () | src. isDirectory () | dst. isDirectory ()){
Return false;
}
If (! Dst. exists ()){
Dst. createNewFile ();
Return false;
}
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
InputStream = new FileInputStream (src );
OutputStream = new FileOutputStream (dst );
Int readLen = 0;
Byte [] buf = new byte [1, 1024];
While (readLen = inputStream. read (buf ))! =-1 ){
OutputStream. write (buf, 0, readLen );
}
OutputStream. flush ();
InputStream. close ();
OutputStream. close ();
Return true;
}
/**
* @ Param src
* @ Param dst
* @ Return
* @ Throws IOException
*/
Public static boolean copyFile (String src, String dst) throws IOException
{
Return copyFile (new File (src), new File (dst ));
}
/**
* Copy the entire directory
* @ Param srcDir
* @ Param destDir
* @ Param auto
* @ Return
* @ Throws IOException
*/
Public static boolean copyFolder (File srcDir, File destDir, boolean auto)
Throws IOException
{
If ((! SrcDir. exists ())){
Return false;
}
If (srcDir. isFile () | destDir. isFile ())
Return false;
If (! DestDir. exists ()){
If (auto ){
DestDir. mkdirs ();
}
Else {
Return false;
}
}
File [] srcFiles = srcDir. listFiles ();
Int len = srcFiles. length;
For (int I = 0; I <len; I ++ ){
If (srcFiles [I]. isFile ()){
File destFile = new File (destDir. getPath () + "//"
+ SrcFiles [I]. getName ());
CopyFile (srcFiles [I], destFile );
}
Else if (srcFiles [I]. isDirectory ()){
File theDestDir = new File (destDir. getPath () + "//"
+ SrcFiles [I]. getName ());
CopyFolder (srcFiles [I], theDestDir, auto );
}
}
Return true;
}
/**
* @ Param srcDir
* @ Param desDir
* @ Param auto
* @ Return
* @ Throws IOException
*/
Public static boolean copyFolder (String srcDir, String desDir, boolean auto)
Throws IOException
{
Return copyFolder (new File (srcDir), new File (desDir), auto );
}
/**
* @ Param srcDir
* @ Param desDir
* @ Return
* @ Throws IOException
*/
Public static boolean copyFolder (File srcDir, File desDir)
Throws IOException
{
Return copyFolder (srcDir, desDir, DEFAULT_AUTO_CREATE_DIRECTORY );
}
/**
* @ Param srcDir
* @ Param desDir
* @ Return
* @ Throws IOException
*/
Public static boolean copyFolder (String srcDir, String desDir)
Throws IOException
{
Return copyFolder (srcDir, desDir, DEFAULT_AUTO_CREATE_DIRECTORY );
}
/**
* Move a single file
* @ Param src
* @ Param dst
* @ Return
*/
Public static boolean moveFile (File src, File dst)
{
Boolean isCopy = false;
Try {
IsCopy = copyFile (src, dst );
}
Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
If (! IsCopy)
{
Return false;
}
DeleteFile (src );
Return true;
}
/**
* @ Param src
* @ Param dst
* @ Return
*/
Public static boolean moveFile (String src, String dst)
{
Return moveFile (new File (src), new File (dst ));
}
/**
* Move the entire directory
* @ Param srcDir
* @ Param destDir
* @ Param auto
* @ Return
*/
Public static boolean moveFolder (File srcDir, File destDir, boolean auto)
{
If (! SrcDir. isDirectory () |! DestDir. isDirectory ()){
Return false;
}
If (! SrcDir. exists ())
{
Return false;
}
If (! DestDir. exists ())
{
If (auto)
{
DestDir. mkdirs ();
}
Else {
Return false;
}
}
File [] srcDirFiles = srcDir. listFiles ();
Int len = srcDirFiles. length;
If (len <= 0)
{
SrcDir. delete ();
}
For (int I = 0; I <len; I ++ ){
If (srcDirFiles [I]. isFile ()){
File oneDestFile = new File (destDir. getPath () + "//"
+ SrcDirFiles [I]. getName ());
MoveFile (srcDirFiles [I], oneDestFile );
} Else if (srcDirFiles [I]. isDirectory ()){
File oneDestFile = new File (destDir. getPath () + "//"
+ SrcDirFiles [I]. getName ());
MoveFolder (srcDirFiles [I], oneDestFile, auto );
DeleteFolder (srcDirFiles [I]);
}
}
Return true;
}
/**
* @ Param src
* @ Param dst
* @ Param auto
* @ Return
*/
Public static boolean moveFolder (String src, String dst, boolean auto)
{
Return moveFolder (new File (src), new File (dst ));
}
/**
* @ Param src
* @ Param dst
* @ Return
*/
Public static boolean moveFolder (File src, File dst)
{
Return moveFolder (src, dst, DEFAULT_AUTO_CREATE_DIRECTORY );
}
/**
* @ Param src
* @ Param dst
* @ Return
*/
Public static boolean moveFolder (String src, String dst)
{
Return moveFolder (new File (src), new File (dst), DEFAULT_AUTO_CREATE_DIRECTORY );
}
/**
* Obtain the private file directory (/data/yourpackage/file)
*
* @ Return
*/
Public static File getPrivateDir ()
{
Return ApplicationContext. mContext. getFilesDir ();
}
}