文件操作
// file:///var/containers/Bundle/Application/4256C1D8-84BC-4F4C-A5A2-BCAAF8A83D2C/OTC_heartStick.app/pack_demo.txt
// url讀取是以 file://開(kāi)始的
NSURL *urlDemo = [[NSBundle mainBundle] URLForResource:@"pack_demo.txt" withExtension:nil];
// 沒(méi)有中文情況下可以使用 直接轉(zhuǎn)string類型數(shù)據(jù)
NSString *urlString = [urlDemo absoluteString];
NSLog(@"%@", urlString);
NSString *contentOne = [NSString stringWithContentsOfURL:urlDemo encoding:NSUTF8StringEncoding error:nil];
// 直接以/xxxx/xxx美浦,钠惩,绊寻,请契,開(kāi)始
NSString *path = [[NSBundle mainBundle] pathForResource:@"pack_demo.txt" ofType:nil];
// 獲取內(nèi)容
NSString *contenttwo = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@---%@", contentOne, contenttwo);
// 返回路徑的組成部分 <__NSArrayM 0x100956340>(
// /,
// var,
// containers,
// Bundle,
// Application,
// 0B43ABDC-17AA-4BA3-BF10-B0DC129A2D07,
// OTC_heartStick.app,
// pack_demo.txt
// )
NSArray *patharray = [path pathComponents];
NSLog(@"%@", patharray);
// 返回路徑最后組成部分 txt
NSString *str = [path lastPathComponent];
// 這種方式有一點(diǎn)不好呜舒,就是需要添加/
[path stringByAppendingString:@"/appFile.text"];
// 不需要添加/
[path stringByAppendingPathComponent:@"appFile.text"];
// 刪除最后的組成部分
str = [path stringByDeletingLastPathComponent];
// 刪除擴(kuò)展名(.text被刪除了)
str = [path stringByDeletingPathExtension];
// 獲取擴(kuò)展名
str = [path pathExtension];
// 添加擴(kuò)展名
[path stringByAppendingPathExtension:@".jpg"];
//存儲(chǔ)是有編碼方式的除抛,編程字節(jié)碼
//相當(dāng)于Java中的解碼和編碼
NSString *s = @"tsdfsdfsdfsdf";
NSData *data = [s dataUsingEncoding:NSUTF8StringEncoding];
s = [[NSString alloc] initWithData:data encoding:(NSUTF8StringEncoding)];
//獲取沙盒目錄
NSString *homePath = NSHomeDirectory();
//在沙盒目錄中創(chuàng)建一個(gè)文件file.text
NSString *filePath = [homePath stringByAppendingPathComponent:@"Documents/file.text"];
//NSFileManager是單利模式, 所以不能使用alloc+init創(chuàng)建
NSFileManager *manager = [NSFileManager defaultManager];
NSString *strone = @"無(wú)線互聯(lián)";
NSData *dataone = [strone dataUsingEncoding:NSUTF8StringEncoding];
//參數(shù):文件路徑建丧、文件內(nèi)容恕汇、文件的屬性
BOOL sucess = [manager createFileAtPath:filePath contents:dataone attributes:nil];
if(sucess){
NSLog(@"文件創(chuàng)建成功");
}else{
NSLog(@"文件創(chuàng)建失敗");
}
//創(chuàng)建文件夾
NSString *filePaths = [homePath stringByAppendingPathComponent:@"Documents/file"];
NSError *error;
//需要傳遞一個(gè)創(chuàng)建失敗的指針對(duì)象椰憋,記錄創(chuàng)建失敗的信息
BOOL success1 = [manager createDirectoryAtPath:filePaths withIntermediateDirectories:YES attributes:nil error:&error];
if(!success1){
NSLog(@"創(chuàng)建成功");
}else{
NSLog(@"創(chuàng)建失敗");
}
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:path];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDictionary *fileAttr = [fileManager attributesOfItemAtPath:filePath error:nil];
NSNumber *fileSize = [fileAttr objectForKey:NSFileSize];
long long sizeValue = [fileSize longLongValue];
NSLog(@"%@--%lld", fileSize, sizeValue);
// 讀取到文件后厅克,使用特定支付分割
NSArray *arrayString = [contenttwo componentsSeparatedByString:@"\r\n"];