iOS文件管理

文件管理器

1、判断文件是否存在

//文件管理单例,负责iOS沙盒中文件的管理
NSFileManager*  fileManager = [NSFileManager defaultManager];

//判断文件或者文件在在某个路径是否存在,并且它是不是文件夹
BOOL isDir = NO;
//要检查的路径:Path。传入是否是文件夹的bool变量地址    
BOOL existed = [fileManagerr fileExistsAtPath:Path isDirectory:&isDir];

2、获取文件或者文件夹的属性

NSDictionary* dic = [fileManger attributesOfItemAtPath:Path error:&error];

//返回的字典中可以用的属性如下:
FOUNDATION_EXPORT NSString * const NSFileType;
FOUNDATION_EXPORT NSString * const NSFileTypeDirectory;
FOUNDATION_EXPORT NSString * const NSFileTypeRegular;
FOUNDATION_EXPORT NSString * const NSFileTypeSymbolicLink;
FOUNDATION_EXPORT NSString * const NSFileTypeSocket;
FOUNDATION_EXPORT NSString * const NSFileTypeCharacterSpecial;
FOUNDATION_EXPORT NSString * const NSFileTypeBlockSpecial;
FOUNDATION_EXPORT NSString * const NSFileTypeUnknown;
FOUNDATION_EXPORT NSString * const NSFileSize;
FOUNDATION_EXPORT NSString * const NSFileModificationDate;
FOUNDATION_EXPORT NSString * const NSFileReferenceCount;
FOUNDATION_EXPORT NSString * const NSFileDeviceIdentifier;
FOUNDATION_EXPORT NSString * const NSFileOwnerAccountName;
FOUNDATION_EXPORT NSString * const NSFileGroupOwnerAccountName;
FOUNDATION_EXPORT NSString * const NSFilePosixPermissions;
FOUNDATION_EXPORT NSString * const NSFileSystemNumber;
FOUNDATION_EXPORT NSString * const NSFileSystemFileNumber;
FOUNDATION_EXPORT NSString * const NSFileExtensionHidden;
FOUNDATION_EXPORT NSString * const NSFileHFSCreatorCode;
FOUNDATION_EXPORT NSString * const NSFileHFSTypeCode;
FOUNDATION_EXPORT NSString * const NSFileImmutable;
FOUNDATION_EXPORT NSString * const NSFileAppendOnly;
FOUNDATION_EXPORT NSString * const NSFileCreationDate;
FOUNDATION_EXPORT NSString * const NSFileOwnerAccountID;
FOUNDATION_EXPORT NSString * const NSFileGroupOwnerAccountID;
FOUNDATION_EXPORT NSString * const NSFileBusy;
FOUNDATION_EXPORT NSString * const NSFileProtectionKey NS_AVAILABLE_IOS(4_0);
FOUNDATION_EXPORT NSString * const NSFileProtectionNone NS_AVAILABLE_IOS(4_0);
FOUNDATION_EXPORT NSString * const NSFileProtectionComplete NS_AVAILABLE_IOS(4_0);
FOUNDATION_EXPORT NSString * const NSFileProtectionCompleteUnlessOpen NS_AVAILABLE_IOS(5_0);
FOUNDATION_EXPORT NSString * const NSFileProtectionCompleteUntilFirstUserAuthentication NS_AVAILABLE_IOS(5_0);

//可以用点语法获取的属性如下:

- (unsigned long long)fileSize;
- (NSDate *)fileModificationDate;
- (NSString *)fileType;
- (NSUInteger)filePosixPermissions;
- (NSString *)fileOwnerAccountName;
- (NSString *)fileGroupOwnerAccountName;
- (NSInteger)fileSystemNumber;
- (NSUInteger)fileSystemFileNumber;
- (BOOL)fileExtensionHidden;
- (OSType)fileHFSCreatorCode;
- (OSType)fileHFSTypeCode;
- (BOOL)fileIsImmutable;
- (BOOL)fileIsAppendOnly;
- (NSDate *)fileCreationDate;
- (NSNumber *)fileOwnerAccountID;
- (NSNumber *)fileGroupOwnerAccountID;

3、获取文件夹中所有文件

  • 浅度遍历
//浅度遍历:返回当前目录下,所有的一级目录的文件夹名和文件名        
NSString *path=@"/Users/qingmai/Desktop/file";
NSArray *arr1=[fileManager contentsOfDirectoryAtPath:path error:&error];
NSLog(@"%@",arr1);
  • 深遍历
//深度遍历:返回当前目录下所有的子文件夹的名和所有的文件名(注意:二级目录以下,返回的时候是相对路径)
NSArray *arr2=[fileManager subpathsOfDirectoryAtPath:path error:&error];
NSLog(@"%@",arr2);

4、创建文件


//创建文件,contents:NSData类型,attributes:设置属性
[fileManager createFileAtPath:path contents:nil attributes:nil];

//创建文件夹
[fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];

5、文件或目录的拷贝

  • 文件或目录的拷贝
//文件/目录的拷贝
//将file文件夹里的file2文件夹拷贝到file1里
//注意事项:1.不能有空格;2.目标路径里必须包含要拷贝或者移动的文件的名字
NSString *fromPath=@"/Users/qingmai/Desktop/file/file2";
NSString *toPath=@"/Users/qingmai/Desktop/file/file1/file2";
[fileManager copyItemAtPath:fromPath toPath:toPath error:&error];
  • 文件或目录的移动
//文件/目录的移动
//将file文件夹里的wen文件移动到file1文件夹里
NSString *fromPath1=@"/Users/qingmai/Desktop/file/wen.rtf";
NSString *toPath1=@"/Users/qingmai/Desktop/file/file1/wen.rtf";
[fileManager moveItemAtPath:fromPath1 toPath:toPath1 error:nil];
  • 删除文件或目录
//删除文件/目录
[fileManager removeItemAtPath:toPath1 error:nil];

6、文件的操作

//打开文件,读取文件内容,并把内容复制到另外一个文件中
#import 

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSFileHandle *inFile, *outFile;
        NSData *buffer;
        //打开读取文件
        inFile = [NSFileHandle fileHandleForReadingAtPath:@"testfile.txt"];

        if (!inFile) {
            NSLog(@"Open the testfile for reading failed");
            return 1;
        }else{
            NSLog(@"Open the testfile for reading successful");
        }
        //判断文件是否已经存在,如果没有就创建
        if (![[NSFileManager defaultManager] fileExistsAtPath:@"testfile2.txt"]) {
            NSLog(@"testfile2.txt is not existed, and creat it.");
            [[NSFileManager defaultManager] createFileAtPath:@"testfile2.txt" contents:nil attributes:nil];
        }else{
            NSLog(@"testfile2.txt is existed.");
        }

        //打开文件写入
        outFile = [NSFileHandle fileHandleForWritingAtPath:@"testfile2.txt"];
        if (outFile) {
            NSLog(@"Open of testout for writing successful");
        }else{
            NSLog(@"Open of testout for writing failed");
            return 2;
        }
        //截断输出文件
        [outFile truncateFileAtOffset:0];
        //从inFile读取数据到缓存中
        buffer = [inFile readDataToEndOfFile];
        //从缓存中读取数据,写入outFile
        [outFile writeData:buffer];
        //关闭两个文件
        [inFile closeFile];
        [outFile closeFile];
        //验证文件内容
        NSLog(@"%@", [NSString stringWithContentsOfFile:@"testfile2.txt" encoding:NSUTF8StringEncoding error:NULL]);

    }
    return 0;
}

你可能感兴趣的:(iOS文件管理)