ios資料存放區

來源:互聯網
上載者:User

標籤:

1.檔案儲存體

ios沙箱機制:應用程式只能訪問該應用程式在檔案系統中建立的目錄,

Documents檔案夾:文檔檔案夾,存放持久化的資料,

library檔案夾:caches:存放快取檔案,重啟或退出程式時,資料不會丟失

                   Perferences:喜好設定,存放使用者佈建資訊

tmp檔案夾:存放臨時的快取資料,在重啟或退出程式時清空;

基本檔案操作:

- (void)viewDidLoad

{

    [super viewDidLoad];  

//   檔案管理者

//    NSFileManager

//建立檔案管理對象

    NSFileManager *fm = [NSFileManager defaultManager];

//擷取Documents檔案夾路徑    

    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentPath = [array objectAtIndex:0];  

//   建立檔案夾

//    1.建立檔案夾的路徑

NSString *directoryPath = [documentPath  stringByAppendingPathComponent:@"testFile"];//設定檔案夾路徑,stringByAppendingPathComponent方法會自動用"/"分隔形成完整路徑,

    [fm createDirectoryAtPath:directoryPath withIntermediateDirectories:YES attributes:nil error:nil];

//    2.建立檔案

//    拼接檔案的路徑

    NSString *filePath = [directoryPath stringByAppendingPathComponent:@"file"];

//    NSData  用來封裝資料的

//    存的都是位元據

//    可以存各種資料 字串 音頻  映像 數組 字典

//   把字串轉換成data

    NSString *string = @"你好";

//   UTF8 編碼

    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];

//    建立檔案

    [fm createFileAtPath:filePath contents:data attributes:nil]; 

//    判斷檔案是否存在

   BOOL success = [fm fileExistsAtPath:filePath];

    NSLog(@"是否存在檔案%d",success); 

//    來擷取某個路徑下的所有檔案的路徑

    NSArray *subpathArray = [fm subpathsAtPath:directoryPath];

    for (NSString *path in subpathArray)

    {

        NSLog(@"---->%@",path);

    }

    NSString *path2 = [directoryPath stringByAppendingPathComponent:@"file副本"]; 

//    判斷內容是否相等

//    contentsEqualAtPath

    BOOL success1 = [fm contentsEqualAtPath:filePath andPath:path2];

    NSLog(@"success1 ==  %d",success1);

//    移動某路徑下的元素到另外一個路徑  

    NSString *destinationPath = [NSHomeDirectory() stringByAppendingPathComponent:@"a.txt"];

//    把檔案移動到某檔案路徑(而不是指的目錄)

    [fm moveItemAtPath:filePath toPath:destinationPath error:nil];

//    複製到某個檔案路徑下

    [fm copyItemAtPath:destinationPath toPath:filePath error:nil];

//    移除某路徑下的檔案

    [fm removeItemAtPath:filePath error:nil];

}

 

 

ios資料存放區

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.