【代码笔记】iOS-NSFileManager_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > 【代码笔记】iOS-NSFileManager

【代码笔记】iOS-NSFileManager

 2017/8/1 11:38:56  弦外雨  程序员俱乐部  我要评论(0)
  • 摘要:一,代码。#import"ViewController.h"@interfaceViewController()@end@implementationViewController-(void)viewDidLoad{[superviewDidLoad];//Doanyadditionalsetupafterloadingtheview,typicallyfromanib.//获取应用沙盒根路径[selfdirHome];//获取Documents目录[selfdirDoc]
  • 标签:笔记 file iOS 代码

一,代码。

class="cnblogs_code_copy">复制代码
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //获取应用沙盒根路径
    [self dirHome];
    //获取Documents目录
    [self dirDoc];
    //获取Library目录
    [self dirLib];
    //获取Cache目录
    [self dirCache];
    //创建文件夹
     [self createDir];
    //创建文件
    [self createFile];
    //写数据到文件
    [self writeFile];
    //读文件
    [self readFile];
    //文件属性
    [self fileAttriutes];
    //删除文件
    [self deleteFile];
    
    
    
}
#pragma -mark -funcitons
//获取应用沙盒根路径
-(void)dirHome{
    NSString *dirHome=NSHomeDirectory();
    NSLog(@"应用沙盒根路径: %@",dirHome);
}
//获取Documents目录
-(NSString *)dirDoc{
    //[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSLog(@"Documents目录: %@",documentsDirectory);
    return documentsDirectory;
}
//获取Library目录
-(void)dirLib{
    //[NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *libraryDirectory = [paths objectAtIndex:0];
    NSLog(@"Library目录: %@",libraryDirectory);
}
//获取Cache目录
-(void)dirCache{
    NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachePath = [cacPath objectAtIndex:0];
    NSLog(@"Cache目录: %@",cachePath);
}
//获取Tmp目录
-(void)dirTmp{
    //[NSHomeDirectory() stringByAppendingPathComponent:@"tmp"];
    NSString *tmpDirectory = NSTemporaryDirectory();
    NSLog(@"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");
}



@end
复制代码

 

二,输出。

复制代码
2015-10-23 11:17:54.335 NSFileManager[5578:133206] 应用沙盒根路径: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980
2015-10-23 11:17:54.336 NSFileManager[5578:133206] Documents目录: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980/Documents
2015-10-23 11:17:54.336 NSFileManager[5578:133206] Library目录: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980/Library
2015-10-23 11:17:54.337 NSFileManager[5578:133206] Cache目录: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980/Library/Caches
2015-10-23 11:17:54.337 NSFileManager[5578:133206] Documents目录: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980/Documents
2015-10-23 11:17:54.337 NSFileManager[5578:133206] 文件夹创建成功
2015-10-23 11:17:54.337 NSFileManager[5578:133206] Documents目录: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980/Documents
2015-10-23 11:17:54.349 NSFileManager[5578:133206] 文件创建成功: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980/Documents/test/test.txt
2015-10-23 11:17:54.349 NSFileManager[5578:133206] Documents目录: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980/Documents
2015-10-23 11:17:54.350 NSFileManager[5578:133206] 文件写入成功
2015-10-23 11:17:54.350 NSFileManager[5578:133206] Documents目录: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980/Documents
2015-10-23 11:17:54.351 NSFileManager[5578:133206] 文件读取成功: 测试写入内容!
2015-10-23 11:17:54.351 NSFileManager[5578:133206] Documents目录: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980/Documents
2015-10-23 11:17:54.352 NSFileManager[5578:133206] Key: NSFileOwnerAccountID for value: 501
2015-10-23 11:17:54.352 NSFileManager[5578:133206] Key: NSFileSystemFileNumber for value: 13276863
2015-10-23 11:17:54.353 NSFileManager[5578:133206] Key: NSFileExtensionHidden for value: 0
2015-10-23 11:17:54.353 NSFileManager[5578:133206] Key: NSFileSystemNumber for value: 16777220
2015-10-23 11:17:54.353 NSFileManager[5578:133206] Key: NSFileSize for value: 21
2015-10-23 11:17:54.353 NSFileManager[5578:133206] Key: NSFileGroupOwnerAccountID for value: 80
2015-10-23 11:17:54.353 NSFileManager[5578:133206] Key: NSFilePosixPermissions for value: 420
2015-10-23 11:17:54.355 NSFileManager[5578:133206] Key: NSFileCreationDate for value: 2015-10-23 03:17:54 +0000
2015-10-23 11:17:54.355 NSFileManager[5578:133206] Key: NSFileExtendedAttributes for value: {
    "com.apple.TextEncoding" = <7574662d 383b3133 34323137 393834>;
}
2015-10-23 11:17:54.355 NSFileManager[5578:133206] Key: NSFileType for value: NSFileTypeRegular
2015-10-23 11:17:54.355 NSFileManager[5578:133206] Key: NSFileGroupOwnerAccountName for value: admin
2015-10-23 11:17:54.355 NSFileManager[5578:133206] Key: NSFileReferenceCount for value: 1
2015-10-23 11:17:54.355 NSFileManager[5578:133206] Key: NSFileModificationDate for value: 2015-10-23 03:17:54 +0000
2015-10-23 11:17:54.356 NSFileManager[5578:133206] Documents目录: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980/Documents
2015-10-23 11:17:54.356 NSFileManager[5578:133206] 文件删除成功
2015-10-23 11:17:54.356 NSFileManager[5578:133206] 文件是否存在: NO
复制代码
发表评论
用户名: 匿名