//对于错误信息
NSError *error;
// 创建文件管理器
NSFileManager *fileMgr = [NSFileManagerdefaultManager];
//指向Documents目录
NSString *documentsDirectory= [NSHomeDirectory()stringByAppendingPathComponent:@"Documents"];
//创建一个目录
[[NSFileManager defaultManager] createDirectoryAtPath: [NSString stringWithFormat:@"%@/myFolder", NSHomeDirectory()] attributes:nil];
创建一个文件现在我们已经有了文件目录,我们就能使用这个路径在沙盒中创建一个新文件并编写一段代码:
// File we want to create in the documents directory
我们想要创建的文件将会出现在文件目录中
// Result is: /Documents/file1.txt 结果为:/Documents/file1.txt
NSString *filePath= [documentsDirectorystringByAppendingPathComponent:@"file1.txt"];
//需要写入的字符串
NSString *str= @"iPhoneDeveloper Tips\nhttp://iPhoneDevelopTips,com";
//写入文件[str writeToFile:filePath atomically:YESencoding:NSUTF8StringEncoding error:&error];
//显示文件目录的内容
NSLog(@"Documentsdirectory: %@",[fileMgr contentsOfDirectoryAtPath:documentsDirectoryerror:&error]);
NSFileManager *fileManager = [NSFileManager defaultManager];
//在这里获取应用程序Documents文件夹里的文件及文件夹列表
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir = [documentPaths objectAtIndex:0];
NSError *error = nil;
NSArray *fileList = [[NSArray alloc] init];
//fileList便是包含有该文件夹下所有文件的文件名及文件夹名的数组
fileList = [fileManager contentsOfDirectoryAtPath:documentDir error:&error];
NSMutableArray *dirArray = [[NSMutableArray alloc] init];
BOOL isDir = NO;
//在上面那段程序中获得的fileList中列出文件夹名
for (NSString *file in fileList)
{
NSString *path = [documentDir stringByAppendingPathComponent:file];
[fileManager fileExistsAtPath:path isDirectory:(&isDir)];
if (isDir)
{
[dirArray addObject:file];
}
isDir = NO;
}
NSLog(@"Every Thing in the dir:%@",fileList);
NSLog(@"All folders:%@",dirArray);
原博客地址:http://blog.csdn.net/iorchid/article/details/6752903