文件 操作
来源:互联网 发布:效果图网络兼职 编辑:程序博客网 时间:2024/05/20 14:23
// 文件管理器 简单单例模式 // NSFileManager *fileManagr1 = [[NSFileManager alloc]init]; NSFileManager *fileManager = [NSFileManager defaultManager]; // Returns the shared file manager object for the process. // This method always returns the same file manager object. // defaultManager 总是会返回一个相同的文件管理器对象// If you plan to use a delegate with the file manager to receive notifications about the completion of file-based operations, // 如果你计划去用 文件管理器的代理 去接收 通知 来帮助 文件管理器 完成文件基本操作// you should create a new instance of NSFileManager (using the init method) rather than using the shared object. // 你应该创建一个实例 而不是一个单例 // 创建一个单例对象,单例对象的生命周期是从函数的开始到函数的结束 User *user = [User defaultUser]; User *user2 = [User defaultUser]; NSLog(@"%p %p",user,user2); user.age = 21; NSLog(@"user1 age %d user2 age %d ",user.age,user2.age); // 创建空文件 [fileManager createFileAtPath:@"/Users/ms/Desktop/oc 总结/总结- day5.txt" contents:nil attributes:nil]; // 获取文件的属性 /* <1.首先需要创建一个文件 */ NSString *string = @"Hello World!"; NSString *path = @"/Users/ms/Desktop/文件操作/file.txt"; NSError *error; [string writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error]; if (error == nil) { NSLog(@"创建成功"); } else { NSLog(@"创建失败"); } NSFileManager *fileManager = [NSFileManager defaultManager]; // <2.通过文件管理器来获取属性 NSDictionary *dic = [fileManager attributesOfItemAtPath:path error:&error]; NSLog(@"dic = %@",dic); // NSString * const NSFileSize; 指向常量的指针 // 文件大小 NSString *fileSize = dic[NSFileSize]; NSLog(@"fileSize = %@",fileSize); // NSString * const NSFileCreationDate; // 创建时间 NSString *createTime = dic[NSFileCreationDate]; NSLog(@"createTime = %@",createTime); /* dic = { NSFileCreationDate = "2015-07-21 06:45:57 +0000"; NSFileExtendedAttributes = { "com.apple.TextEncoding" = <7574662d 383b3133 34323137 393834>; }; NSFileExtensionHidden = 0; NSFileGroupOwnerAccountID = 20; NSFileGroupOwnerAccountName = staff; NSFileHFSCreatorCode = 0; NSFileHFSTypeCode = 0; NSFileModificationDate = "2015-07-21 06:45:57 +0000"; NSFileOwnerAccountID = 501; NSFileOwnerAccountName = ms; NSFilePosixPermissions = 420; NSFileReferenceCount = 1; NSFileSize = 12; NSFileSystemFileNumber = 1026748; NSFileSystemNumber = 16777218; NSFileType = NSFileTypeRegular; } */ NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *path = @"/Users/ms/Desktop/文件操作"; NSError *error; // 1.获取指定路径下的文件夹 一级目录的文件// This method performs a shallow search of the directory // 目录下浅搜索 NSArray *array = [fileManager contentsOfDirectoryAtPath:path error:&error]; NSLog(@"array = %@",array); // 2.获取指定路径文件夹下所有目录的文件 NSArray *array1 = [fileManager subpathsOfDirectoryAtPath:path error:&error];// Performs a deep enumeration of the specified directory and returns the paths of all of the contained subdirectories. // 目录下深度遍历 NSLog(@"%@",array1); NSFileManager *fileManager = [NSFileManager defaultManager]; // 1.创建目录 /* NSError *error ; [fileManager createDirectoryAtPath:@"/Users/ms/Desktop/文件操作2/Charles1/HelloWorld" withIntermediateDirectories:YES attributes:nil error:&error]; // If YES, this method creates any non-existent parent directories as part of creating the directory in path. If NO, this method fails if any of the intermediate parent directories does not exist. This method also fails if any of the intermediate path elements corresponds to a file and not a directory. // 第二个参数,如果是YES 就能创建任意的不存在的父目录,如果NO,这个方法会失败(如果中间的父目录不存在的话),这个方法同样会失败(如果父目录是一个文件而不是一个目录的话) if (error == nil) { NSLog(@"创建成功"); } else { NSLog(@"创建失败"); } */ // 2.移动目录,剪切目录 /* [fileManager moveItemAtPath: @"/Users/ms/Desktop/文件操作2/Charles1/HelloWorld" toPath:@"/Users/ms/Desktop/文件操作2/Charles1/HelloWorld2/new1" error:&error];// If an item with the same name already exists at dstPath, this method aborts the move attempt and returns an appropriate error. // 如果目标目录中已经有了一个重名的item,那么在它移动尝试的时候就会报错 if (error == nil) { NSLog(@"移动成功"); } else { NSLog(@"移动失败"); } */ // 3.复制目录 NSError *error1 ; [fileManager copyItemAtPath:@"/Users/ms/Desktop/文件操作2/Charles1/HelloWorld" toPath:@"/Users/ms/Desktop/文件操作2/Charles1/newHelloWorld2" error:&error1]; if (error1 == nil) { NSLog(@"复制成功"); } else { NSLog(@"复制失败"); } // 4.删除目录 NSError *error3 ; [fileManager removeItemAtPath:@"/Users/ms/Desktop/文件操作2/Charles1/newHelloWorld2" error:&error1]; if (error1 == nil) { NSLog(@"删除成功"); } else { NSLog(@"删除失败"); }
0 0
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- git的实际操作用法
- php后台管理员权限相关表结构
- poj 1251 Jungle Roads(最小生成树)(基础)
- Ngui随Panel移动的滑动条,滑动条长度可变可不变
- logback使用
- 文件 操作
- DB2 表无法访问 也无法删除
- hibernate连接mysql所需的jar包
- How HashMap works in Java
- shell编程
- 1154 能量项链
- iOS企业开发In House ipa发布流程
- docker的原理和类比
- Undefined symbols for architecture i386:和"_OBJC_CLASS_$_xx", referenced from:问题解决方法