[IOS开发教程] NSfileManager的使用方法

总结了一下NSFileManager的一些常用方法,


NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory ,NSUserDomainMask,YES);

NSString *documentDirectory = [paths objectAtIndex:];http://www.kmnk03.com/hxpfk/qcd/392.html

NSLog(@"%@", documentDirectory);//路径查看

//1.创建文件夹

NSFileManager *fileManager = [NSFileManagerdefaultManager];

NSString *testDirectory = [documentDirectoryhttp://www.kmnk03.com/hxpfk/qcd/393.html stringByAppendingPathComponent:@"testbug1"];//创建文件夹

//创建目录

[fileManager http://www.kmnk03.com/hxpfk/qcd/394.htmlcreateDirectoryAtPath:testDirectorywithIntermediateDirectories:YESattributes:nilerror:nil];

//2.创建文件

NSString *test11 = [http://www.kmnk03.com/hxpfk/qcd/395.htmltestDirectory stringByAppendingPathComponent:@"test111.txt"];

NSString *test22 = [testDirectory http://www.kmnk03.com/hxpfk/qcd/396.htmlstringByAppendingPathComponent:@"test222.txt"];

NSString *string = @"写入内容1";

[fileManager createFileAtPath:test11contents:[string dataUsingEncoding:NSUTF8StringEncoding]attributes:nil];

[fileManager createFileAtPath:test22contents:[string dataUsingEncoding:NSUTF8StringEncoding]attributes:nil];

//3.查看Document里所有文件

NSArray *files = [fileManagerhttp://www.kmnk03.com/hxpfk/qcd/397.html subpathsAtPath:documentDirectory];

//4.查看目录里所有子文件

NSArray *subfiles = [http://www.kmnk03.com/hxpfk/qcd/398.htmlfileManager subpathsAtPath:testDirectory];

NSLog(@"%@", subfiles);

NSLog(@"%@", files);

//5.更改到待操作的到目录下

[fileManager changeCurrentDirectoryPath:[documentDirectorystringByExpandingTildeInPath]];

//创建文件filenamehttp://www.kmnk03.com/hxpfk/qcd/399.html文件名,contents文件的内容

NSString *fileName =@"testfilemanager.txt";

NSArray *array = [[NSArrayalloc]initWithObjects:@"hello world",@"hello World", nil];


[fileManagercreateFileAtPath:fileName contents:array attributes:nil];

//6.删除文件

[fileManagerremoveItemAtPath:fileName error:nil];

//7.复制文件后者给前者

NSError *error;

[fileManagercopyItemAtPath:test11 toPath:test22 error:&error];

//8.移动文件

NSString *test33 = [documentDirectory stringByAppendingPathComponent:@"test33.txt"];

if ([fileManager mhttp://www.kmnk03.com/hxpfk/qcd/400.htmloveItemAtPath:test11toPath:test33 error:&error] !=YES) {

NSLog(@"111%@", [errorlocalizedDescription]);

}

//9.文件是否存在

if ([[NSFileManagerdefaultManaghttp://www.kmnk03.com/hxpfk/qcd/401.htmler]fileExistsAtPath:@"/Users/dlios/Library/Application Support/iPhone Simulator/7.1/Applications/AC7B8788-BDCB-43AC-ABB6-8E9676CF94DC/Documents/testbug/test22333.txt"]) {

NSLog(@"yes");

}

else NSLog(@"no");

kmnk03.com

www.kmnk03.c0m

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值