日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS学习之iOS沙盒(sandbox)机制和文件操作之NSFileManager

發(fā)布時(shí)間:2023/12/10 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS学习之iOS沙盒(sandbox)机制和文件操作之NSFileManager 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

1、在Documents里創(chuàng)建目錄

創(chuàng)建一個(gè)叫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];??
  • 啟動(dòng)程序,這時(shí)候目錄就創(chuàng)建了:


    2、在test目錄下創(chuàng)建文件

    創(chuàng)建文件怎么辦呢?接著上面的代碼 testPath 要用stringByAppendingPathComponent拼接上你要生成的文件名,比如test00.txt。這樣才能在test下寫入文件。

    testDirectory是上面代碼生成的路徑哦,不要忘了。我往test文件夾里寫入三個(gè)文件,test00.txt ,test22.txt,text.33.txt。內(nèi)容都是寫入內(nèi)容,write String。

    實(shí)現(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];??
  • 看下面的圖,三個(gè)文件都出來了,內(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文件夾里的文件名

    打印結(jié)果

    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"

    )

    兩個(gè)方法都可以,隱藏的文件也打印出來了。

    4、fileManager使用操作當(dāng)前目錄

    [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)容可以設(shè)置為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并把三個(gè)hello world寫入文件了

    changeCurrentDirectoryPath目錄更改到當(dāng)前操作目錄時(shí),做文件讀寫就很方便了,不用加上全路徑

    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ù)怎么樣了:


    我們看到后面的是亂碼,那是中文被轉(zhuǎn)成了NSData后,還有int float的二進(jìn)制

    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);??

  • 打印出來的結(jié)果:

    2012-06-17 23:51:14.723 IosSandbox[1285:f803] stringData:nihao hello! intData:1234332 floatData:3.140000

    這里把寫入的漢字改成了 hello。因?yàn)閇temp length]算長度是,把中文算成一位了,出來的結(jié)果有誤。

    轉(zhuǎn)載于:https://my.oschina.net/china008/blog/232783

    創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

    總結(jié)

    以上是生活随笔為你收集整理的iOS学习之iOS沙盒(sandbox)机制和文件操作之NSFileManager的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。