/file
文件操作
NSFileManager
常見的NSFileManager文件的方法:
-(BOOL)contentsAtPath:path ?? ? ?? ? ??從文件中讀取數(shù)據(jù)
-(BOOL)createFileAtPath:path contents:(BOOL)dataattributes:attr ? ??向一個(gè)文件寫入數(shù)據(jù)
-(BOOL)removeFileAtPath: path handler: handler? 刪除一個(gè)文件
-(BOOL)movePath: from toPath: to handler: handler重命名或移動(dòng)一個(gè)文件(to可能已經(jīng)存在)
-(BOOL)copyPath:from toPath:to handler: handler 復(fù)制文件(to不能存在)
-(BOOL)contentsEqualAtPath:path1 andPath:path2??比較兩個(gè)文件的內(nèi)容
-(BOOL)fileExistsAtPath:path ??測(cè)試文件是否存在
-(BOOL)isReadablefileAtPath:path測(cè)試文件是否存在,且是否能執(zhí)行讀操作
-(BOOL)isWritablefileAtPath:path測(cè)試文件是否存在,且是否能執(zhí)行寫操作
-(NSDictionary *)fileAttributesAtPath:pathtraverseLink:(BOOL)flag ?獲取文件的屬性
-(BOOL)changeFileAttributes:attr atPath:path? ? ?? ? ?? ? ?? ??更改文件的屬性
NSFileManager對(duì)象的創(chuàng)建 :
NSFileManager *fm;
fm = [NSFileManagerdefaultManager];
NSDictionary *attr =[fm fileAttributesAtPath: fnametraverseLink: NO] ; //文件屬性
NSLog(@"file size is:%i bytes ",[[attrobjectForKey:NSFileSize] intValue]);
NSData *data =[fm contentsAtPath:@"filename"];//文件內(nèi)容
常見的NSFileManager目錄的方法:
-(NSString *)currentDirectoryPath ?? ? ?? ? ?? ? ??獲取當(dāng)前目錄
-(BOOL)changeCurrentDirectoryPath:path ?? ? ?? ? ??更改當(dāng)前目錄
-(BOOL)copyPath:from toPath:to handler:handler? ??復(fù)制目錄結(jié)構(gòu),to不能已經(jīng)存在
-(BOOL)createDirectoryAtPath:path attributes:attr? ?創(chuàng)建目錄
-(BOOL)fileExistsAtPath:path isDirectory:(BOOL *)flag? ? ? 測(cè)試文件是否為目錄(flag存儲(chǔ)結(jié)構(gòu)yes/no)
-(NSArray *)contentsOfDirectoryAtPath:path ?? ? ?? ??列出目錄的內(nèi)容
-(NSDirectoryEnumerator *)enumeratorAtPath:path?枚舉目錄的內(nèi)容
-(BOOL)removeFileAtPath:path handler:handler? ?刪除空目錄
-(BOOL)movePath:from toPath:to handler:handler??重命名或移動(dòng)一個(gè)目錄,to不能是已經(jīng)存在的
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]; //跳過(guò)子目錄
}
path= [fm currentDirectoryPath] ;
dirarray = [fm contentsOfDirectoryAtPath:path];
NSLog(@"%@ ",dirarray);
常用路徑工具函數(shù)
NSString * NSUserName(); 返回當(dāng)前用戶的登錄名
NSString * NSFullUserName();返回當(dāng)前用戶的完整用戶名
NSString * NSHomeDirectory();返回當(dāng)前用戶主目錄的路徑
NSString * NSHomeDirectoryForUser();返回用戶user的主目錄
NSString * NSTemporaryDirectory();返回可用于創(chuàng)建臨時(shí)文件的路徑目錄
常用路徑工具方法
-(NSString *) pathWithComponents:components ?? ? ?? ? ?? ? ?? ?根據(jù)components中元素構(gòu)造有效路徑
-(NSArray *)pathComponents ?? ? ?? ? ?? ? ?? ? ?? ? ?? ? ???析構(gòu)路徑,獲取路徑的各個(gè)部分
-(NSString *)lastPathComponent ?? ? ?? ? ?? ? ?? ? ?? ? ?? ? ?提取路徑的最后一個(gè)組成部分
-(NSString *)pathExtension ?? ? ?? ? ?? ? ?? ? ?? ? ?? ? ?? ? 路徑擴(kuò)展名
-(NSString *)stringByAppendingPathComponent:path? ? ?? ? ?? ? ??將path添加到現(xiàn)有路徑末尾
-(NSString *)stringByAppendingPathExtension:ext? ? ?? ? ?? ? ?? 將拓展名添加的路徑最后一個(gè)組成部分
-(NSString *)stringByDeletingPathComponent ?? ? ?? ? ?? ? ?? ? ?刪除路徑的最后一個(gè)部分
-(NSString *)stringByDeletingPathExtension ?? ? ?? ? ?? ? ?? ? ?刪除路徑的最后一個(gè)部分 的擴(kuò)展名
-(NSString *)stringByExpandingTildeInPath ?? ? ?? ? ?? ? ?? ? ??將路徑中的代字符擴(kuò)展成用戶主目錄(~)或指定用戶主目錄(~user)
-(NSString *)stringByResolvingSymlinksInPath? ? ?? ? ?? ? ?? ? ?嘗試解析路徑中的符號(hào)鏈接
-(NSString *)stringByStandardizingPath ?? ? ?? ? ?? ? ?? ? ?? ?通過(guò)嘗試解析~、..、.、和符號(hào)鏈接來(lái)標(biāo)準(zhǔn)化路徑
-
使用路徑NSPathUtilities.h
tempdir = NSTemporaryDirectory();臨時(shí)文件的目錄名
path = [fm currentDirectoryPath];
[path lastPathComponent];從路徑中提取最后一個(gè)文件名
fullpath = [pathstringByAppendingPathComponent:fname];將文件名附加到路勁的末尾
extenson = [fullpath pathExtension];路徑名的文件擴(kuò)展名
homedir = NSHomeDirectory();用戶的主目錄
component = [homedir pathComponents];?路徑的每個(gè)部分
NSProcessInfo類:允許你設(shè)置或檢索正在運(yùn)行的應(yīng)用程序的各種類型信息
(NSProcessInfo *)processInfo ?? ? ?? ? ?? ? ?? ? ?? ? ??返回當(dāng)前進(jìn)程的信息
-(NSArray*)arguments ? ?? ? ?? ? ?? ? ?? ? ?? ? ?? ? ?? 以NSString對(duì)象數(shù)字的形式返回當(dāng)前進(jìn)程的參數(shù)
-(NSDictionary *)environment ?? ? ?? ? ?? ? ?? ? ?? ? ?? 返回變量/值對(duì)詞典。描述當(dāng)前的環(huán)境變量
-(int)processIdentity ? ?? ? ?? ? ?? ? ?? ? ?? ? ?? ? ??返回進(jìn)程標(biāo)識(shí)
-(NSString *)processName ? ?? ? ?? ? ?? ? ?? ? ?? ? ?? ? 返回進(jìn)程名稱
-(NSString *)globallyUniqueString ?? ? ?? ? ?? ? ?? ? ???每次調(diào)用該方法都會(huì)返回不同的單值字符串,可以用這個(gè)字符串生成單值臨時(shí)文件名
-(NSString *)hostname ? ?? ? ?? ? ?? ? ?? ? ?? ? ?? ? ??返回主機(jī)系統(tǒng)的名稱
-(unsigned int)operatingSystem ?? ? ?? ? ?? ? ?? ? ?? ? ?返回表示操作系統(tǒng)的數(shù)字
-(NSString *)operatingSystemName ?? ? ?? ? ?? ? ?? ? ?? ? ?? ?返回操作系統(tǒng)名稱
-(NSString *)operatingSystemVersionString ?? ? ?? ? ?? ? ?? ? ?? ? ?? ? 返回操作系統(tǒng)當(dāng)前版本
-(void)setProcessName:(NSString *)name ?? ? ?? ? ?? ? ?? ? ?? ??將當(dāng)前進(jìn)程名稱設(shè)置為name
過(guò)濾數(shù)組中的文件類型 ?:[fileListpathsMatchingExtensions:[NSArrayarrayWithObject:@"jpg"]];
///////////////////////////////////////////////////////////////////////////////////////////////////////////
基本文件操作NSFileHandle
常用NSFileHandle方法
(NSFileHandle *)fileHandleForReadingAtPath:path? ? ?? ? ?? ? ?? ??打開一個(gè)文件準(zhǔn)備讀取
(NSFileHandle *)fileHandleForWritingAtPath:path? ? ?? ? ?? ? ?? ??打開一個(gè)文件準(zhǔn)備寫入
(NSFileHandle *)fileHandleForUpdatingAtPath:path? ? ?? ? ?? ? ?? ??打開一個(gè)文件準(zhǔn)備更新(讀取和寫入)
-(NSData *)availableData ? ?? ? ?? ? ?? ? ?? ? ?? ? ?? ? ?? ? ?? ?從設(shè)備或通道返回可用數(shù)據(jù)
-(NSData *)readDataToEndOfFile ?? ? ?? ? ?? ? ?? ? ?? ? ?? ? ?? ??讀取其余的數(shù)據(jù)直到文件末尾(最多UINT_MAX字節(jié))
-(NSData *)readDataOfLength:(unsigned int)bytes從文件讀取指定數(shù)目bytes的內(nèi)容
-(void)writeData:data ? ?? ? ?? ? ??將data寫入文件
-(unsigned long long) offsetInFile ?? ?獲取當(dāng)前文件的偏移量
-(void)seekToFileOffset:offset ?? ? ?設(shè)置當(dāng)前文件的偏移量
-(unsigned long long) seekToEndOfFile ?? ?將當(dāng)前文件的偏移量定位的文件末尾
-(void)truncateFileAtOffset:offset ?? ??將文件的長(zhǎng)度設(shè)置為offset字節(jié)
-(void)closeFile ? ?? ? ?? ? ?? ? ?? ? 關(guān)閉文件
獲取文件大小
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];
}