美文网首页
NSFileManager的使用

NSFileManager的使用

作者: 崔盛希 | 来源:发表于2018-11-16 11:37 被阅读6次
/*
         两种获取文件路径的方式
         */
        
        //绝对路径
        NSString*directPath=@"/Users/cuixi/desktop";  //目录路径,直接通过路径获取
        //通过函数获取路径
        NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES);
        NSString*deskPath=paths[0];
        //文件路径
        NSString*filePath1=[directPath stringByAppendingPathComponent:@"myfile.txt"];
        NSLog(@"direc==%@",directPath);
        NSString*filePath2=[deskPath stringByAppendingPathComponent:@"myfile.txt"];
        NSLog(@"desk==%@",deskPath);
        NSFileManager*manager=[NSFileManager defaultManager];
        
        NSLog(@"f1=%@,f2=%@",filePath1,filePath2);
        
        if ([manager fileExistsAtPath:filePath1]) {
            NSLog(@"myfile.txt exist in filePath1");
            
        }
        if([manager fileExistsAtPath:filePath2]){
            NSLog(@"myfile.txt exist in filePath2");
        }
        
        //赋值、移动、删除
        // 仅仅是移动
        if ([manager copyItemAtPath:@"/Users/cuixi/desktop/myfile.txt" toPath:@"/Users/cuixi/desktop/python学习/myfile.txt" error:nil]) {
            NSLog(@"移动文件success");
        }
        // 移动和改名,原来位置的文件不在了,在新的位置。
        if ([manager moveItemAtPath:@"/Users/cuixi/desktop/myfile.txt" toPath:@"/Users/cuixi/desktop/python学习/moveItem.txt" error:nil]) {
            NSLog(@"移动文件,修改文件名称success");
        }
        //删除文件
        if ([manager removeItemAtPath:@"/Users/cuixi/desktop/python学习/moveItem.txt" error:nil]&&[manager removeItemAtPath:@"/Users/cuixi/desktop/python学习/myfile.txt" error:nil]) {
            NSLog(@"删除文件success");
        }
        

相关文章

网友评论

      本文标题:NSFileManager的使用

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