您的位置:首页 > 其它

基础框架-文件操作-目录操作

2015-01-23 14:29 369 查看
下面表格中的措施  适用于NSFileManager这类物件,可以用来进行  目录操作:
措施名称描述
-(NSString *)currentDirectoryPath获取当前目录的路径
-(BOOL)changeCurrentDirectoryPath:path将当前目录的路径更换到path
-(BOOL)copyPath:from toPath:to handler:handler将目录from复制到to,to所代表的目录不能事先存在
-(BOOL)createDirectoryAtPath:path attributes:attr创建一个新目录
-(BOOL)fileExistsAtPath:path isDirectory:(BOOL *)flag检查path是否是目录
-(NSArray *)directoryContentsAtPath:path将目录内容列举出来
-(NSDirectoryEnumerator *)enumeratorAtPath:path枚举目录内容
-(BOOL)removeFileAtPath:path handler:handler删除空目录
-(BOOL)movePath:from toPath:to handler:handler重命名或者移动目录,to代表的目录不能事先存在。
下面的程序  示范了  如何进行  基本的目录操作:

[cpp] view
plaincopy

#import <Foundation/NSObject.h>  

#import <Foundation/NSString.h>  

#import <Foundation/NSFileManager.h>  

#import <Foundation/NSAutoreleasePool.h>  

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

{  

    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];  

    NSString *directoryName=@"myDirectory";  

    NSString *path;  

    NSFileManager *myFileManager;  

    myFileManager=[NSFileManager defaultManager];  

    //获取当前目录,并且显示出来  

    path=[myFileManager currentDirectoryPath];  

    NSLog(@"当前目录路径为:%@",path);  

    //在当前目录当中创建一个新目录  

    if([myFileManager createDirectoryAtPath:directoryName attributes:nil]==NO)  

    {  

        NSLog(@"无法创建目录!");  

        return 1;  

    }  

    //将新建的目录重命名为newDirectory  

    if([myFileManager movePath:directoryName toPath:@"newDirectory" handler:nil]==NO)  

    {  

        NSLog(@"目录重命名失败!");  

        return 2;  

    }  

    //将当前目录由可执行文件所在目录更换到newDirectory这个目录  

    if([myFileManager changeCurrentDirectoryPath:@"newDirectory"]==NO)  

    {  

        NSLog(@"更换目录失败!");  

        return 3;  

    }  

    //将更换后的当前目录路径显示出来,以验证操作是否成功e  

    path=[myFileManager currentDirectoryPath];  

    NSLog(@"当前目录路径为:%@",path);  

    NSLog(@"所有操作成功完成。");  

    [pool drain];  

    return 0;  

}  

运行  这个程序后,可以得到  这样的结果:

2012-05-10 13:54:52.615 Sample[1256:707] 当前目录路径为:/Users/Zijin/Library/Developer/Xcode/DerivedData/Sample-bwtvtehgdvdkzgaqfkwvluuhrhso/Build/Products/Debug
2012-05-10 13:54:52.618 Sample[1256:707] 当前目录路径为:/Users/Zijin/Library/Developer/Xcode/DerivedData/Sample-bwtvtehgdvdkzgaqfkwvluuhrhso/Build/Products/Debug/newDirectory
2012-05-10 13:54:52.619 Sample[1256:707] 所有操作成功完成。
Program ended with exit code: 0

在可执行文件所在的目录当中,我们  可以看到  这样的情况:

(图片丢失。。。)

在这个程序当中

1path=[myFileManager currentDirectoryPath];
这行语句  对  myFileManager这个物件  采取了  currentDirectoryPath这项措施,将  当前目录的路径  作为结果  存储  在变量path当中。接着  将  当前目录的路径  显示出来。
然后

1

[myFileManager createDirectoryAtPath:directoryName attributes:nil]

这行语句  对  myFileManager这个物件  采取了  createDirectoryAtPath:attributes:这项措施,创建了  一个新的目录,而  目录的名称  则为  参数directoryName中设定的myDirectory。

接着的

1[myFileManager movePath:directoryName toPath:@"newDirectory" handler:nil]
这行语句  对  myFileManager这个物件  采取了  movePath:toPath:handler:这项措施,将  第一个参数代表的目录  移动  到当前的目录,并且  命名为  newDirectory。由于  移动的目标目录  就是  当前目录,所以  这行语句  就是  给  目录  重命名。
movePath:toPath:handler:这项措施  也可以将  整个目录(包括  其中的内容)从文件系统中的一个位置  移动 到另外一个位置。
最后的

1

[myFileManager changeCurrentDirectoryPath:@"newDirectory"]

这行语句  对myFileManager这个物件  采取了  changeCurrentDirectoryPath:这项措施,将  当前目录  由可执行文件所在的目录  更换为  参数中的newDirectory这个目录。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: