FileManager 文件管理器

1.获得沙盒根目录
NSString *homePath = NSHomeDirectory();
2.获取Documents目录路径
方法一:
NSString *documentsPath =[NSDocumentsDirectory() stringByAppendingPathComponent:@"Documents"];
方法二:
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [path objectAtIndex:0];
3.获取Library/Preferences目录路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [paths objectAnIndex:0];
4.获取Library/Caches路径
NSArray *paths = NSSearchPathForDirectoriesDomains(NSCacheDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [paths objectAtIndex:0];
5.获取tmp路径
NSString *tmpDirectory = NSTemporaryDirectory();
4.NSFileManager:用于执行一般的文件系统操作
读取文件、向文件中写入数据、文件删除、修改、移动、比较两个文件的内容、测试文件的存在性、读取/更改文件的属性

1.初始化文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
2.如果要监听文件处理的结果需要设置代理
NSFileManager *fileManager = [[NSFileManager alloc] init];
fileManager.delegate = self;
3.判断一个给定的路径是否为文件夹
BOOL createPathOK = YES;
[[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory: &createPathOK];
4.指向沙盒中的Documents文件夹
NSSString *documentDirectory = [NSHomeDirectory stringByAppendingPathComponent:@"Documents"];
5.在Documents文件夹下创建一个名为“myFolder”文件夹
[fileManager createDirectoryAtPath:[documentDirectory stringByAppendingPathComponent:@"myFolder"] withIntermediaDirecotries:YES attributes:nil error: nit];
注意文件夹是没有后缀的,文件是有后缀的
/user/SunJian/Documents/myFolder
/user/SunJian/Documents/myFolder/text.txt
6.创建一个文件
NSString *filePath = [documentDirectory stringByAppendingPathComponent:@"file.txt"];
7.向文件中写入数据
NSStirng *str = @"用来测试";
[str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
8.获文件夹下的文件目录
NSArray *content = [fileManager contentsOfDirectoryAtPath:documentDiretory error: &error];
9.移除文件
[fileManager removeItemAtPath:filePath error:&error];
10.路径下是否存在文件
BOOL isExist = [fileManager fileExistsAaPath:filePath];
11.从文件中获取数据
NSData *mydata = [fileManager contentsAtPath:path];
12.创建一个文件并写入数据
[fileManager createFileAtPath:path contents:myData attributes:dic];
13.string与data互转
NSString *str = @"SunJian";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//文件细粒度操作
NSFileManager *manager = [NSFileManageer defaultManager];
NSString *filePath = @"/Users/Sunjian/Desktop/Document/test.txt";//最简单的事就是编程
//以只读的方式打开文件
NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath];
NSData *data = [fileHandle readDataToEndOfFile];//完整的读取文件
//以可写的方式打开文件操作系统,这才是重要的,要知道其原理肯定能实现
[NSFileHandle fileHandlForWritingAtPath:newPath];
//写入文件
[fileHandle writeData:data];
//关闭文件
[fileHandle closeFile];
//定位到指定的位置,默认在文件开头
[fileHandle seekToFileOffset:12];
[fileHandle readDataToEndofFile];
component:所包含的内容
将iOS和Linux联系起来
1.当前文件所在的目录(PWD)
NSString *currentPath = [manager currentDirectoryPath];
2.创建目录(mkdir)
BOOL result = [manager createDirectorhAtPath:myPath withIntermediateDirectories:YES attributes:nil error:nil];
3.目录重命名(mv srcName to DirName)
BOOL result = [manager moveItemAtPath:oldPath toPath:newPath error:&error];
4.改变当前目录
[manager changeCurrentDirectoryPath:newPath];
6.遍历整个目录(ls)
方法1:
NSDirectoryEnumerator * diretocyEnumerator = [namager enumeratorAtPath:newPath];
while (path = [directoryEnumerator nextObject]){
   NSLog(@"%@",path);
}
方法2:
NSArray *paths = [manager contentsOfDirectoryAtPath:newPath error:nil];
NSObject *p;
for(p in paths){
  NSLog(@"%@",p);
}
8.删除目录(rm)
BOOL result = [manager removeItemAtPath:newPath error:nil];
9.文件操作, 判断文件是否存在(find、where)
[manager filtExistsAtPath:filePath isDirectory:NO];
10.文件是否可读(isReadable)
[manager isReadableAtPath:filePath];
11.判断两文件呢内容是否相等(equal)
[namager contentsEqualAtPath:filePath andPath:filePath2];
12.读取文件属性(attributes)
NSDictionary *attributes;
if((attributes = [manager attributesOfItemAnPath:newPath error:nil]) == nilt){
  NSLog(@"attributes");
}
13.文件细粒度操作,以只读的方式打开文件
NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath];
14.读取文件
NSData *data  = [fileHandle readDataToEndofFile];
15.以写的方式打开文件
NSFileHandle *fileHandle2 = [NSFileHandle fileHandleForWritingAtpath:newPath];
16.写入文件
[fileHandel writeData:data];
17.关闭文件
[fileHandle closeFile];
18.定位文件
[fileHandle seekToFileOffset:12];

 

你可能感兴趣的:(FileManager 文件管理器)