iOS基础一 NSFileManager

//NSFileManager *fm = [NSFileManager defaultManager];
       // NSString *dirName = @"testdir";
        //NSString *path = [fm currentDirectoryPath];
        //NSLog(@"Current directory path is %@",path);
        
        //create a new directory
        //if([fm createDirectoryAtPath:dirName withIntermediateDirectories:NO attributes:nil error:nil] == NO){
        //    NSLog(@"couldn't create directory");
        //    return 1;
        //}
        
        //rename the directory
        //if([fm moveItemAtPath:dirName toPath:@"newdir" error:nil] == NO){
        //    NSLog(@"directory rename failed");
        //    return 2;
        //}
        
        //change directory into the new directory
        //if([fm changeCurrentDirectoryPath:@"newdir"] == NO){
        //    NSLog(@"change directory failed!");
        //    return 3;
        //}
        //path = [fm currentDirectoryPath];
        //NSLog(@"%@",path);
        /*
        NSFileManager *fm = [NSFileManager defaultManager];
        NSDirectoryEnumerator *dirEnum;
        NSArray *dirArray;
        NSString *path = [fm currentDirectoryPath];
        
        //Enumerate the directory
        dirEnum = [fm enumeratorAtPath:path];
        NSLog(@"contents of %@",path);
        BOOL flag;
        while ((path = [dirEnum nextObject]) != nil) {
            NSLog(@"%@",path);
            [fm fileExistsAtPath:path isDirectory:&flag];
            if(flag == YES)
                [dirEnum skipDescendants];
        }
        
        //Another way to enumerate a directory
        dirArray = [fm contentsOfDirectoryAtPath:[fm currentDirectoryPath] error:nil];
        NSLog(@"contents using directoryContentsAtPath: ");
        for(path in dirArray)
            NSLog(@"%@",path);
         */
        
        NSString *fName = @"test.txt";
        NSFileManager *fm;
        NSString *path,*tempdir,*extension,*homedir,*fullpath;
        //NSString *upath = @"~";
        NSArray *components;
        fm = [NSFileManager defaultManager];
        
        //get the temporary working directory
        tempdir = NSTemporaryDirectory();
        NSLog(@"temporary directory is %@",tempdir);
        
        //extact the base directory from current directory
        path = [fm currentDirectoryPath];
        NSLog(@"base dir is %@",[path lastPathComponent]);
        
        //create a fullpath to the file fName in current directory
        fullpath = [path stringByAppendingPathComponent:fName];
        NSLog(@"fullpath to %@ is %@",fName,fullpath);
        
        //get the file name extension
        extension = [fullpath pathExtension];
        NSLog(@"extension for %@ is %@",fullpath,extension);
        
        //get the user's home directory
        homedir = NSHomeDirectory();
        NSLog(@"your home directory is %@",homedir);
        
        //divide a path into its components
        components = [homedir componentsSeparatedByString:@"/"];
        NSLog(@"%@",components);
        
        //standardize a path
        NSLog(@"%@ => %@",@"../",[@"../" stringByStandardizingPath]);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值