文件管理 NSFileManager


#import "ViewController.h"

@interface ViewController ()
{
    NSFileManager *_manager;
    NSString *_document;
    NSString *_str;
    NSData *_data;
    NSString *_path;
}

@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //一個應用程序對應一個沙盒目錄 當前應用程序只能訪問自己的沙盒目錄,不能訪問其他應用程序的沙盒目錄
    //Documents: 存放永久化數據文件
    //Library: 存放系統的大型文件(永久) 和緩存數據-cache
    //Tmp: 存放臨時性文件,應用程序重新啓動將銷燬所有數據

    //獲取沙盒目錄
    NSString *homePath=NSHomeDirectory();
    _document=[homePath stringByAppendingPathComponent:@"Documents"];


    //文件管理
    _manager=[NSFileManager defaultManager];

    //創建一個文件
    //編輯文件內部的文件
    _str=@"你好大神";

    _data=[_str dataUsingEncoding:NSUTF8StringEncoding];

    //創建文件存儲路徑
    _path=[_document stringByAppendingPathComponent:@"test.tst"];

    //創建文件
    [self creatFile];

    //創建文件夾
    [self creatDirectory];

    //讀取文件內容
    [self readFile];

    //文件的屬性
    [self propertiOfFile];

    //文件的 複製 剪切 刪除
    [self changeFile];
    //通過路徑在 Finder 上查找文件是否創建成功

}


- (void)creatFile{

    //創建文件 如果重複創建會覆蓋原文件;
    BOOL isSuc=[_manager createFileAtPath:_path contents:_data attributes:nil];

    if (isSuc) {
        NSLog(@"文件創建成功");
    }

}

- (void)creatDirectory{

    BOOL isSuc=[_manager createDirectoryAtPath:[_document stringByAppendingString:@"/test2"] withIntermediateDirectories:YES attributes:nil error:nil];

    if (isSuc) {
        NSLog(@"文件夾創建成功");
    }
}

- (void)readFile{

    NSData *data=[_manager contentsAtPath:_path];

    NSString *str1=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"str1 is:%@",str1);

    //這個方法已經被棄用了
    NSString *str2=[NSString stringWithContentsOfFile:_path];
    NSLog(@"str2 is:%@",str2);

}

- (void)changeFile{
    //複製文件
    BOOL isCopy=[_manager copyItemAtPath:_path toPath:[_document stringByAppendingString:@"/test22/test.txt"] error:nil];
    if (isCopy) {
        NSLog(@"複製成功");
    }

    //剪切文件
    BOOL isSuc=[_manager moveItemAtPath:_path toPath:[_document stringByAppendingPathComponent:@"test2/test1.txt"] error:nil];

    if (isSuc) {
        NSLog(@"移動成功");
    }

    BOOL isDelete=[_manager removeItemAtPath:_path error:nil];

    if (isDelete) {
        NSLog(@"刪除成功");
    }


}

- (void)propertiOfFile{

    //獲取出來的文件的屬性有很多 以字典的形式存儲
    NSDictionary *attributes=[_manager attributesOfItemAtPath:_path error:nil];

    NSLog(@"attributes is:%@",attributes);

    //計算 document 中的文件總大小
    //獲取當前文件夾下的所有子文件 的到的是子文件的相對路徑
    NSArray *subPaths=[_manager subpathsAtPath:_document];
    NSLog(@"subPaths is:%@",subPaths);

    NSInteger totalSize=0;
    for (NSString *path in subPaths) {
        //將得到的相路徑拼接成絕對路徑
        NSString *subPath=[_document stringByAppendingPathComponent:path];

        //獲取子文件的屬性
        NSDictionary *subFileAttributes=[_manager attributesOfItemAtPath:subPath error:nil];

        //NSFileSize 是系統給帶的字符串 相當於@"NSFileSize"
        NSNumber *size=[subFileAttributes objectForKey:NSFileSize];

        totalSize +=[size integerValue];
    }

    NSLog(@"totalSize is:%ld",totalSize);

}

@end
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章