iOS學習筆記(十七)——檔案操作(NSFileManager)

來源:互聯網
上載者:User

Library:儲存程式的預設設定或其它狀態資訊;

               

 

-(void)dirHome{    NSString *dirHome=NSHomeDirectory();        NSLog(@"app_home: %@",dirHome);}
目錄路徑:

 

 

//擷取Documents目錄-(NSString *)dirDoc{    //[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentsDirectory = [paths objectAtIndex:0];    NSLog(@"app_home_doc: %@",documentsDirectory);    return documentsDirectory;}
擷取Library目錄路徑:

 

 

//擷取Library目錄-(void)dirLib{    //[NSHomeDirectory() stringByAppendingPathComponent:@"Library"];    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);    NSString *libraryDirectory = [paths objectAtIndex:0];    NSLog(@"app_home_lib: %@",libraryDirectory);}
擷取Cache目錄路徑:

 

 

//擷取Cache目錄-(void)dirCache{    NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);    NSString *cachePath = [cacPath objectAtIndex:0];    NSLog(@"app_home_lib_cache: %@",cachePath);}

 

擷取Tmp目錄路徑:

 

 

 

//擷取Tmp目錄-(void)dirTmp{    //[NSHomeDirectory() stringByAppendingPathComponent:@"tmp"];    NSString *tmpDirectory = NSTemporaryDirectory();    NSLog(@"app_home_tmp: %@",tmpDirectory);}

 

 

 

//建立檔案夾-(void *)createDir{    NSString *documentsPath =[self dirDoc];    NSFileManager *fileManager = [NSFileManager defaultManager];    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];    // 建立目錄    BOOL res=[fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];    if (res) {        NSLog(@"檔案夾建立成功");    }else        NSLog(@"檔案夾建立失敗"); }



 

 

//建立檔案-(void *)createFile{    NSString *documentsPath =[self dirDoc];    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];    NSFileManager *fileManager = [NSFileManager defaultManager];    NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];    BOOL res=[fileManager createFileAtPath:testPath contents:nil attributes:nil];    if (res) {        NSLog(@"檔案建立成功: %@" ,testPath);    }else        NSLog(@"檔案建立失敗");}

 

 

//寫檔案-(void)writeFile{    NSString *documentsPath =[self dirDoc];    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];    NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];    NSString *content=@"測試寫入內容!";    BOOL res=[content writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:nil];    if (res) {        NSLog(@"檔案寫入成功");    }else        NSLog(@"檔案寫入失敗");}

 

 

 

//讀檔案-(void)readFile{    NSString *documentsPath =[self dirDoc];    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];    NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];//    NSData *data = [NSData dataWithContentsOfFile:testPath];//    NSLog(@"檔案讀取成功: %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);    NSString *content=[NSString stringWithContentsOfFile:testPath encoding:NSUTF8StringEncoding error:nil];    NSLog(@"檔案讀取成功: %@",content);}

 

 

 

//檔案屬性-(void)fileAttriutes{    NSString *documentsPath =[self dirDoc];    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];    NSFileManager *fileManager = [NSFileManager defaultManager];    NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];    NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:testPath error:nil];       NSArray *keys;    id key, value;    keys = [fileAttributes allKeys];    int count = [keys count];    for (int i = 0; i < count; i++)    {        key = [keys objectAtIndex: i];        value = [fileAttributes objectForKey: key];        NSLog (@"Key: %@ for value: %@", key, value);    }}

 

 

  

//刪除檔案-(void)deleteFile{    NSString *documentsPath =[self dirDoc];    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];    NSFileManager *fileManager = [NSFileManager defaultManager];    NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];       BOOL res=[fileManager removeItemAtPath:testPath error:nil];    if (res) {        NSLog(@"檔案刪除成功");    }else        NSLog(@"檔案刪除失敗");       NSLog(@"檔案是否存在: %@",[fileManager isExecutableFileAtPath:testPath]?@"YES":@"NO");}

 

 

 

 

 


@張興業TBOW(#->@)

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.