Objective-C 学习笔记11 文件使用

这个类的方法具有如下功能:

  • 创建 一个新文件
  • 从现有文件中读取数据
  • 将数据写入文件中
  • 重新命名文件
  • 删除文件
  • 测试文件是否存在
  • 确定文件的大小及其他属性
  • 复制文件
  • 测试两个文件的内容是否相同

NSFileManager 管理文件和目录

removeItemAtPath : error:

NSFileManager *fm;
fm=[NSFileManager defaultManager];
if([fm removeItemAtPath:@"todo" error:NULL]==NO){
    NSLog(@"CAN NOT REMOVE TODO");
}
if([fm removeItemAtPath:@"lossay.archiver" error:NULL]==NO){
    NSLog(@"CAN NOT REMOVE TODO");
}
else{
    NSLog(@"REMOVE LOSSAY OK");
}

 判断是否存在 

fileExistsAtPath 

if([fm fileExistsAtPath:@"lossay.archiver"]==NO){
    NSLog(@"THIS PATH NOT EXIST!");
}

文件拷贝copyItemAtPath: toPath: error: 

if([fm copyItemAtPath:@"filename" toPath:@"newfile" error:NULL]==NO){
    NSLog(@"FILE COPY FAILED");
}
    

文件比较 contentsEqualAtPath:andPath:

if([fm contentsEqualAtPath:@"a" andPath:@"b"]==NO){
    NSLog(@"not equal");
}

 文件重命名

if([fm moveItemAtPath:@"a" toPath:@"b" error:NULL]==NO){
    NSLog(@"move path eror");
}

文件是否可读 isReadableFileAtPath

if([fm isReadableFileAtPath:@"a"]==NO){
    NSLog(@"文件不存在或不可读!");
}

文件是否可写 isWritableFileAtPath

if([fm isWritableFileAtPath:@"a"]==NO){
    NSLog(@"文件不存在或不可写!");
}

 

 

posted on 2013-01-26 11:56  ios开发达人  阅读(232)  评论(0)    收藏  举报