iOS文件管理系統NSFileManager使用詳解


1,找到自己的程序的目錄: NSHomeDirectory() ,目錄結構為:
    ---Documents/
    ---"YourAppName.app"
    ---Library/
    ---自己創建的文件夾 或 "自己創建的文件"
    ---tmp/
所以要找到自己程序的Documents文件夾,可以使用[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; 
也可以使用:
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *string=[paths objectAtIndex:0];
兩者功能應該一樣。

2,文件一些主要操作可以通過NSFileManage 來操作,可以通過 [NSFileManger defaultManger] 得到它得實例。

3,使用[aNSString stringByAppendingPathCompnent:aNSString] 來創建文件路徑

4,tmp 目錄我們可以在里面寫入一些程序運行時需要用得數據,里面寫入得數據在程序退出后會沒有。
可以通過NSTemporaryDirectory();獲得其路徑。

5,NSFileManager的一些相關操作:
1)創建文件夾:
 NSString *myDirectory = [documentDirectory stringByAppendingPathComponent:@"test"];
     BOOL ok = [fileManage createDirectoryAtPath:myDirectory withIntermediateDirectories:YES attributes:nil error:&error];
2)取得一個目錄下得所有文件名:(如上面的myDirectory)可用
NSArray *file = [fileManager subpathsOfDirectoryAtPath: myDirectory error:nil];


NSArray *files = [fileManager subpathsAtPath: myDirectory ];

NSArray *files=[fileManager  contentsOfDirectoryAtPath:documentDirectory error:&error];

3)讀取某個文件:
NSData *data = [fileManger contentsAtPath:myFilePath];//myFilePath是包含完整路徑的文件名
或直接用NSData 的類方法:
NSData *data = [NSData dataWithContentOfPath:myFilePath];


4)保存某個文件:
可以用 NSFileManager的
- (BOOL)createFileAtPath:(NSString *)path contents:(NSData *)data attributes:(NSDictionary *)attr;
或 NSData 的
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;
- (BOOL)writeToFile:(NSString *)path options:(NSUInteger)writeOptionsMask error:(NSError **)errorPtr;

5)字符串寫入文件:
[str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
[NSString stringWithContentsOfFile...];

6)移動文件:
if ([fileMgr moveItemAtPath:filePath toPath:filePath2 error:&error] != YES)

7)刪除文件:
if ([fileMgr removeItemAtPath:filePath2 error:&error] != YES)

8)判斷是否是文件夾:
BOOL isDir = NO;
[fileManager fileExistsAtPath:path isDirectory:(&isDir)];
 if (isDir) { ...}

9)以下代碼用於獲取本機上的文件資源或圖片
獲取文本:
NSFileManager *fileManager=[NSFileManager defaultManager];
NSData *data=[fileManager contentsAtPath:@"/Developer/Documentation/wxWidgets/docs/lgpl.txt"];
NSString *string=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",string);
獲取圖片:
NSData *myData=[fileManager contentsAtPath:@"/Users/ruby/Desktop/Photo1.jpg"];
UIImage *myImage=[UIImage imageWithData:myData];

imageView.image=myImage;
 



//===========================
NSFileManager Class Reference:
概述:
NSFileManager類支持NSURL和NSString作為文件或文件夾的路徑。
如果你正在移動、拷貝、連接或刪除文件或文件夾,你可以使用一個代理連同file manager一起管理這些操作。代理的任務是確認操作和決定是否繼續--當錯誤發生時。在Mac OS X v10.7以后,代理必須遵守 NSFileManagerDelegate協議。
在iOS5.0以后和Mac OS X v10.7以后,NSFileManager包括了管理存儲在雲端的items的方法。標記為雲存儲的文件和文件夾被同步到iCloud。item在一個位置的更改會在同步時 are propagated to all other locations。
線程考慮:
NSFileManager可以安全地在多個線程中調用。然而,如果你使用一個delegate來接收move,copy,remove,and link操作的通知,你應該
創建一個唯一的file manager 對象的實例,指定你的代理到這個對象,並使用這個file manager來初始化你的操作。
任務:
1)創建一個File Manager:
- init   如果你打算使用delegate,你應該使用init方法創建一個fileManager
+defaultManager 
2)發現文件夾內容:
– contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error:
– contentsOfDirectoryAtPath:error:
– enumeratorAtURL:includingPropertiesForKeys:options:errorHandler:
– enumeratorAtPath:
– mountedVolumeURLsIncludingResourceValuesForKeys:options:
– subpathsOfDirectoryAtPath:error:
– subpathsAtPath:
3)創建和刪除items:
    – createDirectoryAtURL:withIntermediateDirectories:attributes:error:
    – createDirectoryAtPath:withIntermediateDirectories:attributes:error:
    – createFileAtPath:contents:attributes:
    – removeItemAtURL:error:
    – removeItemAtPath:error:
    – replaceItemAtURL:withItemAtURL:backupItemName:options:resultingItemURL:error:

4)移動和拷貝 Items

    – copyItemAtURL:toURL:error:
    – copyItemAtPath:toPath:error:
    – moveItemAtURL:toURL:error:
    – moveItemAtPath:toPath:error:
5)Determining Access to Files

    – fileExistsAtPath:
    – fileExistsAtPath:isDirectory:
    – isReadableFileAtPath:
    – isWritableFileAtPath:
    – isExecutableFileAtPath:
    – isDeletableFileAtPath:

6)Getting and Setting Attributes

    – componentsToDisplayForPath:
    – displayNameAtPath:
    – attributesOfItemAtPath:error:
    – attributesOfFileSystemForPath:error:
    – setAttributes:ofItemAtPath:error:

7)Getting and Comparing File Contents

    – contentsAtPath:
    – contentsEqualAtPath:andPath:

8)Converting File Paths to Strings

    – fileSystemRepresentationWithPath:
    – stringWithFileSystemRepresentation:length:

9)Managing the Delegate

    – setDelegate:
    – delegate

10)Managing the Current Directory

    – changeCurrentDirectoryPath:
    – currentDirectoryPath


NSFileManagerDelegate Protocol Reference:


Moving an Item

    – fileManager:shouldMoveItemAtURL:toURL:
    – fileManager:shouldMoveItemAtPath:toPath:
    – fileManager:shouldProceedAfterError:movingItemAtURL:toURL:
    – fileManager:shouldProceedAfterError:movingItemAtPath:toPath:

Copying an Item

    – fileManager:shouldCopyItemAtURL:toURL:
    – fileManager:shouldCopyItemAtPath:toPath:
    – fileManager:shouldProceedAfterError:copyingItemAtURL:toURL:
    – fileManager:shouldProceedAfterError:copyingItemAtPath:toPath:

Removing an Item

    – fileManager:shouldRemoveItemAtURL:
    – fileManager:shouldRemoveItemAtPath:
    – fileManager:shouldProceedAfterError:removingItemAtURL:
    – fileManager:shouldProceedAfterError:removingItemAtPath:

Linking an Item

    – fileManager:shouldLinkItemAtURL:toURL:
    – fileManager:shouldLinkItemAtPath:toPath:
    – fileManager:shouldProceedAfterError:linkingItemAtURL:toURL:
    – fileManager:shouldProceedAfterError:linkingItemAtPath:toPath:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM