国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

IOS UI學習教程之區分NSBundle和NSURL(讀取文件、寫入文件)

2020-07-26 03:23:43
字體:
來源:轉載
供稿:網友

本文實例為大家區分NSBundle和NSURL,具體實現內容如下

在項目的工程中添加一個文件,本例程添加的是aa.txt,文件的內容為百度: www.baidu.com,現在要使用NSBundle和NSURL分別去獲取內容,代碼如下:

//  讀取文件內容//  方法1:按照文件路徑讀取  NSString *pathBundle = [[NSBundle mainBundle]pathForResource:@"aa" ofType:@"txt"];  NSString *outstringbundle = [NSString stringWithContentsOfFile:pathBundle encoding:NSUTF8StringEncoding error:nil];  //  方法2:按照URL讀取  NSURL *pathUrl = [[NSBundle mainBundle]URLForResource:@"aa" withExtension:@"txt" subdirectory:nil];  NSString *outstringUrl = [NSString stringWithContentsOfURL:pathUrl encoding:NSUTF8StringEncoding error:nil];    NSLog(@"%@/n/////////n%@",outstringbundle,outstringUrl);

輸出結果如下:

2016-03-30 14:48:02.939 沙盒機制and文件路徑[11786:518929] 百度: www.baidu.com //////// 百度: www.baidu.com

寫入文件:

先新建一個文件:

NSString *newPath = [NSString stringWithFormat:@"%@/Documents/New",NSHomeDirectory()]; //  先把文件路徑和文件名定義好   NSString *newfile = [NSString stringWithFormat:@"%@/new.mp3",newPath]; //  使用createFileAtPath創建文件   [[NSFileManager defaultManager]createFileAtPath:newfile contents:nil attributes:nil];   NSLog(@"%@",newPath);

在讀取并寫入:

//  寫入文件//  1、先用data讀取數據  NSData *data = [[NSData alloc]initWithContentsOfFile:pathBundle];  NSLog(@"%@",data);  //  2、把讀取的data寫入沙盒文件,newfile為上面在沙盒文件中創建的mp3文件  [data writeToFile:newfile atomically:YES];

通過簡短實例為大家區分NSBundle和NSURL,希望對大家的學習有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 温宿县| 淄博市| 上杭县| 北票市| 衡阳县| 会宁县| 定日县| 正阳县| 出国| 齐齐哈尔市| 金阳县| 喀喇| 南开区| 宜川县| 襄汾县| 瑞丽市| 克东县| 西丰县| 鹤峰县| 新邵县| 南乐县| 肥西县| 沾化县| 萝北县| 英德市| 东源县| 葫芦岛市| 社旗县| 两当县| 祁阳县| 城市| 舒城县| 土默特右旗| 隆安县| 合川市| 南川市| 洛川县| 永德县| 镇赉县| 南丹县| 福州市|