NSFileMangerの使用説明
11537 ワード
NSFileManagerは、複数のスレッドで安全に呼び出すことができます.しかし、delegateを使用してmove、copy、remove、and link操作の通知を受信する場合は、defaultManagerではなくinitメソッドを使用してfileManagerを作成し、このオブジェクトにエージェントを指定し、このfile managerを使用して操作を初期化する一意のfile managerオブジェクトのインスタンスを作成する必要があります.
まず、自分のプログラムのディレクトリを見つけます.NSHomeDirectory()です.ディレクトリ構造は次のとおりです.
一般的なNSFileManagerファイルメソッド
NSFileManagerの使用例:
1、ファイルの作成
2、ファイルの名前を変更するファイルの名前を変更するには、ファイルを新しいパスの下に移動する必要があります.次のコードは、目的のターゲットファイルのパスを作成し、ファイルの移動と移動後のファイルディレクトリの表示を要求します.
3、ファイルを削除する
4、ディレクトリの下のすべてのファイルを削除する
5、書類があるかどうかを判断する
NSFileManagerの方法
NSFileManagerのエージェント
共通パスツール関数
一般的なパスツールの方法
パスを使うh
NSProcessInfoクラス:実行中のアプリケーションのさまざまなタイプの情報を設定または取得できます.
NSFileHandleクラスでは、ファイルをより効率的に使用できます.
1、ファイルを開き、読み取り、書き込み、または更新(読み書き)操作を実行する機能.2、ファイルの中で指定の場所を探す;3、ファイルから特定の数のバイトを読み出したり、特定の数のバイトをファイルに書き込んだりするほか、NSFileHandleクラスが提供する方法は、様々なデバイスやソケットにも使用できます.一般的に、ファイルを処理するには、1、ファイルを開いてNSFileHandleオブジェクトを取得する(後のI/O操作でファイルを参照するために).2、オープンファイルに対してI/O操作を行う.3、ファイルを閉じる.
注意:NSFileHandleクラスではファイルを作成する機能がありません.NSFileManagerを使用してファイルを作成する必要があります.
まず、自分のプログラムのディレクトリを見つけます.NSHomeDirectory()です.ディレクトリ構造は次のとおりです.
Documents(NSDocumentDirectory) // , ios iTunes ,
Library/Caches(NSCachesDirectory) // , 。iTunes
tmp(use NSTemporaryDirectory()) // , , ,
Library/Preferences // , NSUserDefault 、
// Documents , Doucments , ,
NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [docPath objectAtIndex:0];
NSLog(@"Documents :%@",documentsPath);
// Cache
NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [cacPath objectAtIndex:0];
NSLog(@"Cache :%@",cachePath);
//Library
NSArray *libsPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libPath = [libsPath objectAtIndex:0];
NSLog(@"Library :%@",libPath);
//temp
NSString *tempPath = NSTemporaryDirectory();
NSLog(@"temp :%@",tempPath);
一般的なNSFileManagerファイルメソッド
-(NSData *)contentsAtPath:path //
-(BOOL)createFileAtPath: path contents:(NSData *)data attributes:attr // ( attributes )
-(BOOL)removeItemAtPath:path error:err //
-(BOOL)moveItemAtPath:from toPath:to error:err // (to )
-(BOOL)copyItemAtPath:from toPath:to error:err // (to )
-(BOOL)contentsEqualAtPath:path andPath:path2 //
-(BOOL)fileExistAtPath:path //
-(BOOL)isReadableFileAtPath:path // ,
-(BOOL)isWriteableFileAtPath:path // ,
-(NSDictionary *)attributesOfItemAtPath:path error:err //
-(BOOL)setAttributesOfItemAtPath:attr error:err //
NSFileManagerの使用例:
1、ファイルの作成
-(IBAction) CreateFile
{
NSError *error; //
//
NSFileManager *fileMgr = [NSFileManager defaultManager];
//
NSString *documentsDirectory= [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
//
[[NSFileManager defaultManager] createDirectoryAtPath: [NSString stringWithFormat:@"%@/myFolder", NSHomeDirectory()] attributes:nil];
: , 。 ( )
NSString *filePath= [documentsDirectory stringByAppendingPathComponent:@"file2.txt"];
//
NSString *str= @"iPhoneDeveloper Tips
http://iPhoneDevelopTips,com";
//
[str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
//
NSLog(@"Documentsdirectory: %@",[fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);
}
2、ファイルの名前を変更するファイルの名前を変更するには、ファイルを新しいパスの下に移動する必要があります.次のコードは、目的のターゲットファイルのパスを作成し、ファイルの移動と移動後のファイルディレクトリの表示を要求します.
//
NSString *filePath2= [documentsDirectory
stringByAppendingPathComponent:@"file2.txt"];
//
if ([fileMgr moveItemAtPath:filePath toPath:filePath2 error:&error] != YES)
{
NSLog(@"Unable to move file: %@", [error localizedDescription]);
}else{
//
NSLog(@"Documentsdirectory: %@",[fileMgr contentsOfDirectoryAtPath:documentsDirectoryerror:&error]);
}
: (to )
3、ファイルを削除する
, :
// filePath2
if ([fileMgr removeItemAtPath:filePath2 error:&error] != YES){
NSLog(@"Unable to delete file: %@", [error localizedDescription]);
}else{
//
NSLog(@"Documentsdirectory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectoryerror:&error]);
}
4、ディレクトリの下のすべてのファイルを削除する
//
- (NSString *)attchmentFolder{
NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [document stringByAppendingPathComponent:@"Attchments"];
NSFileManager *manager = [NSFileManager defaultManager];
if(![manager contentsOfDirectoryAtPath:path error:nil]){
[manager createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:nil];
}
return path;
}
--
BOOL result = [[NSFileManager defaultManager] removeItemAtPath:[[MOPAppDelegate instance] attchmentFolder] error:nil];
5、書類があるかどうかを判断する
NSString *filePath =[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"test.plist"];
if ([[NSFileManager defaultManager]fileExistsAtPath:filePath])
{
//do some thing
}
NSFileManagerの方法
:
– contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error:
– contentsOfDirectoryAtPath:error:
– enumeratorAtURL:includingPropertiesForKeys:options:errorHandler:
– enumeratorAtPath:
– mountedVolumeURLsIncludingResourceValuesForKeys:options:
– subpathsOfDirectoryAtPath:error:
– subpathsAtPath:
items:
– createDirectoryAtURL:withIntermediateDirectories:attributes:error:
– createDirectoryAtPath:withIntermediateDirectories:attributes:error:
– createFileAtPath:contents:attributes:
– removeItemAtURL:error:
– removeItemAtPath:error:
– replaceItemAtURL:withItemAtURL:backupItemName:options:resultingItemURL:error:
Items
– copyItemAtURL:toURL:error:
– copyItemAtPath:toPath:error:
– moveItemAtURL:toURL:error:
– moveItemAtPath:toPath:error:
Determining Access to Files
– fileExistsAtPath:
– fileExistsAtPath:isDirectory:
– isReadableFileAtPath:
– isWritableFileAtPath:
– isExecutableFileAtPath:
– isDeletableFileAtPath:
Getting and Setting Attributes
– componentsToDisplayForPath:
– displayNameAtPath:
– attributesOfItemAtPath:error:
– attributesOfFileSystemForPath:error:
– setAttributes:ofItemAtPath:error:
Getting and Comparing File Contents
– contentsAtPath:
– contentsEqualAtPath:andPath:
Converting File Paths to Strings
– fileSystemRepresentationWithPath:
– stringWithFileSystemRepresentation:length:
Managing the Delegate
– setDelegate:
– delegate
Managing the Current Directory
– changeCurrentDirectoryPath:
– currentDirectoryPath
NSFileManagerのエージェント
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:
共通パスツール関数
NSString * NSUserName();
NSString * NSFullUserName();
NSString * NSHomeDirectory();
NSString * NSHomeDirectoryForUser(); user
NSString * NSTemporaryDirectory();
一般的なパスツールの方法
-(NSString *)pathWithComponents:components components(NSArray )
-(NSArray *) pathComponents ,
-(NSString *)lastPathComponent
-(NSString *)pathExtension
-(NSString *)stringByAppendingPathComponent:path path
-(NSString *)stringByAppendingPathExtension:ext
-(NSString *)stringByDeletingPathComponent
-(NSString *)stringByDeletingPathExtension
-(NSString *)stringByExpandingTildeInPath (~) (~user)
-(NSString *)stringByResolvingSymlinksInPath
-(NSString *)stringByStandardizingPath ~、..、.、
パスを使うh
tempdir = NSTemporaryDirectory();
path = [fm currentDirectoryPath];
[path lastPathComponent];
fullpath = [path stringByAppendingPathComponent:fname];
extenson = [fullpath pathExtension];
homedir = NSHomeDirectory();
component = [homedir pathComponents];
NSProcessInfoクラス:実行中のアプリケーションのさまざまなタイプの情報を設定または取得できます.
(NSProcessInfo *)processInfo
-(NSArray*)arguments NSString
-(NSDictionary *)environment / 。
-(int)processIdentity
-(NSString *)processName
-(NSString *)globallyUniqueString ,
-(NSString *)hostname
-(unsigned int)operatingSystem
-(NSString *)operatingSystemName
-(NSString *)operatingSystemVersionString
-(void)setProcessName:(NSString *)name name
NSFileHandleクラスでは、ファイルをより効率的に使用できます.
1、ファイルを開き、読み取り、書き込み、または更新(読み書き)操作を実行する機能.2、ファイルの中で指定の場所を探す;3、ファイルから特定の数のバイトを読み出したり、特定の数のバイトをファイルに書き込んだりするほか、NSFileHandleクラスが提供する方法は、様々なデバイスやソケットにも使用できます.一般的に、ファイルを処理するには、1、ファイルを開いてNSFileHandleオブジェクトを取得する(後のI/O操作でファイルを参照するために).2、オープンファイルに対してI/O操作を行う.3、ファイルを閉じる.
NSFileHandle *fileHandle = [[NSFileHandle alloc]init];
//
fileHandle = [NSFileHandle fileHandleForReadingAtPath:path];
fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:path];
//
fileData = [fileHandle availableData];
fileData = [fileHandle readDataToEndOfFile];
// NSData
[fileHandle writeData:fileData];
// ... ...
[fileHandle closeFile];
注意:NSFileHandleクラスではファイルを作成する機能がありません.NSFileManagerを使用してファイルを作成する必要があります.