您的位置:首页 > 其它

NSFileManager管理文件常用办法

2015-08-21 17:45 411 查看
// 4.1 NSFileManager管理文件常用办法

// 4.2创建文件

//创建NSFileManager对象

NSFileManager*fileManager = [NSFileManager defaultManager];

NSString*homePath1 = NSHomeDirectory();

NSLog(@"homePath1====%@",homePath1);

NSString*pathMg = [homePath1 stringByAppendingPathComponent:@"liRuYi2.text"];

//创建内容

NSString*text = @"媒介生而平等,但有的媒介比其它媒介更加平等,不是人们不读书了,而是文字阅读已经不再是唯一有价值、有营养的阅读形态了。";

//将字符串转换成NSData类型

NSData*date = [text dataUsingEncoding:NSUTF8StringEncoding];

//写入文件

BOOL success = [fileManager createFileAtPath:pathMg contents:date attributes:nil];

NSLog(@"%d",success);

// 4.3读取文件

// 4.3.1 创建NSFileManager对象

NSFileManager*fileManager2 = [NSFileManager defaultManager];

//4.3.2 根据路径读取文件

NSData*fileDate = [fileManager2 contentsAtPath:pathMg];

//4.3.3 将NSDate转换成NSString

NSString*content = [[NSString alloc] initWithData:fileDate encoding:NSUTF8StringEncoding];

NSLog(@"content=====%@",content);

// 4.4 剪切复制

NSString*homePath3 = NSHomeDirectory();

//源文件路径

NSString*srcPath = [homePath3 stringByAppendingPathComponent:@"liRuYi2.text"];

//目标路径

NSString*toPath = [homePath3 stringByAppendingPathComponent:@"Documents/file.text"];

//4.4.1剪切(移动)文件

//将src路径的文件剪切到toPath路径

BOOL isSuccess = [fileManager2 moveItemAtPath:srcPath toPath:toPath error:nil];

NSLog(@"%d",isSuccess);

//4.4.2 复制文件

//源文件路径

NSString*srcPath2 = [homePath3 stringByAppendingPathComponent:@"file.text"];

//目标路径

NSString*toPath2 = [homePath3 stringByAppendingPathComponent:@"Library/good.text"];

BOOL success2 = [fileManager2 copyItemAtPath:srcPath2 toPath:toPath2 error:nil];

NSLog(@"%d",success2);

// 4.5 删除文件

//判断文件是否存在

NSString*deletePath = [homePath3 stringByAppendingPathComponent:@"liRuYi2.text"];

BOOL isExist = [fileManager2 fileExistsAtPath:deletePath];

if (isExist) {

//删除文件

BOOL seccessDelete = [fileManager2 removeItemAtPath:deletePath error:nil];

if (seccessDelete) {

NSLog(@"remove success");

UIAlertView*alertView = [[UIAlertView alloc] initWithTitle:nil message:@"remove success" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];

[alertView show];

}

}

// 4.6获取文件大小

//获取文件的属性字典

NSDictionary*fileDic = [fileManager2 attributesOfItemAtPath:toPath2 error:nil];

//获取文件大小

NSNumber*fileSize = [fileDic objectForKey:NSFileSize];

NSLog(@"fileSize=======%@",fileSize);

// 5 写文件

// 数组 字典 字符串 NSData都是容纳数据的,他们都有一个writeToFile的方法

//5.1数组写入文件

NSString *str1 = @"明月几时有";

NSString *str2 = @"把酒问青天";

NSArray *array = [NSArray arrayWithObjects:str1,str2, nil];

NSString*path10 = [homePath stringByAppendingPathComponent:@"hehe.text"];

//将数组中的数据写入文件

[array writeToFile:path10 atomically:YES];

NSString*path11 = [homePath stringByAppendingPathComponent:@"hehe.plist"];

[array writeToFile:path11 atomically:YES];

// 5.2 属性列表文件

// 数组只能写入NSNumber NSString NSDate NSData NSArray NSDictionary

// 数组,字典写入的文件叫属性列表(plist),可以用xcode打开编辑

// 6 读文件

// 数组 字典 字符串 NSData 同样的可以将文件数据读入程序

// 6.1数组读文件

//通过alloc创建,并读入文件数据

NSArray*array1 = [[NSArray alloc] initWithContentsOfFile:path10];

NSLog(@"array1===========%@",array1);

//通过类方法创建,并读入文件数据

NSArray*arry2 = [NSArray arrayWithContentsOfFile:path11];

NSLog(@"arry2 ===== %@",arry2);

// 补充 创建文件夹

//[[NSSearchPathForDirectoriesInDomains(NSDemoApplicationDirectory, NSUserDomainMask, YES)是一个数组,作用是获取路径

NSString*createDir = [[NSSearchPathForDirectoriesInDomains(NSDemoApplicationDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"test/test/test"];

//创建文件夹 createIntermediates该参数bool类型 是否创建文件夹路径中没有的文件夹目录

[fileManager createDirectoryAtPath:createDir withIntermediateDirectories:YES attributes:nil error:nil];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: