//file
文件操做
NSFileManager
常見的NSFileManager文件的方法:
-(BOOL)contentsAtPath:path 從文件中讀取數據
-(BOOL)createFileAtPath:path contents:(BOOL)data attributes: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:path traverseLink:(BOOL)flag 獲取文件的屬性
-(BOOL)changeFileAttributes:attr atPath:path 更改文件的屬性
NSFileManager對象的建立 :
NSFileManager *fm;
fm = [NSFileManager defaultManager];
NSDictionary *attr =[fm fileAttributesAtPath: fname traverseLink: NO] ; //文件屬性
NSLog(@"file size is:%i bytes ",[[attr objectForKey: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);
[fm fileExistsAtPath:path isDirectory:&flag];
if(flag == YES)
[direnu skipDescendents]; //跳過子目錄
}
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 = [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
過濾數組中的文件類型 : [fileList pathsMatchingExtensions:[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 = [filemanager attributesOfItemAtPath:[self getCompletePath] error:nil];post
// file size
NSNumber *theFileSize;
if (theFileSize = [attributes objectForKey:NSFileSize])測試
_fileSize= [theFileSize intValue];
}spa