#import <Foundation/Foundation.h>app
int main(int argc, const char * argv[]) {spa
@autoreleasepool {.net
//在oc裏面對文件路徑操做須要NSStringorm
NSString *homePath = NSHomeDirectory();rem
NSLog(@"%@",homePath);get
NSDate *date = [NSDate date];string
NSLog(@"%@",date);it
NSDateFormatter *formater = [[NSDateFormatter alloc]init];io
[formater setDateFormat:@"yyyy年MM月dd日 hh點mm分ss秒\n"];ast
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;
}