#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
//在oc里面对文件路径操作需要NSString
NSString *homePath = NSHomeDirectory();
NSLog(@"%@",homePath);
NSDate *date = [NSDate date];
NSLog(@"%@",date);
NSDateFormatter *formater = [[NSDateFormatter alloc]init];
[formater setDateFormat:@"yyyy年MM月dd日 hh点mm分ss秒\n"];
NSString * okname = [formater stringFromDate:date];
NSString *desktopPath = @"/Users/feifanchengxuyuan1/Desktop";
NSArray *arrayCom = [desktopPath pathComponents];
NSLog(@"%@",arrayCom);
[desktopPath lastPathComponent];
NSLog(@"追加路径:%@", [desktopPath stringByAppendingPathComponent:@"test.txt"]);
//文件管理
NSFileManager *fileManger = [NSFileManager defaultManager];
NSString *appendCon = @"alice\n";
NSData *appendData = [appendCon dataUsingEncoding:NSUTF8StringEncoding];
if ([fileManger createFileAtPath:[desktopPath stringByAppendingPathComponent:@"createFile.txt"] contents:appendData attributes:nil]) {
NSLog(@"文件创建成功!");
}
NSData *contentData = [fileManger contentsAtPath:[desktopPath stringByAppendingPathComponent:@"createFile.txt"]];
// NSLog(@"===%@",contentData);
NSString *contentStr = [[NSString alloc]initWithData:contentData encoding:NSUTF8StringEncoding];
NSLog(@"===%@",contentStr);
//复制文件
NSString *copyPath = @"/Users/feifanchengxuyuan1/Desktop/file/createFile.txt";
[fileManger copyItemAtPath:[desktopPath stringByAppendingPathComponent:@"createFile.txt"] toPath:copyPath error:nil];
//删除文件
[fileManger removeItemAtPath:copyPath error:nil];
//创建目录
if( [fileManger createDirectoryAtPath:@"/Users/feifanchengxuyuan1/Desktop2/fileTest" withIntermediateDirectories:NO attributes:nil error:nil]){
NSLog(@"目录创建成功");
}
else{
NSLog(@"目录创建失败");
}
NSLog(@"%@", [fileManger contentsOfDirectoryAtPath:desktopPath error:nil]);
// 深度遍历目录路径
// NSLog(@"%@", [fileManger subpathsOfDirectoryAtPath:desktopPath error:nil]);
NSFileHandle *filehandleI = nil;
[NSFileHandle fileHandleForReadingAtPath:[desktopPath stringByAppendingPathComponent:@"createFile.txt"]];
// NSFileHandle *fileHandle = [[NSFileHandle alloc]init];
// filehandleI = [NSFileHandle fileHandleForUpdatingAtPath:[desktopPath stringByAppendingPathComponent:@"createFile.txt"]];
[filehandleI seekToEndOfFile];
NSString *appendContent = @"hilary\n";
NSData *appeddData = [appendContent dataUsingEncoding:NSUTF8StringEncoding];
[filehandleI writeData:appeddData];
[filehandleI closeFile];
}
return 0;
}