//
// main.m
// fileManager
//
// Created by MAC on 15/12/26.
// Copyright © 2015年 MAC. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
//实例化它自己 单例设计模式
NSFileManager *fm = [NSFileManager defaultManager];
//获得文件的目录 两个都是作用域
NSArray *array = [fm URLsForDirectory:NSDesktopDirectory inDomains:NSUserDomainMask];
NSLog(@"%@",array);
//判断文件是否存在
NSString *filePath = @"/tmp/test.txt";
BOOL r = [fm fileExistsAtPath:filePath];
NSLog(@"%@",r==1?@"YES":@"NO");
//在某个路径下创建一个文件
NSString *filePath1 = @"/tmp/test100000.txt";
NSString *content = @"hello world";
NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];
BOOL r1 = [fm createFileAtPath:filePath1 contents:data attributes:nil];
NSLog(@"%@",r1==1?@"YES":@"NO");
//删除文件
BOOL r2 = [fm removeItemAtPath:filePath1 error:nil];
NSLog(@"%@",r2==1?@"YES":@"NO");
//文件读写
NSString *str = @"asdzxc";
[str writeToFile:filePath1 atomically:YES encoding:NSUTF8StringEncoding error:nil];
str = [NSString stringWithContentsOfFile:filePath1 encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",str);
//拷贝文件 调用fm里的方法
NSString *filePath2 = @"/tmp/test2.txt";
BOOL r3= [fm copyItemAtPath:filePath1 toPath:filePath2 error:nil];
NSLog(@"%@",r3==1?@"YES":@"NO");
//重命名(移动)
[fm moveItemAtPath:filePath2 toPath:filePath1 error:nil];
//获得文件属性
NSDictionary *dict=[fm attributesOfItemAtPath:filePath error:nil];
for (NSString *key in dict) {
NSString *value = [dict objectForKey:key];
NSLog(@"%@,%@",key,value);
}
//- ----对目录的操作
//创建目录
NSString *dir = @"/tmp/wjr";
BOOL r4=[fm createDirectoryAtPath:dir withIntermediateDirectories:YES attributes:nil error:nil];
NSLog(@"%@",r4==1?@"YES":@"NO");
//在桌面创建目录
array = [fm URLsForDirectory:NSDesktopDirectory inDomains:NSUserDomainMask];
if (array.count>0) {
NSURL *url = [array objectAtIndex:0];
NSLog(@"------%@",url);
str = url.path;
NSLog(@"%@",str);
str = [str stringByAppendingString:@"/abc"];
NSLog(@"%@",str);
[fm createDirectoryAtPath:str withIntermediateDirectories:YES attributes:nil error:nil];
NSLog(@"%@",r4==1?@"YES":@"NO");
}
//删除目录
r4 = [fm removeItemAtPath:dir error:nil];
NSLog(@"%@",r4==1?@"YES":@"NO");
//获得当前文件目录路径
str = fm.currentDirectoryPath;
NSLog(@"%@",str);
//目录的遍历
NSString *path = @"/";
array = [fm contentsOfDirectoryAtPath:path error:nil];
NSLog(@"%@",array);
}
return 0;
}