//
// 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;
}