TXFileOperation
- iOS file (folder) can quickly and simply add, delete, modify, check and write. It can quickly and accurately calculate the folder size and file size series file operations
TXFileOperation index
- Sandbox catalog related
- traverse folder
- Get file properties
- Create file (folder)
- Delete file (folder)
- Copy file (folder)
- Move file (folder)
- Get file name from URL
- Judge whether the file (folder) exists
- Get file (folder) size
- Write file contents
cocoapods integration
- pod 'TXFileOperation', '~> 1.0'
code snippet
#pragma mark - sandbox directory related
///Home directory path of sandbox
+ (NSString *)homeDir;
///Directory path of Documents in sandbox
+ (NSString *)documentsDir;
///Directory path of Library in sandbox
+ (NSString *)libraryDir;
///Directory path of Library / Preferences in sandbox
+ (NSString *)preferencesDir;
///Directory path of Library / Caches in sandbox
+ (NSString *)cachesDir;
///Directory path of tmp in sandbox
+ (NSString *)tmpDir;
#pragma mark - traversal folder
/**
File traversal
@param path Absolute path to directory
@param deep Deep traversal or not
1. Shallow traversal: returns all files and folders in the current directory
2. Deep traversal: returns all files and folders in the current directory and subdirectories
@return Traversal result array
*/
+ (NSArray *)listFilesInDirectoryAtPath:(NSString *)path deep:(BOOL)deep;
///Traverse sandbox home directory
+ (NSArray *)listFilesInHomeDirectoryByDeep:(BOOL)deep;
///Traverse Documents directory
+ (NSArray *)listFilesInDocumentDirectoryByDeep:(BOOL)deep;
///Traverse Library Directory
+ (NSArray *)listFilesInLibraryDirectoryByDeep:(BOOL)deep;
///Traverse cache directory
+ (NSArray *)listFilesInCachesDirectoryByDeep:(BOOL)deep;
///Traverse tmp directory
+ (NSArray *)listFilesInTmpDirectoryByDeep:(BOOL)deep;
#pragma mark - write file contents
///Write file contents
+ (BOOL)writeFileAtPath:(NSString *)path content:(NSObject *)content;
///Write file contents (error message error)
+ (BOOL)writeFileAtPath:(NSString *)path content:(NSObject *)content error:(NSError **)error;
Usage method
- #import <TXFileOperation.h>
//Get documents path
NSString * documentsDirPath=[TXFileOperation documentsDir];
//Folder name
NSString * folderName=@"test";
//Splicing folder path
NSString * folderPath=[NSString stringWithFormat:@"%@/%@",documentsDirPath,folderName];
//create folder
if ([TXFileOperation createDirectoryAtPath:folderPath]) {
NSLog(@"Folder created successfully");
NSLog(@"Folder path:%@",folderPath);
}else{
NSLog(@"Folder creation failed");
}
//File name
NSString * fileName=@"text.txt";
//Splicing file path
NSString * filePath=[NSString stringWithFormat:@"%@/%@",folderPath,fileName];
//create a file
if ([TXFileOperation createFileAtPath:filePath]) {
NSLog(@"File created successfully");
NSLog(@"File path:%@",filePath);
}else{
NSLog(@"File creation failed");
}
//Write content to file
if ([TXFileOperation writeFileAtPath:filePath content:self.textView.text]) {
NSLog(@"File content written successfully");
NSLog(@"Write contents:%@",self.textView.text);
}else{
NSLog(@"File content write failed");
}
Check the source code of the project, please click here!