文件和文件夹管理
ios是基于OSX,但是在IOS中,应用程序不能看见和访问操作系统的其他文件目录。因为在IOSs中的app。是生存与沙河中
a sandbox environment is exactly what it sound likes: a sanctioned area where only the app that owns the sandbox can access the contents of the folder
只有沙盒的所有者,才可以访问沙盒
every app has its own sandbox folder an the sandbox folder by default hava subfolder that apps can access
每个app都有他自己的沙盒,app可以访问沙盒的文件目录,以及其中子目录。
14.1 查找最常用的目录
NSFileManager *fileManager = [[NSFileManager alloc] init] ;
NSArray *urls = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] ;
if ([ urls count] > 0) {
NSURL *documentsFolder = urls[0] ;
NSLog(@"%@" ,documentsFolder ) ;
} else {
NSLog(@"could not find the documents folder") ;
}
URLsForDirectory 参数
NSLibraryDirectory
NSDocumentDirectory
NSCachesDirectory
inDomains:参数
NSUserDomainMask
//获得tmp目录
NSString *tempDirectory = NSTemporaryDirectory() ;
NSLog(@"tmp Directory=%@",tempDirectory) ;
14.2 读写文件
通过Cocoa类,我们可以保存NSString UIImage NSData到指定的路径
14.2.1 字符串的文件读写
//向文件中保存字符串
NSString *someText =@"Random string that won‘t be back up";
NSString *destinationPath = [NSTemporaryDirectory() stringByAppendingString:@"MyFile.txt"] ;
NSError *error = nil ;
BOOL succeeded = [someText writeToFile:destinationPath atomically:YES encoding:NSUTF8StringEncoding error:&error] ;
if (succeeded) {
NSLog(@"successfully stored the file at %@" ,destinationPath);
//从文件读字符串
NSString *readText = [[NSString alloc]initWithContentsOfFile:destinationPath encoding:NSUTF8StringEncoding error:(nil)];
if([readText length] >0 ) {
NSLog(@"Text read from disk =%@",readText);
}
} else {
NSLog(@"Failed to store the file, error =%@",error) ;
}
14.2.2 数组在文件读写
NSString *filePath_array = [NSTemporaryDirectory() stringByAppendingString:@"MyArray.txt"] ;
NSArray * arrayofName = @[@"steve" , @"jphh" ,@"Edward"] ;
if ([arrayofName writeToFile:filePath_array atomically:YES]) {
NSArray *readArray = [[ NSArray alloc] initWithContentsOfFile:filePath_array] ;
if ([readArray count] == [arrayofName count]) {
NSLog(@"Read the array back from disk just fine") ;
}
else {
NSLog(@"Failed to read the array back from disk ") ;
}
}
else {
NSLog(@"Failed to save the array to disk.") ;
}
}
14.2.3 字典在文件中的序列化
//在文件中读写 字典类型变量
NSString *filePath_dict = [ NSTemporaryDirectory() stringByAppendingString:@"myDict.txt"] ;
NSDictionary *dict = @ {
@"first name" : @"steven" ,
@"middle name" : @"Paul" ,
@"last name" : @"Jobs" ,
} ;
if ([dict writeToFile:filePath_dict atomically:YES ]) {
NSDictionary *readDictionary = [[ NSDictionary alloc] initWithContentsOfFile:filePath_dict] ;
if ([readDictionary isEqualToDictionary:dict]) {
NSLog(@"Read Dict == wirte Dict") ;
NSLog(@"readDictionary==%@",readDictionary) ;
}
else {
NSLog(@"Failed to read the dicitonary from disk") ;
}
}
else {
NSLog(@"Failed to write the dictionary to disk");
}
14.2.4 原生类型的文件的序列号
///原生类型的读写。
NSString *str_raw = [NSTemporaryDirectory() stringByAppendingString:@"MyRaw.tx"] ;
char bytes[4] = {'a','b','v','5'} ;
NSData *dataFromBytes = [[NSData alloc] initWithBytes:bytes length:sizeof(bytes)];
if ([dataFromBytes writeToFile:str_raw atomically:YES]) {
NSData *readData = [[NSData alloc] initWithContentsOfFile:str_raw] ;
if ([readData isEqualToData:dataFromBytes]) {
NSLog(@"The data read is the same data as was write to disk") ;
}
else {
NSLog(@"Failed to read the data from disk. read=%@" ,readData) ;
NSLog(@"Failed to read the data from disk. dataFromBytes=%@" ,dataFromBytes) ;
}
}
else {
NSLog(@"Failed to save the data to disk. %@", str_raw) ;
}
14.3 新建文件夹
//新建文件及
NSFileManager *fileManager2 = [[NSFileManager alloc] init ] ;
NSString *imagesDir = [NSTemporaryDirectory() stringByAppendingString:@"images"] ;
NSError *err=nil ;
if ([fileManager2 createDirectoryAtPath:imagesDir withIntermediateDirectories:YES attributes:nil error:&err]) {
NSLog(@"successfully created the diretory== ") ;
} else {
NSLog(@"Failed to create the directory .Error= %@" ,err) ;
}
withIntermediateDirectories:true-->新建的目录中间目录没有的,一切创建
attributes:-->字典属相,描述文件夹的属性。