输出结果
#import "ViewController.h"
#define PATH @"/Users/user/Desktop"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSFileManager* fm = [NSFileManager defaultManager];
[fm createFileAtPath:[NSString stringWithFormat:@"%@/%@", PATH, @"1.txt"] contents:nil attributes:nil];
[fm createDirectoryAtPath:[NSString stringWithFormat:@"%@/%@", PATH, @"1"] withIntermediateDirectories:YES attributes:nil error:nil];
BOOL have = [fm fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", PATH, @"1.txt"]];
NSLog(@"判断文件是否存在 == %d", have);
BOOL isDir = YES;
BOOL have1 = [fm fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", PATH, @"1"] isDirectory:&isDir];
NSLog(@"判断文件夹是否存在 == %d", have1);
NSString* str = @"此内容于(2017/04/17)写入 写入人:天才刘";
NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
[data writeToFile:[NSString stringWithFormat:@"%@/%@", PATH, @"1.txt"] atomically:YES];
[fm copyItemAtPath:[NSString stringWithFormat:@"%@/%@", PATH, @"1.txt"] toPath:[NSString stringWithFormat:@"/Users/user/Downloads/%@",@"2.txt"] error:nil];
[fm moveItemAtPath:[NSString stringWithFormat:@"%@/%@", PATH, @"1.txt"] toPath:[NSString stringWithFormat:@"/Users/user/Downloads/%@",@"1.txt"] error:nil];
NSData* data1 = [NSData dataWithContentsOfFile:[NSString stringWithFormat:@"/Users/user/Downloads/%@",@"1.txt"]];
NSString* str1 = [[NSString alloc]initWithData:data1 encoding:NSUTF8StringEncoding];
NSLog(@"文件内容 == %@", str1);
[fm removeItemAtPath:[NSString stringWithFormat:@"/Users/user/Downloads/%@",@"1.txt"] error:nil];
NSLog(@"深度遍历 == %@", [fm subpathsAtPath:PATH]);
NSLog(@"浅度遍历 == %@", [fm contentsOfDirectoryAtPath:PATH error:nil]);
NSFileHandle* fh = [NSFileHandle fileHandleForReadingAtPath:[NSString stringWithFormat:@"/Users/user/Downloads/%@",@"2.txt"]];
NSData* data3 = [fh readDataOfLength:6];
NSString* str2 = [[NSString alloc]initWithData:data3 encoding:NSUTF8StringEncoding];
NSLog(@"读取6个长度的文本内容为 == %@", str2);
[fh seekToFileOffset:6];
[fh seekToEndOfFile];
NSFileHandle* fh1 = [NSFileHandle fileHandleForWritingAtPath:[NSString stringWithFormat:@"/Users/user/Downloads/%@",@"2.txt"]];
[fh1 seekToEndOfFile];
[fh1 writeData:data3];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end