1 NSFileManager
NSFileManger使用NSString作爲路徑名,可以是絕對路徑或相對路徑。相對路徑就是當前目錄。
絕對路徑以“/”開頭,“/”,表示根目錄。
常用方法:
NSFileManager *fm = [NSFileManager defaultManager];
//刪除文件
if([fm removeItemAtPath:@"abc" error:NULL]==NO){
NSLog("%@","刪除失敗");
}
//判斷是否存在
if( [fm fileExistsAtPath:@"test"]==NO )
//創建副本
if( [fm copyItemAtPath:@"test" toPath:@"newfile" error:NULL]==YES )
//判斷是否相等
if( [frm contentsEqualAtPath:@"test" toPath:@"newFile"]==YES )
//重命名
if( [frm moveItemAtPath:@"oldfile" toPath:@"newFile" error:NULL]==YES )
//獲取屬性
NSDictionary *attr = [frm attributesOfItemPath:@"newFile" error:NULL];
NSLog("%li",[attr objectForKey:NSFileSize]);
//刪除
if([ frm removeItemAtPath:@"oldFile" error:NULL]==YES)
//文件內容
NSLog( "%@",[frm stringWithContentsOfFile:@"file" encoding:NSUTF8StringEncoding error:NULL] );
2 緩衝區NSData
NSData可以設置緩衝區,將文件內容讀入緩衝區中,或將文件內容寫到文件中。32位環境下最多緩衝2G,64位緩衝8EB(8億G)。
不可變緩衝區NSData。
NSFileManage *fm = [NSFileManage defaultManager];
NSData *data;
data = [fm contentsAtPath:@"file"];
if( [fm createFileAtPath:@"file" contents:data attributes:nil]==NO ){
NSLog("%@","失敗");
}
可變緩衝區NSMutableData。
3 目錄
NSFileManager可以操作目錄。
枚舉目錄內容:
NSFileManage *fm = [NSFileManage defaultManager];
NSString *str = [fm currentDirectoryPath];
NSDirectoryEnumerator *dirEnum;
dirEnum = [fm enumeratorAtPath:str];
while( (path = [dirEnum nextObject]) != nil ){
NSLog(@"%@",path);
}
NSArray *arr = [fm contentsOfDirectoryAtPath:[fm currentPath] error:NULL];
for(path in arr){
NSLog(@"%@",path);
}
4 常用路徑方法
5 NSProcessInfo
ProcessInfo可以獲取到系統進程的內容,常用方法:
6 NSFileHandle
NSFileHandle的常用方法如下:
NSFileHandle可用於標準輸入輸出:fileHandleWithDevice,device可以是standardInput,standardOutput,standardError,NULLDevice。
注意:NSFileHandle並沒有提供創建文件的方法,所以它操作的是已經存在的文件,所以fileHandleForWritingAtPath 和 fileHandleForUpdatingAtPath操作的都是存在的文件,如果不存在就會返回nil。
NSFileHandle *inFile = [NSFileHandle fileHandleWithReadingAtPath:@"testFile"];
if(handle==nil){
return 1;
}
[[NSFileManager defaultManager] createFileAtPath:@"testout" contents:nil attributes:nil];
NSFileHandle *outFile = [FileHandle fileHandleForWritingAtPath:@"testout"];
//長度截斷爲0
[outFile truncateFileAtOffset:0];
NSData *data = [inFile readDataToEndOfFile];
[outFile writeData:data];
//關閉文件流
[inFile closeFile];
[outFile closeFile];
readDataToEndOfFile方法一次從文件中讀取UNIT_MAX字節數據,定義在limits.h,在大多數系統中等於FFFFFFFF。
readDataOfLength可以設置一次讀取字節數。
如果讀取到文件結尾,方法將返回一個空的NSData,可以使用length方法判斷是否讀取到數據。
如果要更新一個文件,偏移量被設置爲文件的開始,可以通過seekToFileOffSet
NSFileHandle *inFile = [NSFileHandle fileHandleWithReadingAtPath:@"testFile"];
NSFileHandle *outFile = [NSFileHandle fileHandleWithWritingAtPath:@"fileB"];
[outFile seekToEndOfFile];
NSData *data = [inFile readDataToEndOfFile];
[outFile writeData:data];
[inFile closeFile];
[outFile closeFile];
7 NSURL
NSURL *url = [NSURL URLWithString:@"http://google.com"];
NSString *content = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:NULL];
NSDictionary *dict = [NSDictionary dictionayWithContentsOfURL];
NSArray *arr = [NSArray arrayWithContentsOfURL];
NSData *data = [NSData dataWithContentsOfURL];
8 NSBundle簡介
bundle是一個目錄,其中包含了程序會使用到的資源。我們的程序是一個bundle. 在Finder中,一個應用程序看上去和其他文件沒有什麼區別. 但是實際上它是一個包含了nib文件,編譯代碼,以及其他資源的目錄. 我們把這個目錄叫做程序的main bundle。
NSBundle *myBundle = [NSBundle mainBundle]; //獲取main bundle。
NSBundle *bundle = [NSBundlebundleWithPath:@"~/.myApp/Good.bundle"];