文件的基本操作


#import "AppDelegate.h"


    /*________________________1.创建文件____________________________*/
    /*
    //获取本地沙盒路径
    NSString *homePath = NSHomeDirectory();
    
    //设置文件路径
    NSString *filePath = [homePath stringByAppendingPathComponent:@"Documents/file.text"];
    
//    NSFileManager
//    NSFileManager *fileManager = [[NSFileManager alloc] init];  错误
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    //创建需要显示的内容
    NSString *string = @"啊污染物";
    //转换成NSData
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    
    //filePath:创建的文件的所放置的位置,这个方法是用来创建文件,不是文件夹
    BOOL success = [fileManager createFileAtPath:filePath contents:data attributes:nil];
    
    if (success) {
        NSLog(@"创建成功了");
    }else {
        NSLog(@"创建失败了");
    }
   */
    /*________________________2.创建文件夹____________________________*/
    /*
    NSString *fPath = [homePath stringByAppendingPathComponent:@"Documents/Demo"];
    
    NSError *error = nil;
    
    BOOL success2 = [fileManager createDirectoryAtPath:fPath
           withIntermediateDirectories:YES
                            attributes:nil
                                 error:&error];
    if (success2) {
        NSLog(@"文件夹创建成功");
        NSLog(@"%@",error);
    }else {
        NSLog(@"%@",error);
        
    }
     NSData *data = [NSData dataWithContentsOfFile:filePath];
     */
    
    /*________________________3.读取文件____________________________*/
    /*
    //本地沙盒路径
    NSString *homePath = NSHomeDirectory();
    //设置文件路径
    NSString *filePath = [homePath stringByAppendingPathComponent:@"Documents/file.text"];
    //方式一:
//    NSData *data = [NSData dataWithContentsOfFile:filePath];
//    NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//    NSLog(@"string:%@",string);

    //方式二:
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSData *data = [fileManager contentsAtPath:filePath];
    NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"string:%@",string);
     */
    
    /*________________________4.移动文件(剪切)____________________________*/
/*
    //本地沙盒路径
    NSString *homePath = NSHomeDirectory();
    //文件以前所在的路径
    NSString *filePath = [homePath stringByAppendingPathComponent:@"Documents/file.text"];
    
    //需要移动到的路径
    NSString *toPath = [homePath stringByAppendingPathComponent:@"Documents/Demo/file2.text"];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    NSError *error = nil;
    
    BOOL success = [fileManager moveItemAtPath:filePath toPath:toPath error:&error];
    
    if (success) {
        NSLog(@"移动成功");
    }else {
        NSLog(@"error:%@",error);
    }
    
    //注意:可以用这种方式修改文件的名字
 */

    /*________________________5.复制文件____________________________*/
    /*
    //本地沙盒路径
    NSString *homePath = NSHomeDirectory();
    //设置文件所在路径
    NSString *filePath = [homePath stringByAppendingPathComponent:@"Documents/Demo/file3.text"];
    NSString *copyPath = [homePath stringByAppendingPathComponent:@"Documents/file.text"];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    NSError *errror = nil;
    
    BOOL success = [fileManager copyItemAtPath:filePath toPath:copyPath error:&errror];
    
    if (success) {
        NSLog(@"拷贝成功");
    }else {
    
        NSLog(@"error:%@",errror);
        
    }
     */
    
    /*________________________6.删除文件____________________________*/
    /*
    
    //本地沙盒路径
    NSString *homePath = NSHomeDirectory();
    //设置文件所在路径
    NSString *filePath = [homePath stringByAppendingPathComponent:@"Documents/file.text"];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    //判断文件是否存在于相应的路径下
    
    if ([fileManager fileExistsAtPath:filePath]) {
        BOOL success = [fileManager removeItemAtPath:filePath error:NULL];
        if ( success) {
            NSLog(@"删除成功");
        }
    }
    //清除缓存
    */
    
    /*_____________________6.文件属性_________________________*/
    
    NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Demo/file3.text"];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSDictionary *attributes = [fileManager attributesOfItemAtPath:filePath error:nil];
//    NSLog(@"attributes:%@",attributes);
    //文件的大小,单位是字节
    NSNumber *fileSize = [attributes objectForKey:NSFileSize];

    long size = [fileSize longValue];
    NSLog(@"文件大小:%ld字节",size);
    
    
    
    return YES;
}


@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值