天天看點

20150619_OC之NSFileManager檔案管理器

//
//  main.m
//  IOS150619_ObjectiveC_目錄及檔案的建立
//
//  Created by qianfeng on 15/6/19.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSFileManager *file1 = [NSFileManager defaultManager];
        
        //建立目錄
        //- (BOOL)createDirectoryAtPath:(NSString *)path withIntermediateDirectories:(BOOL)createIntermediates attributes:(NSDictionary *)attributes error:(NSError **)error NS_AVAILABLE(10_5, 2_0);
        //path:建立目錄路徑,包含要建立的目錄的目錄名
        //createIntermediates:是否建立中間目錄
        //attributes:目錄屬性
        NSError *error = nil;
        BOOL ret = [file1 createDirectoryAtPath:@"/Users/qianfeng/Desktop/test/dic3" withIntermediateDirectories:YES attributes:nil error:&error];
        if (ret) {
            NSLog(@"建立目錄成功");
        }
        else
        {
            NSLog(@"建立目錄失敗,%@",error);
        }
        
        //建立檔案
        //- (BOOL)createFileAtPath:(NSString *)path contents:(NSData *)data attributes:(NSDictionary *)attr;
        //path:檔案的路徑,要包含檔案名
        //data:檔案的初始内容
        //attr:檔案袋餓屬性
        ret = [file1 createFileAtPath:@"/Users/qianfeng/Desktop/test/dic3/Hello.txt" contents:nil attributes:nil];
        if (ret) {
            NSLog(@"檔案目錄成功");
        }
        else
        {
            NSLog(@"檔案目錄失敗");
        }
        
        /* attributesOfItemAtPath:error: returns an NSDictionary of key/value pairs containing the attributes of the item (file, directory, symlink, etc.) at the path in question. If this method returns 'nil', an NSError will be returned by reference in the 'error' parameter. This method does not traverse a terminal symlink.
         
         This method replaces fileAttributesAtPath:traverseLink:.
         */
        //擷取檔案或者目錄的屬性
        //- (NSDictionary *)attributesOfItemAtPath:(NSString *)path error:(NSError **)error NS_AVAILABLE(10_5, 2_0);
        NSDictionary *attribute = [file1 attributesOfItemAtPath:@"/Users/qianfeng/Desktop/test/1.txt" error:nil];
        NSLog(@"%@",attribute);
        //結果:
//        {
//            NSFileCreationDate = "2015-06-19 03:07:33 +0000";
//            NSFileExtensionHidden = 0;
//            NSFileGroupOwnerAccountID = 20;
//            NSFileGroupOwnerAccountName = staff;
//            NSFileHFSCreatorCode = 0;
//            NSFileHFSTypeCode = 0;
//            NSFileModificationDate = "2015-06-19 03:07:33 +0000";
//            NSFileOwnerAccountID = 501;
//            NSFileOwnerAccountName = qianfeng;
//            NSFilePosixPermissions = 420;
//            NSFileReferenceCount = 1;
//            NSFileSize = 0;
//            NSFileSystemFileNumber = 2265753;
//            NSFileSystemNumber = 16777220;
//            NSFileType = NSFileTypeRegular;
//        }
        NSLog(@"size = %@",[attribute objectForKey:@"NSFileSize"]);
        //結果:size = 0
        
        NSDictionary  *attri = [file1 attributesOfFileSystemForPath:@"/Users/qianfeng/Desktop/test/1.txt" error:nil];
        NSLog(@"%@",attri);
        //結果:
//        {
//            NSFileSystemFreeNodes = 108641653;
//            NSFileSystemFreeSize = 444996210688;
//            NSFileSystemNodes = 121798654;
//            NSFileSystemNumber = 16777220;
//            NSFileSystemSize = 498887294976;
//        }
        
        //檔案拷貝
        //- (BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error NS_AVAILABLE(10_5, 2_0);
        //dstPath:必須包含需要拷貝的目的檔案名或者目錄名,否則失敗.在一下例子中是1.txt
        //srcPath:The path to the file or directory you want to move. This parameter must not be nil.
        //dstPath:The path at which to place the copy of srcPath. This path must include the name of the file or directory in its new location. This parameter must not be nil.
        //error:On input, a pointer to an error object. If an error occurs, this pointer is set to an actual error object containing the error information. You may specify nil for this parameter if you do not want the error information.
        BOOL result = [file1 copyItemAtPath:@"/Users/qianfeng/Desktop/test/1.txt" toPath:@"/Users/qianfeng/Desktop/test/dic3/1.txt" error:nil];
        if (result) {
            NSLog(@"拷貝成功");
        }
        else
        {
            NSLog(@"拷貝失敗"); //檔案已經存在時會拷貝失敗
        }
        
        //檔案及目錄移動(剪切或重命名)
        //- (BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error NS_AVAILABLE(10_5, 2_0);
        //srcPath與dstPath路徑對象相同時是重命名的功能,否則是剪切的功能(toPath中要包含目的檔案或目錄名)
        result = [file1 moveItemAtPath:@"/Users/qianfeng/Desktop/test/dic2" toPath:@"/Users/qianfeng/Desktop/test/dic4" error:nil];//将dic2檔案夾重命名為dic4
        //result = [file1 moveItemAtPath:@"/Users/qianfeng/Desktop/test/dic2" toPath:@"/Users/qianfeng/Desktop/testFile/dic4/dic2" error:nil];//将dic2移動到dic4檔案夾中
        if (result) {
            NSLog(@"移動成功");
        }
        else
        {
            NSLog(@"移動失敗");
        }
        //删除檔案及目錄
        //徹底删除檔案,而不是放在廢紙簍中
        //- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error NS_AVAILABLE(10_5, 2_0);
        result = [file1 removeItemAtPath:@"/Users/qianfeng/Desktop/file1/dic3" error:nil];
        if (result) {
            NSLog(@"删除成功");
        }
        else
        {
            NSLog(@"删除失敗");
        }
        
        //判斷檔案或目錄是否存在
        //- (BOOL)fileExistsAtPath:(NSString *)path;
        //path:The path of the file or directory. If path begins with a tilde (~), it must first be expanded with stringByExpandingTildeInPath; otherwise, this method returns NO.
        result = [file1 fileExistsAtPath:@"/Users/qianfeng/Desktop/file1/"];
        if (result) {
            NSLog(@"目錄存在");
        }
        else
        {
            NSLog(@"目錄不存在");
        }
    }
    return 0;
}
           
<pre name="code" class="objc">//
//  main.m
//  ISO150619_ObjectiveC_檔案管理器
//
//  Created by qianfeng on 15/6/19.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import <Foundation/Foundation.h>

//NSFileManager就是一個單例類
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSFileManager *file1 = [NSFileManager defaultManager];
        NSFileManager *file2 = [NSFileManager defaultManager];
        NSFileManager *file3 = [NSFileManager defaultManager];
        NSLog(@"file1 = %p,file2 = %p,file3 = %p",file1,file2,file3);
        //結果:file1 = 0x10010f5b0,file2 = 0x10010f5b0,file3 = 0x10010f5b0
        
        //淺層周遊目錄,隻周遊一級目錄
        //- (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error NS_AVAILABLE(10_5, 2_0);
        //path:目錄的路徑
        //error:打開目錄的時候出錯,會建立一個NSError對象
        NSError *direcError;
        NSArray *direcArray;
        direcArray = [file1 contentsOfDirectoryAtPath:@"/Users/qianfeng/Desktop/test" error:&direcError];
        if(direcArray)
        {
           
           NSLog(@"direcArray = %@",direcArray);//直接列印數組,裡面的中文列印出來都是UTF8編碼,周遊一邊數組可以列印中文
        }
        else
        {
            NSLog(@"error = %@",direcError);
        }

        //擷取檔案的擴充名
        for (NSString *obj in direcArray) {
            NSLog(@"name = %@",[obj pathExtension]);
        }
    
        //深層周遊目錄
        //- (NSArray *)subpathsOfDirectoryAtPath:(NSString *)path error:(NSError **)error NS_AVAILABLE(10_5, 2_0);
        direcError = nil;
        NSArray *direcArray2 = [file2 subpathsOfDirectoryAtPath:@"/Users/qianfeng/Desktop/test" error:&direcError];
        if(direcArray2)
        {
            
            NSLog(@"direcArray2 = %@",direcArray2);//直接列印數組,裡面的中文列印出來都是UTF8編碼,周遊一邊數組可以列印中文
        }
        else
        {
            NSLog(@"error = %@",direcError);
        }
        
    }
    return 0;
}
           

繼續閱讀