ios学习路线—ios沙盒(sandbox)机制和文件操作(三)

1.在Documents里创建目录

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];  
NSLog(@"documentsDirectory%@",documentsDirectory); 
NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSString *testDirectory = [documentsDirectory stringByAppendingPathComponent:@"test"];
//创建目录  
[fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];  

2.在test目录下创建文件

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    
NSString *documentsDirectory = [paths objectAtIndex:0];    
NSLog(@"documentsDirectory%@",documentsDirectory);    
NSFileManager *fileManager = [NSFileManager defaultManager];    
NSString *testDirectory = [documentsDirectory stringByAppendingPathComponent:@"test"]; 
NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test00.txt"];
NSString *string = @"写入内容,write String";  
[fileManager createFileAtPath:testPath contents:[string  dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];

3.获取目录列里所有文件名

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    
NSString *documentsDirectory = [paths objectAtIndex:0];    
NSLog(@"documentsDirectory%@",documentsDirectory);    
NSFileManager *fileManage = [NSFileManager defaultManager];    
NSString *myDirectory = [documentsDirectory stringByAppendingPathComponent:@"test"];    
NSArray *file = [fileManage subpathsOfDirectoryAtPath: myDirectory error:nil];   
NSLog(@"%@",file);    
NSArray *files = [fileManage subpathsAtPath: myDirectory ];   
NSLog(@"%@",files); 

4.fileManager使用操作当前目录

//创建文件管理器 
NSFileManager *fileManager = [NSFileManager defaultManager];  
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
NSString *documentsDirectory = [paths objectAtIndex:0];
//更改到待操作的目录下
[fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];
//创建文件fileName文件名称,contents文件的内容,如果开始没有内容可以设置为nil,attributes文件的属性,初始为nil  
NSString * fileName = @"testFileNSFileManager.txt";  
NSArray *array = [[NSArray alloc] initWithObjects:@"hello world",@"hello world1", @"hello world2",nil];  
[fileManager createFileAtPath:fileName contents:array attributes:nil];

5.删除文件

[fileManager removeItemAtPath:fileName error:nil];

6.混合数据的读写
6.1.写入数据

NSString * fileName = @"testFileNSFileManager.txt";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
NSString *documentsDirectory = [paths objectAtIndex:0];
//获取文件路径  
NSString *path = [documentsDirectory stringByAppendingPathComponent:fileName];
//待写入的数据  
NSString *temp = @"nihao 世界";  
int dataInt = 1234;  
float dataFloat = 3.14f;  
//创建数据缓冲  
NSMutableData *writer = [[NSMutableData alloc] init];  
//将字符串添加到缓冲中  
[writer appendData:[temp dataUsingEncoding:NSUTF8StringEncoding]];     
//将其他数据添加到缓冲中  
[writer appendBytes:&dataInt length:sizeof(dataInt)];  
[writer appendBytes:&dataFloat length:sizeof(dataFloat)];    
//将缓冲的数据写入到文件中  
[writer writeToFile:path atomically:YES]; 

6.2.读取刚才写入的数据

//读取数据
int intData;  
float floatData = 0.0;  
NSString *stringData;

NSData *reader = [NSData dataWithContentsOfFile:path];  
stringData = [[NSString alloc] initWithData:[reader subdataWithRange:NSMakeRange(0, [temp length])]  
                                  encoding:NSUTF8StringEncoding];  
[reader getBytes:&intData range:NSMakeRange([temp length], sizeof(intData))];  
[reader getBytes:&floatData range:NSMakeRange([temp length] + sizeof(intData), sizeof(floatData))];  
NSLog(@"stringData:%@ intData:%d floatData:%f", stringData, intData, floatData);  
阅读更多
个人分类: iOS
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭