//----------------------获取沙盒信息-----------------
//获取应用程序根目录
NSString *path1 = NSHomeDirectory();
//获取docment的目录
NSArray *patharr = NSSearchPathForDirectori esInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//取出数组内容
NSString *path2 = [patharr objectAtIndex:0];
NSLog(@"path1:%@\n,patharr = %@\n,path2 =%@\n",path1,patharr,path2);
NSArray *patharr = NSSearchPathForDirectori esInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//取出数组内容
NSString *path2 = [patharr objectAtIndex:0];
NSString *filePath = [path2 stringByAppendingPathCom ponent:@"helloword.txt"];
NSLog(@"filePath=%@",filePath);
//-------------------文件操作 --------------------
//通过NSFileManager 创建文件
//---- 初始化
NSFileManager *fm = [NSFileManager defaultManager];
//---- 创建保存的路径
NSArray *patharr = NSSearchPathForDirectori esInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//取出数组内容
NSString *path = [patharr objectAtIndex:0];
//---- path :/Users/liwei/Documents
//---- 创建文件保存的路径
NSString *filePath = [path stringByAppendingPathCom ponent:@"helloword.txt"];
//---- filepath :/Users/liwei/Documents/helloword.txt
NSString *text = @"我喜欢凤姐5.6,121212";
//---- 定义data
NSData *data = [text dataUsingEncoding:NSUTF8StringEncoding];
//---- 写入文件
BOOL isOK=[fm createFileAtPath:filePath contents:data attributes:nil];
if (isOK) {
NSLog(@"文件创建成功!");
}else{
NSLog(@"失败了!");
}
//-------------创建文件方法2:简单版
NSString *str = @"缺课的人喜欢凤姐!";
[str writeToFile:@"/Users/liwei/Desktop/fengjie.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
//--------------------------创建目录----------------
//withIntermediateDirector ies
// YES 如果文件夹不存在,则创建, 如果存在表示可以覆盖
// NO 如果文件夹不存在,则创建, 如果存在不可以覆盖
NSString *dirPath = @"/Users/liwei/Desktop/test";
BOOL isOK = [fm createDirectoryAtPath:dirPath withIntermediateDirector ies:NO attributes:nil error:nil];
if (isOK) {
NSLog(@"创建成功!");
}else{
NSLog(@"创建失败!");
}
//------------ 读取文件 -------------
//三种方法读取文件,假设读取文件内容为NSString
//1、通过NSData 读取文件
// 1)读取文件到NSData
NSString *filePath=@"/Users/liwei/Desktop/fengjie.txt";
NSData *data2 = [NSData dataWithContentsOfFile:filePath];
// 2)将NSData转换为NSString
NSString *contentStr1 = [[NSString alloc] initWithData:data2 encoding:NSUTF8StringEncoding];
NSLog(@"fileContent------:%@",contentStr1);
//2、根据系统路径,指定文件名的文件读取
// 1)产生文件路径
NSArray *patharr = NSSearchPathForDirectori esInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//取出数组内容
NSString *path = [patharr objectAtIndex:0];
//---- path :/Users/liwei/Documents
//---- 创建文件保存的路径
NSString *filePath2 = [path stringByAppendingPathCom ponent:@"helloword.txt"];
NSLog(@"filePath2 : %@",filePath2);
// 2)读取到NSData
NSData *data3 = [NSData dataWithContentsOfFile:filePath2];
// 3)转换到NSString
NSString *contentStr3 = [[NSString alloc] initWithData:data3 encoding:NSUTF8StringEncoding];
NSLog(@"contentStr3 : %@",contentStr3);
//3、字符串使用文件进行初始化 stringWithContentsOfFile :path
//定义字符串的同时就用文件初始化
NSString *contentStr4 = [NSString stringWithContentsOfFile :@"/Users/liwei/Desktop/test.txt" encoding:NSUTF8StringEncoding error:nil];
NSLog(@"contentStr4 : %@",contentStr4);
// ----------------- 移动文件 -------------
NSString *formPath = @"/Users/liwei/Desktop/test2.txt";
NSString *moveToPath = @"/Users/liwei/Desktop/test3/test.txt";
//创建目录 /Users/liwei/Desktop/test3
[fm createDirectoryAtPath:[moveToPath stringByDeletingLastPath Component] withIntermediateDirector ies:YES attributes:nil error:nil];
NSError *err;
//开始移动文件,并且返回移动结果 YES or NO
BOOL isOK = [fm moveItemAtPath:formPath toPath:moveToPath error:&err];
if(isOK){
NSLog(@"移动文件成功!");
}else{
NSLog(@"移动失败!");
}
//---------------文件复制------------------
NSString *formPath = @"/Users/liwei/Desktop/test3/test.txt";
NSString *copyToPath = @"/Users/liwei/Desktop/备份/test3.txt";
//创建目录 /Users/liwei/Desktop/备份
[fm createDirectoryAtPath:[copyToPath stringByDeletingLastPath Component] withIntermediateDirector ies:YES attributes:nil error:nil];
NSError *err;
//开始复制文件,并且返回移动结果 YES or NO
BOOL isOK = [fm copyItemAtPath:formPath toPath:copyToPath error:&err];
if(isOK){
NSLog(@"复制文件成功!");
}else{
NSLog(@"复制文件失败!");
}
//-----------------删除文件--------------
NSString *deletePath = @"/Users/liwei/Desktop/备份/test3.txt";
//判断要删除的文件是否存在
if ([fm fileExistsAtPath:deletePath]) {
NSLog(@"文件存在!");
//如果存在,则删除文件
if ([fm removeItemAtPath:deletePath error:nil]) {
NSLog(@"文件删除成功!");
}else{
NSLog(@"文件删除失败!");
}
}else{
//提示要删除的文件不存在
NSLog(@"您要删除的文件不存在!");
}
//----------------目录列表--------------
//设定要读取的目录
NSString *userDirPath = @"/Users/liwei/Desktop";//NSHomeDirectory();
//定义枚举对象
NSDirectoryEnumerator *dirEnum = [fm enumeratorAtPath:userDirPath];
//定义变量存储路径
NSString *dirPath =nil;
//遍历目录,打印所有的文件或者子目录名称
while ((dirPath = [dirEnum nextObject])!=nil) {
NSLog(@"list dir: --- %@",dirPath);
}
}
return 0;