iOS中的NSFileManager

2018-09-20  本文已影响0人  皆为序幕_

NSFileManager

NSFileManager常用方法

NSFileManager *file = [NSFileManager defaultManager];
BOOL flag = [file fileExistsAtPath:@"/Users/soso/Desktop/demo1/NSFileManager/string.txt"];
//该方法的返回值是说明传入的路径对应的文件或文件夹是否存在
//第二个参数是用于保存判断的结果,如果是一个目录,那么就是赋值为YES,若不是就是NO
BOOL dir = NO;
BOOL flag = [file fileExistsAtPath:@"/Users/user/Desktop/test2/test2/string.txt" isDirectory:&dir];
NSDictionary *dic = [file attributesOfItemAtPath:@"/Users/soso/Desktop/demo1/NSFileManager/string.txt" error:&error];
NSLog(@"dic = %@",dic);
/*
dic = {
    NSFileCreationDate = "2018-09-20 06:58:59 +0000";
    NSFileExtendedAttributes =     {
        "com.apple.TextEncoding" = <7574662d 383b3133 34323137 393834>;
        "com.apple.lastuseddate#PS" = <5945a35b 00000000 83a9ea21 00000000>;
        "com.apple.metadata:_kMDItemUserTags" = <62706c69 73743030 a0080000 00000000 01010000 00000000 00010000 00000000 00000000 00000000 0009>;
        "com.apple.metadata:kMDLabel_irw3ijioz5o2ngwrmpxndxxi5i" = <f23d67c8 34efdd1f abea9f4d 5a0ef354 bca6ab5f f25ae881 59beea49 1849408e f8f0aa29 27ffceb4 2366c613 0b4882fb 09dd795b f22c927d e6279594 572e9f28 20b48fab 7166d56f ca83d350 e29e9d02 02237c35 4d80198f b3>;
    };
    NSFileExtensionHidden = 0;
    NSFileGroupOwnerAccountID = 20;
    NSFileGroupOwnerAccountName = staff;
    NSFileModificationDate = "2018-09-20 06:59:39 +0000";
    NSFileOwnerAccountID = 501;
    NSFilePosixPermissions = 420;
    NSFileReferenceCount = 1;
    NSFileSize = 2;
    NSFileSystemFileNumber = 8980313;
    NSFileSystemNumber = 16777222;
    NSFileType = NSFileTypeRegular;
}
*/
NSArray *array = [file contentsOfDirectoryAtPath:@"/Users/soso/Desktop/demo1/NSFileManager" error:&error];
NSLog(@"array = %@",array);
/*
array = (
    "AppDelegate.h",
    "Assets.xcassets",
    "ViewController.m",
    "Base.lproj",
    "main.m",
    "AppDelegate.m",
    "Info.plist",
    "ViewController.h",
    "string.txt"
)
*/
NSArray *array = [file subpathsAtPath:@"/Users/soso/Desktop/demo1/NSFileManager"];
NSLog(@"array = %@",array1);
/*
array = (
    "AppDelegate.h",
    "Assets.xcassets",
    "Assets.xcassets/AppIcon.appiconset",
    "Assets.xcassets/AppIcon.appiconset/Contents.json",
    "Assets.xcassets/Contents.json",
    "ViewController.m",
    "Base.lproj",
    "Base.lproj/LaunchScreen.storyboard",
    "Base.lproj/Main.storyboard",
    "main.m",
    "AppDelegate.m",
    "Info.plist",
    "ViewController.h",
    "string.txt"
)
*/
NSArray *array2 = [file subpathsOfDirectoryAtPath:@"/Users/soso/Desktop/demo1/NSFileManager" error:&error];
/*
createDirectoryAtPath:创建文件夹到什么地方
withIntermediateDirectories:如果指定的文件中有一些文件夹不存在,是否自动创建不存在的文件夹,如果是YES,要在该路径下创建Text,若路径中没有NSFileManager这个文件夹,系统会自动创建,若为NO,则不会创建成功
 attributes:创建出来为文件夹的属性
error:是否创建成功
注意:该方法只能用于创建文件夹,不能用于创建文件,路径最后的test就是新创建的文件夹
*/
BOOL flag = [file createDirectoryAtPath:@"/Users/soso/Desktop/demo1/NSFileManager/Text" withIntermediateDirectories:YES attributes:nil error:&error];
NSString *str = @"hello world";
NSData * data = [str dataUsingEncoding:NSUTF8StringEncoding];
/*
  createFileAtPath:指定存储文件的路径
  contents:文件内容data
  attributes:属性
*/
BOOL flag3 = [file createFileAtPath:@"/Users/soso/Desktop/demo1/NSFileManager/test.txt" contents:data attributes:nil];

上一篇 下一篇

猜你喜欢

热点阅读