首页 > 代码库 > 文件管理NSFileManager

文件管理NSFileManager

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3e1e81 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000; min-height: 21.0px } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #6122ae } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1e9421 } p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c81b13 } p.p7 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #294c50 } p.p8 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC"; color: #1e9421 } p.p9 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #703daa } span.s1 { } span.s2 { color: #c42275 } span.s3 { color: #000000 } span.s4 { color: #c81b13 } span.s5 { color: #6122ae } span.s6 { color: #3e1e81 } span.s7 { font: 18.0px "PingFang SC" } span.s8 { color: #703daa } span.s9 { color: #1e9421 } span.s10 { font: 18.0px Menlo } span.s11 { font: 18.0px Menlo; color: #000000 }</style>

//NSFileManager

 

 

- (void)viewDidLoad {

    [super viewDidLoad];

 

    

    NSLog(@"%@",NSHomeDirectory());

    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

    NSString *filePath = [docPath stringByAppendingPathComponent:@"firstFM"];

    

    NSFileManager *fm = [NSFileManager defaultManager];

    //.创建

    //1.创建文件夹

    if (![fm fileExistsAtPath:filePath]) {

        NSError *error = nil;

        BOOL isSuc = [fm createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:&error];

        if (!isSuc) {

            NSLog(@"fail:%@",error.localizedDescription);

        }

    }else{

        NSLog(@"file existed");

    }

    //2.创建文件

    NSString *txtPath = [filePath stringByAppendingPathComponent:@"firstTxt.txt"];

    NSString *test    = @"hello world";

    NSData  *writeData= http://www.mamicode.com/[test dataUsingEncoding:NSUTF8StringEncoding];

    

    if (![fm fileExistsAtPath:txtPath]) {

        BOOL isSuc = [fm createFileAtPath:txtPath contents:writeData attributes:nil];

        if (!isSuc) {

            NSLog(@"fail");

        }

    }else{

        NSLog(@"txt existed");

    }

   

    //1.

//    [self scanDirectoryOrFile:filePath];

    //2.

    NSString *sourcePath = txtPath;

    NSString *newPath = [docPath stringByAppendingPathComponent:@"firstTxt.txt"];

    

//    [self moveDiectoryOrFile:sourcePath toNewPath:newPath];

//    [self deleteDirectoryOrFile:[docPath stringByAppendingPathComponent:@"rrr"]];

    

    [self attributeForDirectoryOrFile:newPath];

  

}

 //.操作

//1.遍历文件夹(目录)

- (void)scanDirectoryOrFile:(NSString *)path{

    NSFileManager *fm = [NSFileManager defaultManager];

    //浅层遍历(只包含当前路径的子目录)

    if ([fm fileExistsAtPath:path]) {

        //数组包含的是文件夹的名字

        NSArray *arr1 = [fm contentsOfDirectoryAtPath:path error:nil];

        NSLog(@"%ld--%@",arr1.count,arr1.firstObject);

 

    }

    //深层遍历(包含路径下所有子文件)

//    if ([fm fileExistsAtPath:path]) {

//        NSArray *arrAll = [fm subpathsOfDirectoryAtPath:path error:nil];

//    }

    

}

 

//2.拷贝

- (void)copyDirectoryOrFile:(NSString *)sourcePath toNewPath:(NSString *)newPath{

    NSFileManager *fm = [NSFileManager defaultManager];

    NSError *error = nil;

    BOOL isSuc = [fm copyItemAtPath:sourcePath toPath:newPath error:&error];

    if (isSuc) {

        NSLog(@"success");

    }else{

        NSLog(@"%@",error.localizedDescription);

    }

    

    

}

//3.移动(剪切)

- (void)moveDiectoryOrFile:(NSString *)sourcePath toNewPath:(NSString *)newPath{

    NSFileManager *fm = [NSFileManager defaultManager];

    NSError *error = nil;

    BOOL isSuc = [fm moveItemAtPath:sourcePath toPath:newPath error:&error];

    if (isSuc) {

        NSLog(@"success");

    }else{

        NSLog(@"%@",error.localizedDescription);

    }

    

}

 

//4.删除

- (void)deleteDirectoryOrFile:(NSString *)path{

    NSFileManager *fm = [NSFileManager defaultManager];

    NSError *error = nil;

    BOOL isSuc = [fm removeItemAtPath:path error:nil];

    if (isSuc) {

        NSLog(@"success");

    }else{

        NSLog(@"%@",error.localizedDescription);

    }

    

    

    

}

//.获取文件属性

- (void)attributeForDirectoryOrFile:(NSString *)path{

    NSFileManager *fm = [NSFileManager defaultManager];

    NSError *error = nil;

    NSDictionary *dic = [fm attributesOfItemAtPath:path error:&error];

    

    NSLog(@"%@",dic);

    

}

文件管理NSFileManager