1. 程式人生 > >FileManager 檔案管理器

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];