美文网首页
OC:文件的读写、复制、移动和删除

OC:文件的读写、复制、移动和删除

作者: 春暖花已开 | 来源:发表于2018-11-24 10:17 被阅读13次

    NSDocumentDirectory与NSDocumentationDirectory 的区别:

    NSDocumentDirectory是指程序中对应的Documents路径,而NSDocumentionDirectory对应于程序中的Library/Documentation路径,这个路径是没有读写权限的,所以看不到文件生成。

    获取写入路径:

    - (NSString *)documentsDirectory {
        
        NSString *documentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
        return documentsDirectory;
    }
    

    写入一
    - (IBAction)write:(id)sender {
        
        NSString *txt = @"This is a book";
        NSString *filePath = [[self documentsDirectory] stringByAppendingPathComponent:@"MZString.txt"];
        BOOL isSuccess = [txt writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
        if (isSuccess) {
            NSLog(@"Write Succeed");
        } else {
            NSLog(@"Write Failed");
        }
        
        // 追加写入(不覆盖原先数据)
        NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:filePath];
        [handle seekToEndOfFile]; // 移动到文件的末尾
        [handle writeData:[@"以下为追加写入😄" dataUsingEncoding:NSUTF8StringEncoding]]; // 在文件末尾写入数据
        [handle closeFile]; // 关闭
        
        NSLog(@"%@", filePath);
        
        // 获取文件总长度
        NSFileManager *manager = [NSFileManager defaultManager];
        NSDictionary *dict = [manager attributesOfItemAtPath:filePath error:nil]; // 返回给定路径item的属性
        long long length = [[dict objectForKey:NSFileSize] longLongValue];
        NSLog(@"%lld", length);
    }
    
    // 沙盒路径
    - (NSString *)documentsDirectory {
        
        NSString *documentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
        return documentsDirectory;
    }
    

    写入二

    - (IBAction)write:(id)sender {
        
        NSLog(@"%@", [self folderPath]);
        
        // 检查缓存是否存在
        NSString *filePath = [[self folderPath] stringByAppendingPathComponent:@"File"];
        id contents = [self getContentsAtPath:filePath];
        if (contents) {
            NSLog(@"😄缓存存在:%@", contents);
            return;
        }
        
        NSString *txt = @"{\"data\":\"This is a book.\"}";
        
        [[NSFileManager defaultManager] createDirectoryAtPath:[self folderPath] withIntermediateDirectories:YES attributes:nil error:nil];
        
        // 写入的速度比较慢
    //    [[NSFileManager defaultManager] createFileAtPath:filePath contents:[txt dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
        [txt writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    }
    
    // 沙盒路径
    - (NSString *)documentsDirectory {
        
        NSString *documentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
        return documentsDirectory;
    }
    
    // 存储路径
    - (NSString *)folderPath {
        
        return [[self documentsDirectory] stringByAppendingPathComponent:@"MyCache"];
    }
    
    // 检查缓存是否存在
    - (id)getContentsAtPath:(NSString *)path {
        
        // 指定路径不存在
        if ([path isKindOfClass:[NSNull class]] || !path || path.length == 0) {
            return nil;
        }
        
        NSData *data = [NSData dataWithContentsOfFile:path];
        if (data) {
            return [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        }
        return nil;
    }
    

    移动

    - (IBAction)move:(id)sender {
        
        NSString *filePath = [[self folderPath] stringByAppendingPathComponent:@"File"];
        NSString *contFilePath = [[self folderPath] stringByAppendingPathComponent:@"newFile"];
        
        NSFileManager *manager = [NSFileManager defaultManager];
        [manager moveItemAtPath:filePath toPath:contFilePath error:nil];
    }
    

    复制

    - (IBAction)copy:(id)sender {
        
        NSString *filePath = [[self folderPath] stringByAppendingPathComponent:@"newFile"];
        NSString *CopyFilePath = [[self folderPath] stringByAppendingPathComponent:@"CopedFile"];
        
        NSFileManager *manager = [NSFileManager defaultManager];
        [manager copyItemAtPath:filePath toPath:CopyFilePath error:nil];
    }
    

    删除

    - (IBAction)delete:(id)sender {
        
        NSString *file = [[self folderPath] stringByAppendingPathComponent:@"CopedFile"];
        
        NSFileManager *manager = [NSFileManager defaultManager];
        [manager removeItemAtPath:file error:nil];
    }
    

    相关文章

      网友评论

          本文标题:OC:文件的读写、复制、移动和删除

          本文链接:https://www.haomeiwen.com/subject/wsueqqtx.html