#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
//------文件名处理----------常用NSString提供操作文件路径名方法
// 返回路径的组成部分
NSString *path = @"/Users/yubo/text.txt";
NSArray *array = [path pathComponents];
NSLog(@"array--%@",array);
// 返回最后组成的部分---文件的名称
NSString *str = [path lastPathComponent];
NSLog(@"str-->%@",str);
// 追加
path = [path stringByAppendingPathComponent:@"maizi"];
NSLog(@"%@",path);
//-------文件数据源处理 NSData, 数据进行编码和解码处理-----------
NSString *msg = @"maizi_today";
NSData *data = [msg dataUsingEncoding:NSUTF8StringEncoding];
msg = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"msg-->%@",msg);
//--------- 开始文件真正的操作方法。
// Documents:存放长期使用的文件(一般存放目录)
// Library:系统存放文件
// tmp:临时文件,app重启时,该目录下得文件清空
NSArray *documentArrary = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true);
NSLog(@"%@",documentArrary);
NSArray *libArray = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, true);
NSLog(@"%@",libArray);
// 文件操作类处理---------------------NSFileManager
// 创建文件
NSString *homePath = NSHomeDirectory();
NSLog(@"%@",homePath);
homePath = [homePath stringByAppendingPathComponent:@"Document/file_maizi.txt"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *fileContent = @"码农的自我修养";
NSData *fileData = [fileContent dataUsingEncoding:NSUTF8StringEncoding];
bool flag = [fileManager createFileAtPath:homePath contents:fileData attributes:nil];
if (flag) {
NSLog(@"文件创建成功");
}else{
NSLog(@"文件创建失败");
}
// 创建文件
NSString *filePaths = NSHomeDirectory();
filePaths = [filePaths stringByAppendingPathComponent:@"maiziTest/maiziFile"];
NSError *error;
NSFileManager *manager = [NSFileManager defaultManager];
bool fileFlag = [manager createDirectoryAtPath:filePaths withIntermediateDirectories:true attributes:nil error:&error];
if (fileFlag) {
NSLog(@"文件创建成功");
}
// 读取文件
NSData *datas = [manager contentsAtPath:homePath];
NSString *dataMsg = [[NSString alloc]initWithData:datas encoding:NSUTF8StringEncoding];
NSLog(@"文件内容--->%@",dataMsg);
// 移动文件
bool moveFlag = [manager moveItemAtPath:homePath toPath:filePaths error:&error];
if (moveFlag) {
NSLog(@"文件移动成功");
}else{
NSLog(@"%@",error.description);
}
// 复制文件
bool copyFlag = [manager copyItemAtPath:homePath toPath:filePaths error:&error];
if (copyFlag) {
NSLog(@"复制文件成功");
}else{
NSLog(@"error is %@",error.description);
}
// 删除文件
// bool exist = [manager fileExistsAtPath:homePath];
// if (exist) {
//
// bool removeFlag = [manager removeItemAtPath:homePath error:&error];
// if (removeFlag) {
// NSLog(@"删除成功");
// }
// }
// 查看文件属性
NSDictionary *dic = [manager attributesOfItemAtPath:filePaths error:nil];
NSLog(@"%@",dic);
//-------------- NSFileHandle 文件的读写操作
// 字符串直接写入文件
NSString *strA = @"------我是麦子";
bool writerSuccess = [strA writeToFile:homePath atomically:NO encoding:NSUTF8StringEncoding error:nil];
if (writerSuccess) {
NSLog(@"文件写入成功");
}
NSString *contentA = [NSString stringWithContentsOfFile:homePath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",contentA);
// NSdata 直接读写
NSData *dataA = [NSData dataWithContentsOfFile:homePath];
NSString *strB = [[NSString alloc]initWithData:dataA encoding:NSUTF8StringEncoding];
NSLog(@"---->%@",strB);
NSString *strC = @"我是Data数据,你是哪一位";
NSData *dataC = [strC dataUsingEncoding:NSUTF8StringEncoding];
bool dataSuccess = [dataC writeToFile:homePath atomically:NO];
dataA = [NSData dataWithContentsOfFile:homePath];
NSString *newWord = [[NSString alloc]initWithData:dataA encoding:NSUTF8StringEncoding];
NSLog(@"-data直接获取数据--->%@",newWord);
// NSArray 直接写入文件----------这种直接写入的,会默认给你创建文件,但是不会给你创建文件夹。
NSArray *dataArray = @[@"android",@"ios"];
NSString *arrayFile = NSHomeDirectory();
arrayFile = [arrayFile stringByAppendingPathComponent:@"Documents/array.plist"];
bool success2 = [dataArray writeToFile:arrayFile atomically:true];
if (success2) {
NSLog(@"写入成功");
}else{
NSLog(@"写入失败");
}
// dictory 字典写入文件
NSDictionary *fileDic = @{ @"name" : @"maizi_today",
@"age" : @"27"
};
NSString *dicFile = NSHomeDirectory();
dicFile = [dicFile stringByAppendingPathComponent:@"Document/dicFile.plist"];
bool dicSuccess = [fileDic writeToFile:dicFile atomically:true];
if (dicSuccess) {
NSLog(@"字典保存成功");
}else{
NSLog(@"字典保存失败");
}
// 文件进行处理,内容文件追加处理。
NSString *strD = @"无线互联";
NSString *fileD = NSHomeDirectory();
fileD = [fileD stringByAppendingPathComponent:@"hahaha.txt"];
bool successD = [strD writeToFile:fileD atomically:true encoding:NSUTF8StringEncoding error:nil];
if (successD) {
NSLog(@"写入成功");
}
// 追加--写
NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:fileD];
[handle seekToEndOfFile];
NSString *msgD = @".......我是新加入的字符串.......";
NSData *dataD = [msgD dataUsingEncoding:NSUTF8StringEncoding];
[handle writeData:dataD];
// 记得关闭文件。
[handle closeFile];
NSString *newMsg = [NSString stringWithContentsOfFile:fileD encoding:NSUTF8StringEncoding error:nil];
NSLog(@"newMsg--->%@",newMsg);
}
return 0;
}
iOS 简单文件操作
最新推荐文章于 2021-09-07 23:52:13 发布