/file
文件操作
NSFileManager
常見的NSFileManager文件的方法:
-(BOOL)contentsAtPath:path ?? ? ?? ? ??從文件中讀取數據
-(BOOL)createFileAtPath:path contents:(BOOL)dataattributes:attr ? ??向一個文件寫入數據
-(BOOL)removeFileAtPath: path handler: handler? 刪除一個文件
-(BOOL)movePath: from toPath: to handler: handler重命名或移動一個文件(to可能已經存在)
-(BOOL)copyPath:from toPath:to handler: handler 復制文件(to不能存在)
-(BOOL)contentsEqualAtPath:path1 andPath:path2??比較兩個文件的內容
-(BOOL)fileExistsAtPath:path ??測試文件是否存在
-(BOOL)isReadablefileAtPath:path測試文件是否存在,且是否能執行讀操作
-(BOOL)isWritablefileAtPath:path測試文件是否存在,且是否能執行寫操作
-(NSDictionary *)fileAttributesAtPath:pathtraverseLink:(BOOL)flag ?獲取文件的屬性
-(BOOL)changeFileAttributes:attr atPath:path? ? ?? ? ?? ? ?? ??更改文件的屬性
NSFileManager對象的創建 :
NSFileManager *fm;
fm = [NSFileManagerdefaultManager];
NSDictionary *attr =[fm fileAttributesAtPath: fnametraverseLink: NO] ; //文件屬性
NSLog(@"file size is:%i bytes ",[[attrobjectForKey:NSFileSize] intValue]);
NSData *data =[fm contentsAtPath:@"filename"];//文件內容
常見的NSFileManager目錄的方法:
-(NSString *)currentDirectoryPath ?? ? ?? ? ?? ? ??獲取當前目錄
-(BOOL)changeCurrentDirectoryPath:path ?? ? ?? ? ??更改當前目錄
-(BOOL)copyPath:from toPath:to handler:handler? ??復制目錄結構,to不能已經存在
-(BOOL)createDirectoryAtPath:path attributes:attr? ?創建目錄
-(BOOL)fileExistsAtPath:path isDirectory:(BOOL *)flag? ? ? 測試文件是否為目錄(flag存儲結構yes/no)
-(NSArray *)contentsOfDirectoryAtPath:path ?? ? ?? ??列出目錄的內容
-(NSDirectoryEnumerator *)enumeratorAtPath:path?枚舉目錄的內容
-(BOOL)removeFileAtPath:path handler:handler? ?刪除空目錄
-(BOOL)movePath:from toPath:to handler:handler??重命名或移動一個目錄,to不能是已經存在的
path= [fm currentDirectoryPath] ;
NSArray *dirarray;
NSDirectoryEnumerator *direnu;
direnu = [fm enumeratorAtPath:path];
NSLog(@"contents of %@\n",path);
BOOL flag;
while((path = [direnu nextObject])!=nil)
{
NSLog(@"%@",path);
[fmfileExistsAtPath:path isDirectory:&flag];
if(flag ==YES)
[direnuskipDescendents]; //跳過子目錄
}
path= [fm currentDirectoryPath] ;
dirarray = [fm contentsOfDirectoryAtPath:path];
NSLog(@"%@ ",dirarray);
常用路徑工具函數
NSString * NSUserName(); 返回當前用戶的登錄名
NSString * NSFullUserName();返回當前用戶的完整用戶名
NSString * NSHomeDirectory();返回當前用戶主目錄的路徑
NSString * NSHomeDirectoryForUser();返回用戶user的主目錄
NSString * NSTemporaryDirectory();返回可用于創建臨時文件的路徑目錄
常用路徑工具方法
-(NSString *) pathWithComponents:components ?? ? ?? ? ?? ? ?? ?根據components中元素構造有效路徑
-(NSArray *)pathComponents ?? ? ?? ? ?? ? ?? ? ?? ? ?? ? ???析構路徑,獲取路徑的各個部分
-(NSString *)lastPathComponent ?? ? ?? ? ?? ? ?? ? ?? ? ?? ? ?提取路徑的最后一個組成部分
-(NSString *)pathExtension ?? ? ?? ? ?? ? ?? ? ?? ? ?? ? ?? ? 路徑擴展名
-(NSString *)stringByAppendingPathComponent:path? ? ?? ? ?? ? ??將path添加到現有路徑末尾
-(NSString *)stringByAppendingPathExtension:ext? ? ?? ? ?? ? ?? 將拓展名添加的路徑最后一個組成部分
-(NSString *)stringByDeletingPathComponent ?? ? ?? ? ?? ? ?? ? ?刪除路徑的最后一個部分
-(NSString *)stringByDeletingPathExtension ?? ? ?? ? ?? ? ?? ? ?刪除路徑的最后一個部分 的擴展名
-(NSString *)stringByExpandingTildeInPath ?? ? ?? ? ?? ? ?? ? ??將路徑中的代字符擴展成用戶主目錄(~)或指定用戶主目錄(~user)
-(NSString *)stringByResolvingSymlinksInPath? ? ?? ? ?? ? ?? ? ?嘗試解析路徑中的符號鏈接
-(NSString *)stringByStandardizingPath ?? ? ?? ? ?? ? ?? ? ?? ?通過嘗試解析~、..、.、和符號鏈接來標準化路徑
-
使用路徑NSPathUtilities.h
tempdir = NSTemporaryDirectory();臨時文件的目錄名
path = [fm currentDirectoryPath];
[path lastPathComponent];從路徑中提取最后一個文件名
fullpath = [pathstringByAppendingPathComponent: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
過濾數組中的文件類型 ?:[fileListpathsMatchingExtensions:[NSArrayarrayWithObject:@"jpg"]];
///////////////////////////////////////////////////////////////////////////////////////////////////////////
基本文件操作NSFileHandle
常用NSFileHandle方法
(NSFileHandle *)fileHandleForReadingAtPath:path? ? ?? ? ?? ? ?? ??打開一個文件準備讀取
(NSFileHandle *)fileHandleForWritingAtPath:path? ? ?? ? ?? ? ?? ??打開一個文件準備寫入
(NSFileHandle *)fileHandleForUpdatingAtPath:path? ? ?? ? ?? ? ?? ??打開一個文件準備更新(讀取和寫入)
-(NSData *)availableData ? ?? ? ?? ? ?? ? ?? ? ?? ? ?? ? ?? ? ?? ?從設備或通道返回可用數據
-(NSData *)readDataToEndOfFile ?? ? ?? ? ?? ? ?? ? ?? ? ?? ? ?? ??讀取其余的數據直到文件末尾(最多UINT_MAX字節)
-(NSData *)readDataOfLength:(unsigned int)bytes從文件讀取指定數目bytes的內容
-(void)writeData:data ? ?? ? ?? ? ??將data寫入文件
-(unsigned long long) offsetInFile ?? ?獲取當前文件的偏移量
-(void)seekToFileOffset:offset ?? ? ?設置當前文件的偏移量
-(unsigned long long) seekToEndOfFile ?? ?將當前文件的偏移量定位的文件末尾
-(void)truncateFileAtOffset:offset ?? ??將文件的長度設置為offset字節
-(void)closeFile ? ?? ? ?? ? ?? ? ?? ? 關閉文件
獲取文件大小
Using the C FILE type:
int getFileSizeFromPath(char * path)
{
FILE * file;
int fileSizeBytes = 0;
file = fopen(path,"r");
if(file>0){
fseek(file, 0, SEEK_END);
fileSizeBytes = ftell(file);
fseek(file, 0, SEEK_SET);
fclose(file);
}
return fileSizeBytes;
}
or in XCode use the NSFileManager:
NSFileManager * filemanager = [[NSFileManager alloc]init];
if([filemanager fileExistsAtPath:[self getCompletePath]isDirectory:&isDirectory]){
NSDictionary * attributes = [filemanagerattributesOfItemAtPath:[self getCompletePath] error:nil];
// file size
NSNumber *theFileSize;
if (theFileSize = [attributes objectForKey:NSFileSize])
_fileSize= [theFileSize intValue];
}