ios文件管理

#import <Foundation/Foundation.h>

#define PATH1 @"/Users/aoyolo5/Desktop/文件管理"

#define PATH2 @"/Users/aoyolo5/Desktop/文件管理/name.txt"

#define PATH3 @"/Users/aoyolo5/Desktop/array.plist"

 

void fileManager()

{

    //文件管理

    //1.创建文件管理单例

    NSFileManager *fm = [NSFileManager defaultManager];

    //2.查看目录

    //浅度遍历

    NSArray *array = [fm contentsOfDirectoryAtPath:PATH1 error:nil];

    //深度遍历

    array = [fm subpathsOfDirectoryAtPath:PATH1 error:nil];

    NSLog(@"%@",array);

    //3.创建路径

    NSString *newPath = [PATH1 stringByAppendingString:@"/name.txt"];

    //        [fm createDirectoryAtPath:newPath withIntermediateDirectories:YES attributes:nil error:nil];

    //4.创建文件

    //newFile = /Users/aoyolo5/Desktop/文件管理/n/f/cname.txt

    NSString *newFile = [newPath stringByAppendingString:@"/name.txt"];

    //        [fm createFileAtPath:newFile contents:nil attributes:nil];

    //5.写文件

    NSArray *arr1 = @[@"1", @"2", @"3"];

    //        [arr1 writeToFile:newFile atomically:YES];

    //        NSArray *arr2 = [[NSArray alloc]initWithContentsOfFile:newFile];

    //        NSLog(@"%@", arr2);

    NSError *error = nil;

    //移动和复制操作需要注意的地方:目标文件的最后一个路径字符串要不存在(不能重名),这个字符串将作为被移动(被复制)的文件的新名字

    NSString *toPath = [PATH1 stringByAppendingString:@"/a/c/file1.txt"];

    //        [fm moveItemAtPath:newFile toPath:toPath error:&error];

    if (error == nil) {

        NSLog(@"移动成功");

    }else

    {

        NSLog(@"%@", error);

    }

    NSString *copyToPath = [PATH1 stringByAppendingString:@"/a/c/file2.txt"];

    error = nil;

    //        [fm copyItemAtPath:newFile toPath:copyToPath error:&error];

    if (error == nil) {

        NSLog(@"复制成功");

    }else

    {

        NSLog(@"%@", error);

    }

    

    //        [fm removeItemAtPath:newPath error:nil];

    NSDictionary *infoDic = [fm attributesOfItemAtPath:newPath error:nil];

    NSLog(@"%@",infoDic);

}

 

//文件数据

void reading()

{

    //1.以只读方式创建文件句柄

    NSFileHandle *fh = [NSFileHandle fileHandleForReadingAtPath:PATH2];

    //2.读取文件内容

    //[fh readDataOfLength:<#(NSUInteger)#>];

    NSData *data = [fh readDataToEndOfFile];

    NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

    NSLog(@"%@", data);

    NSLog(@"%@", str);

}

 

void writing()

{

    NSFileHandle *fh = [NSFileHandle fileHandleForWritingAtPath:PATH2];

    NSData *data = [@"abd" dataUsingEncoding:NSUTF8StringEncoding];

    //    [fh writeData:data];//替换

    //    [fh truncateFileAtOffset:0];//清空

    //    [fh writeData:data];

    //    [fh seekToEndOfFile];

    //    [fh writeData:data];

    //将句柄移动到2的位置

    [fh seekToFileOffset:2];

    [fh writeData:data];

 

}

 

void fileHandle()

{

    //NSSArray NSDictionry

    NSArray *array = [[NSArray alloc]initWithContentsOfFile:PATH3];

    NSDictionary *dic = array[5];

    NSLog(@"%@",[dic objectForKey:@"k2"]);

}

 

int main(int argc, const char * argv[]) {

    @autoreleasepool {

        fileHandle();

    }

    return 0;

}

posted @ 2015-08-19 20:15  ios-C  阅读(228)  评论(0编辑  收藏  举报