ios获取新数据要不要关_iOS开发之数据读写
iOS進階
1:數據處理之數據讀寫
1):獲取當前應用程序的沙盒根目錄
NSString*rootPath = NSHomeDirectory();
NSLog(@"%@",rootPath);
rootPath就是根目錄
然后將打印出來的文件目錄右鍵單擊選擇services下的Reveal In Finder
2):
Documents:存儲持久化文件數據
Library/Caches:存儲緩存數據
Library/Preferences:存儲應用的所有偏好設置
tmp:保存應用運行時所需的臨時數據
3):定位當前應用程序的沙盒根目錄
//1.Documents
NSString*docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSLog(@"%@",docPath);
//2.library
NSString*libPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
NSLog(@"libPath is %@",libPath);
//3.library/Caches
NSString*cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSLog(@"cachesPath is %@",cachesPath);
//4.library/preferences ? 只能用拼接方法定位
NSString*prePath = [libPath stringByAppendingPathComponent:@"Preferences"];
NSLog(@"prePath is %@",prePath);
//5.tmp
NSString*tmpPath = NSTemporaryDirectory();
NSLog(@"tmpPath is %@",tmpPath);
4):存儲應用程序的偏好設置的類:NSUserDefaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults] ;
[defaults setObject:@"tang" forKey:@"name"] ;
//同步
[defaults synchronize] ;
NSLog(@"%@",[defaults objectForKey:@"name"]);
5):簡單對象的讀寫操作
只有四種簡單的數據類型才能直接寫入進文件
NSString NSDictionary NSData NSArray
第一步:獲取沙盒下文件夾Documents的路徑
NSString*docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
第二步:創建需要寫入的文件路徑
NSString*filePath = [docPath stringByAppendingString:@"/text.txt"];
第三步:創建字符串對象
NSString*string = @"is副科級領導蓋房了";
第四步:寫入,四種簡單的數據類型的寫入方法差不多的
[string writeToFile:filePath atomically:YESencoding:NSUTF8StringEncodingerror:nil];
第五步:讀取字符串對象
NSString*resultStr = [NSStringstringWithContentsOfFile:filePath encoding:NSUTF8StringEncodingerror:nil];
數組和字典可參照字符串對象的寫入讀取方法
*NSData對象的寫入和讀取(將圖片存儲)
第一步和第二步是一樣的
第三步:UIImage*image = [UIImageimageNamed:@"0"];
第四步:NSData*data = UIImagePNGRepresentation(image);
第五步:寫入
[data writeToFile:dataPath atomically:YES];
讀取圖片并在模擬器上顯示:
NSData*resultData = [NSDatadataWithContentsOfFile:dataPath];
UIImage*reImage = [UIImageimageWithData:resultData];
UIImageView*imageView = [[UIImageViewalloc] initWithImage:reImage];
imageView.frame= self.view.bounds;
[self.viewaddSubview:imageView];
6):在沙盒目錄下創建文件
第一步:找到沙盒路徑(caches)
//1.獲取Documents目錄
NSString*docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//2.創建文件路徑
NSString*filePath = [docPath stringByAppendingString:@"/baobao.txt"];
//3.創建文件管理對象
NSFileManager*manager = [NSFileManagerdefaultManager];
//4.創建
[manager createFileAtPath:filePath contents:[@"sdfasdfs"dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
NSLog(@"%@",filePath);
//計算文件或文件夾的大小
NSDictionary*dic = [manager attributesOfItemAtPath:filePath error:nil];
NSLog(@"%@",dic);
NSNumber*number = [dic objectForKey:NSFileSize];
NSLog(@"%@",number);
7):創建文件夾和創建文件的核心代碼差別在:
a:文件夾路徑沒有后綴名,文件有
b:文件管理器創建方法的不同
//2.創建文件夾的路徑
NSString*filePath = [cachesPath stringByAppendingString:@"/text"];
//3.創建文件管理器對象
NSFileManager*manager = [NSFileManagerdefaultManager];
[manager createDirectoryAtPath:filePath withIntermediateDirectories:YESattributes:nilerror:nil];
/*
在Documents文件夾下,創建一個文件夾(path),在該文件夾下創建一個文件(test.txt),將一個圖片對象存入到該文件中,然后在Caches文件夾下創建一個文件夾名為"testDirectroy",將test.txt文件復制到這個文件夾下.
*/
- (void)moveFile {
//1.獲取Documents目錄
NSString*docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//2.創建文件夾路徑
NSString*filePath = [docPath stringByAppendingString:@"/path"];
NSFileManager*manager = [NSFileManagerdefaultManager];
[manager createDirectoryAtPath:filePath withIntermediateDirectories:YESattributes:nilerror:nil];
//3.創建文件路徑
NSString*testPath = [filePath stringByAppendingString:@"/test.txt"];
[manager createFileAtPath:testPath contents:[@"aa"dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
//4.寫入圖片
UIImage*image = [UIImageimageNamed:@"0"];
NSData*data = UIImagePNGRepresentation(image);
[data writeToFile:testPath atomically:YES];
//5.caches目錄
NSString*cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
//6.創建文件夾testDirectroy
NSString*directoryPath = [cachesPath stringByAppendingPathComponent:@"testDirectroy"];
[manager createDirectoryAtPath:directoryPath withIntermediateDirectories:YESattributes:nilerror:nil];
//7創建一個和documents中需要復制的文件同名的文件
NSString*desPath = [directoryPath stringByAppendingString:@"/test.txt"];
//8.復制時需要創建文件管理器
[manager copyItemAtPath:testPath toPath:desPath error:nil];
NSLog(@"%@",directoryPath);
}
/*
練習要求:
在Documents目錄下創建一個文件text.txt
從文件的偏移量為3的時候開始追加內容1234
*/
- (void)addContent {
//1.得到Documents目錄
NSString*docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//2.創建文件夾路徑
NSString*filePath = [docPath stringByAppendingPathComponent:@"fiel"];
NSFileManager*manager = [NSFileManagerdefaultManager];
[manager createDirectoryAtPath:filePath withIntermediateDirectories:YESattributes:nilerror:nil];
//3.創建文件路徑
NSString*textPath = [filePath stringByAppendingPathComponent:@"/text.txt"];
[manager createFileAtPath:textPath contents:[@"hahahahahaha"dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
//4.創建文件對接器
NSFileHandle*handle = [NSFileHandlefileHandleForUpdatingAtPath:textPath];
//此時文件在更新狀態下,即可讀也可寫
[handle seekToFileOffset:3];
//5.開始在偏移量為3的地方寫入字符串
[handle writeData:[@"1234"dataUsingEncoding:NSUTF8StringEncoding]];
[handle closeFile];
NSLog(@"%@",textPath);
}
//核心代碼如下
//創建文件對接對象
NSFileHandle*handle = [NSFileHandlefileHandleForUpdatingAtPath:filePath];
//此時的文件對接對象既可以讀也可以寫
//將偏移量移動到3的位置
[handle seekToFileOffset:3];
//寫入數據
[handle writeData:[@"1"dataUsingEncoding:NSUTF8StringEncoding]];
//執行完操作之后不要忘了關閉文件
[handle closeFile];
8):復雜文件的歸檔和反歸檔(持久化操作)
//歸檔
- (void)archiver {
//1.創建person對象
Person*p1 = [[Personalloc] initWithName:@"tangxi"age:@"18"];
Person*p2 = [[Personalloc] initWithName:@"aren"age:@"20"];
//2.獲取Documents目錄
NSString*docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString*filePath = [docPath stringByAppendingString:@"/haha.txt"];
//3.創建可變數據對象
NSMutableData*data = [NSMutableDatadata];
//4.創建歸檔類的對象
NSKeyedArchiver*archiver = [[NSKeyedArchiveralloc] initForWritingWithMutableData:data];
//5.將person對象歸檔
[archiver encodeObject:p1 forKey:@"person1"];
[archiver encodeObject:p2 forKey:@"person2"];
//6.結束歸檔
[archiver finishEncoding];//此時不管有幾個對象沒有被歸檔都會停止歸檔了
//7.將可變數據data寫入創建的文件
[data writeToFile:filePath atomically:YES];
NSLog(@"%@",filePath);
}
//反歸檔
- (void)unarchiver {
//1.獲取Documents目錄
NSString*documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString*filePath = [documents stringByAppendingPathComponent:@"haha.txt"];
NSData*data = [NSDatadataWithContentsOfFile:filePath];
NSKeyedUnarchiver*unarchiver = [[NSKeyedUnarchiveralloc] initForReadingWithData:data];
Person*person = [unarchiver decodeObjectForKey:@"person1"];
NSLog(@"name is %@, age is %@",person.name,person.age);
[unarchiver finishDecoding];
}
//還要創建一個模型對象Person,遵循NSCoding協議
實現兩個協議方法:
- (instancetype)initWithName:(NSString*)name age:(NSString*)age {
self= [superinit];
if(self) {
self.name= name;
self.age= age;
}
returnself;
}
- (void)encodeWithCoder:(NSCoder*)aCoder {
[aCoder encodeObject:self.nameforKey:@"name"];
[aCoder encodeObject:self.ageforKey:@"age"];
}
- (nullableinstancetype)initWithCoder:(NSCoder*)aDecoder {
NSString*name = [aDecoder decodeObjectForKey:@"name"];
NSString*age = [aDecoder decodeObjectForKey:@"age"];
return[selfinitWithName:name age:age];
}
//4.寫入圖片
UIImage*image = [UIImageimageNamed:@"0"];
NSData*data = UIImagePNGRepresentation(image);
[data writeToFile:testPath atomically:YES];
//4.創建文件對接器
NSFileHandle*handle = [NSFileHandlefileHandleForUpdatingAtPath:textPath];
//此時文件在更新狀態下,即可讀也可寫
[handle seekToFileOffset:3];
//5.開始在偏移量為3的地方寫入字符串
[handle writeData:[@"1234"dataUsingEncoding:NSUTF8StringEncoding]];
[handle closeFile];
NSLog(@"%@",textPath);
總結
以上是生活随笔為你收集整理的ios获取新数据要不要关_iOS开发之数据读写的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: “幽独抵归山”下一句是什么
- 下一篇: swing 圆角按钮_JFrame实现圆