生活随笔
收集整理的這篇文章主要介紹了
iOS学习之iOS沙盒(sandbox)机制和文件操作之NSFileManager
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
2019獨角獸企業(yè)重金招聘Python工程師標準>>>
1、在Documents里創(chuàng)建目錄
創(chuàng)建一個叫test的目錄,先找到Documents的目錄,
[cpp] view plain copy
NSArray?*paths?=?NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,?NSUserDomainMask,?YES);???? ???NSString?*documentsDirectory?=?[paths?objectAtIndex:0];???? ???NSLog(@"documentsDirectory%@",documentsDirectory);???? ???NSFileManager?*fileManager?=?[NSFileManager?defaultManager];???? ???NSString?*testDirectory?=?[documentsDirectory?stringByAppendingPathComponent:@"test"];???? ???//?創(chuàng)建目錄?? ???[fileManager?createDirectoryAtPath:testDirectory?withIntermediateDirectories:YES?attributes:nil?error:nil];?? 啟動程序,這時候目錄就創(chuàng)建了:
2、在test目錄下創(chuàng)建文件 創(chuàng)建文件怎么辦呢?接著上面的代碼 testPath 要用stringByAppendingPathComponent拼接上你要生成的文件名,比如test00.txt。這樣才能在test下寫入文件。
testDirectory是上面代碼生成的路徑哦,不要忘了。我往test文件夾里寫入三個文件,test00.txt ,test22.txt,text.33.txt。內(nèi)容都是寫入內(nèi)容,write String。
實現(xiàn)代碼如下:
[cpp] view plain copy
NSString?*testPath?=?[testDirectory?stringByAppendingPathComponent:@"test00.txt"];???? NSString?*testPath2?=?[testDirectory?stringByAppendingPathComponent:@"test22.txt"];???? NSString?*testPath3?=?[testDirectory?stringByAppendingPathComponent:@"test33.txt"];???? ?? ?? NSString?*string?=?@"寫入內(nèi)容,write?String";?? [fileManager?createFileAtPath:testPath?contents:[string??dataUsingEncoding:NSUTF8StringEncoding]?attributes:nil];?? [fileManager?createFileAtPath:testPath2?contents:[string??dataUsingEncoding:NSUTF8StringEncoding]?attributes:nil];?? [fileManager?createFileAtPath:testPath3?contents:[string??dataUsingEncoding:NSUTF8StringEncoding]?attributes:nil];?? 看下面的圖,三個文件都出來了,內(nèi)容也對。
在Documents目錄下創(chuàng)建就更簡單了,不用加test就ok了
3、獲取目錄列里所有文件名
兩種方法獲取:subpathsOfDirectoryAtPath 和 subpathsAtPath
[cpp] view plain copy
NSArray?*paths?=?NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,?NSUserDomainMask,?YES);???? NSString?*documentsDirectory?=?[paths?objectAtIndex:0];???? NSLog(@"documentsDirectory%@",documentsDirectory);???? NSFileManager?*fileManage?=?[NSFileManager?defaultManager];???? NSString?*myDirectory?=?[documentsDirectory?stringByAppendingPathComponent:@"test"];???? NSArray?*file?=?[fileManage?subpathsOfDirectoryAtPath:?myDirectory?error:nil];??? NSLog(@"%@",file);???? NSArray?*files?=?[fileManage?subpathsAtPath:?myDirectory?];??? NSLog(@"%@",files);?? 獲取上面剛才test文件夾里的文件名
打印結果
2012-06-17 23:23:19.684 IosSandbox[947:f803] fileList:(
? ? ".DS_Store",
? ? "test00.txt",
? ? "test22.txt",
? ? "test33.txt"
)
2012-06-17 23:23:19.686 IosSandbox[947:f803] fileLit(
? ? ".DS_Store",
? ? "test00.txt",
? ? "test22.txt",
? ? "test33.txt"
)
兩個方法都可以,隱藏的文件也打印出來了。
4、fileManager使用操作當前目錄
[cpp] view plain copy
//創(chuàng)建文件管理器?? ????NSFileManager?*fileManager?=?[NSFileManager?defaultManager];?? ????NSArray?*paths?=?NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,?NSUserDomainMask,?YES);?? ????NSString?*documentsDirectory?=?[paths?objectAtIndex:0];?? ????//更改到待操作的目錄下?? ????[fileManager?changeCurrentDirectoryPath:[documentsDirectory?stringByExpandingTildeInPath]];?? ????//創(chuàng)建文件fileName文件名稱,contents文件的內(nèi)容,如果開始沒有內(nèi)容可以設置為nil,attributes文件的屬性,初始為nil?? ????NSString?*?fileName?=?@"testFileNSFileManager.txt";?? ????NSArray?*array?=?[[NSArray?alloc]?initWithObjects:@"hello?world",@"hello?world1",?@"hello?world2",nil];?? ????[fileManager?createFileAtPath:fileName?contents:array?attributes:nil];?? 這樣就創(chuàng)建了testFileNSFileManager.txt并把三個hello world寫入文件了
changeCurrentDirectoryPath目錄更改到當前操作目錄時,做文件讀寫就很方便了,不用加上全路徑
5、刪除文件
接上面的代碼,remove就ok了。
[cpp] view plain copy
[fileManager?removeItemAtPath:fileName?error:nil];?? 6、混合數(shù)據(jù)的讀寫 用NSMutableData創(chuàng)建混合數(shù)據(jù),然后寫到文件里。并按數(shù)據(jù)的類型把數(shù)據(jù)讀出來
6.1寫入數(shù)據(jù): [cpp] view plain copy
NSString?*?fileName?=?@"testFileNSFileManager.txt";?? NSArray?*paths?=?NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,?NSUserDomainMask,?YES);?? NSString?*documentsDirectory?=?[paths?objectAtIndex:0];?? //獲取文件路徑?? NSString?*path?=?[documentsDirectory?stringByAppendingPathComponent:fileName];?? //待寫入的數(shù)據(jù)?? NSString?*temp?=?@"nihao?世界";?? int?dataInt?=?1234;?? float?dataFloat?=?3.14f;?? //創(chuàng)建數(shù)據(jù)緩沖?? NSMutableData?*writer?=?[[NSMutableData?alloc]?init];?? //將字符串添加到緩沖中?? [writer?appendData:[temp?dataUsingEncoding:NSUTF8StringEncoding]];????? //將其他數(shù)據(jù)添加到緩沖中?? [writer?appendBytes:&dataInt?length:sizeof(dataInt)];?? [writer?appendBytes:&dataFloat?length:sizeof(dataFloat)];???? //將緩沖的數(shù)據(jù)寫入到文件中?? [writer?writeToFile:path?atomically:YES];?? 我們看看數(shù)據(jù)怎么樣了:
我們看到后面的是亂碼,那是中文被轉成了NSData后,還有int float的二進制
6.2讀取剛才寫入的數(shù)據(jù):
[cpp] view plain copy
//讀取數(shù)據(jù):?? ???int?intData;?? ???float?floatData?=?0.0;?? ???NSString?*stringData;?? ????? ???NSData?*reader?=?[NSData?dataWithContentsOfFile:path];?? ???stringData?=?[[NSString?alloc]?initWithData:[reader?subdataWithRange:NSMakeRange(0,?[temp?length])]?? ??????????????????????????????????encoding:NSUTF8StringEncoding];?? ???[reader?getBytes:&intData?range:NSMakeRange([temp?length],?sizeof(intData))];?? ???[reader?getBytes:&floatData?range:NSMakeRange([temp?length]?+?sizeof(intData),?sizeof(floatData))];?? ???NSLog(@"stringData:%@?intData:%d?floatData:%f",?stringData,?intData,?floatData);?? 打印出來的結果:
2012-06-17 23:51:14.723 IosSandbox[1285:f803] stringData:nihao hello! intData:1234332 floatData:3.140000
這里把寫入的漢字改成了 hello。因為[temp length]算長度是,把中文算成一位了,出來的結果有誤。
轉載于:https://my.oschina.net/china008/blog/232783
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎
總結
以上是生活随笔為你收集整理的iOS学习之iOS沙盒(sandbox)机制和文件操作之NSFileManager的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。