iOS學習-沙箱路徑總結、檔案管理NSFileManager總結,沙箱nsfilemanager

來源:互聯網
上載者:User

iOS學習-沙箱路徑總結、檔案管理NSFileManager總結,沙箱nsfilemanager

//

//  ViewController.m

//  沙箱操作

//

//  Created by mncong on 15/11/26.

//  Copyright © 2015年 mancong. All rights reserved.

//

 

#import "ViewController.h"

 

@interface ViewController ()

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

 

    //到達沙箱根路徑

    [self getHomePath];

    

    //擷取Document路徑

    [self getDocumentsPath];

    

    //擷取Library目錄路徑

    [self getLibraryPath];

    

    //擷取Library中的Cache路徑

    [self getCachePath];

    

    //擷取temp路徑

    [self getTempPath];

    

    //NSString類路徑的處理

    [self dealWithPath];

    

    //NSData處理

    [self dealWithData];

    

    //檔案管理

    [self dealWithNSaFileManager];

    

    //擷取檔案中檔案和檔案清單

    [self getDirectorys];

}

 

- (void)getHomePath

{

    NSString * homePath = NSHomeDirectory();

    NSLog(@"app_home: %@",homePath);

}

 

- (NSString *)getDocumentsPath

{

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

    NSLog(@"%@",pathsArr);

    NSString * documentsPath = [pathsArr lastObject];

    NSLog(@"app_documentsPath: %@",documentsPath);

     return documentsPath;

}

- (void)getLibraryPath

{

    NSArray * pathsArr = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);

    NSString * documentsPath = [pathsArr lastObject];

    NSLog(@"app_documentsPath: %@",documentsPath);

}

- (void)getCachePath

{

    NSArray * pathsArr = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);

    NSString * cachePath = [pathsArr lastObject];

    NSLog(@"cachePath: %@",cachePath);

}

- (void)getTempPath

{

    NSString * tempPath = NSTemporaryDirectory();

    NSLog(@"app_tempPath: %@",tempPath);

}

- (void)dealWithPath

{

    //操作的路徑

    NSString * path = @"/Users/apple/testfile.text";

    

    //擷取此路徑的各個組成部分

    NSArray * arr = [path pathComponents];

    NSLog(@"%@",arr);

    

    //提取此路徑的最後一個組成部分

    NSString * lastComponent = [path lastPathComponent];

    NSLog(@"%@",lastComponent);

    

    //刪除路徑的最後一個組成部分

    NSString * deleteLastComponent = [path stringByDeletingLastPathComponent];

    NSLog(@"%@",deleteLastComponent);

    

    //將path添加到先郵路徑的末尾

    NSString * addPathToPath = [path stringByAppendingPathComponent:path];

    NSLog(@"%@",addPathToPath);

    

    //去除路徑最後部分的副檔名

    NSString * extension = [path pathExtension];

    NSLog(@"%@",extension);

    

    //刪除路徑最後部分的副檔名

    NSString * deletePathExtension = [path stringByDeletingPathExtension];

    NSLog(@"%@",deletePathExtension);

    

    //路徑最後部分追加副檔名(注意:方法已經拼接了一個點號了,不要再加了)

    NSString * appendExtension = [path stringByAppendingPathExtension:@"jpg"];

    NSLog(@"%@",appendExtension);

}

- (void)dealWithData

{

    //1.NSString與NSData轉換

    NSString * string1 = @"1234abcd";

    NSData * data1 = [string1 dataUsingEncoding:NSUTF8StringEncoding];

    NSLog(@"data1: %@",data1);

    

    NSString * string2 = [[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding];

    NSLog(@"string2: %@",string2);

    

    //2.NSData與UIImage

    //拼接在本工程下的路徑

    NSString * path = [[NSBundle mainBundle] bundlePath];

    NSString * name = [NSString stringWithFormat:@"1.jpg"];

    NSString * finalPath = [path stringByAppendingPathComponent:name];

    //把路徑改為NSData類型

    NSData * imageData = [NSData dataWithContentsOfFile:finalPath];

    //擷取圖片

    UIImage * image = [UIImage imageWithData:imageData];

    //顯示在UIimageView上

    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200)];

    imageView.image = image;

    [self.view addSubview:imageView];

}

- (void)dealWithNSaFileManager

{

    NSError * error;

    //建立檔案管理

    NSFileManager * fileManager = [NSFileManager defaultManager];

    //指向文檔目錄

    NSString * documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

    NSLog(@"documentsDirectory: %@",documentsDirectory);

    //建立一個目錄

#warning format之後什麼意思 以及後面的參數

    [[NSFileManager defaultManager] createDirectoryAtPath:[NSString stringWithFormat:@"%@/myFolder",NSHomeDirectory()] withIntermediateDirectories:YES attributes:nil error:&error];

    

    

    //1.建立一個檔案

    NSString * filePath = [documentsDirectory stringByAppendingPathComponent:@"file1.txt"];

    NSLog(@"filePath: %@",filePath);

    NSString * str = @"需要寫入的字串";

    //寫入檔案

    [str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];

    //顯示檔案目錄的內容

    NSLog(@"documentsDirectory: %@",[fileManager contentsOfDirectoryAtPath:documentsDirectory error:&error]);

    

    //2.對一個檔案重新命名

    NSString * filePath2 = [documentsDirectory stringByAppendingPathComponent:@"file2.text"];

    NSLog(@"%@",filePath2);

    if ([fileManager moveItemAtPath:filePath toPath:filePath2 error:&error] != YES)

        NSLog(@"Unable to mnove file: %@",error.localizedDescription);

    NSLog(@"DocumentsDirectory: %@",[fileManager contentsOfDirectoryAtPath:documentsDirectory error:&error]);

 

    //3.刪除一個檔案

    if ([fileManager removeItemAtPath:filePath2 error:&error] != YES)

        NSLog(@"Unable to delete file:%@",error.localizedDescription);

    NSLog(@"Documentsdirectory: %@",[fileManager contentsOfDirectoryAtPath:documentsDirectory error:&error]);

}

- (void)getDirectorys

{

    NSFileManager * fileManager = [NSFileManager defaultManager];

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

    NSString * documentsDirectory = [documentsPaths objectAtIndex:0];

    NSError * error = nil;

    NSArray * fileList = [NSArray array];

    fileList = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:&error];

    NSLog(@"fileList: %@",fileList);

    

    //列出給定一個檔案夾裡的所有子檔案夾名

    NSMutableArray * dirArray = [NSMutableArray array];

    BOOL isDir = NO;

     for (NSString * file in fileList) {

        NSString * path = [documentsDirectory stringByAppendingPathComponent:file];

        [fileManager fileExistsAtPath:path isDirectory:(&isDir)];

     if (isDir) {

            [dirArray addObject:file];

        }

        isDir = NO;

    }

    NSLog(@"Every Thing in the Dir: %@",fileList);

    NSLog(@"All folders: %@",dirArray);

}

 

@end

 

相關文章

聯繫我們

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