文章出處
文章列表
一、讀寫方法對比:(主要針對本地讀取本地文件)
方式\操作 | 讀 | 寫 |
非URL方式 |
stringWithContentsOfFile |
writeToFile |
URL方式 |
stringWithContentsOfURL |
writeToURL |
實際開發中,大部分都采用URL方式。
對于寫入操作,情況都是:如果文件存在,則覆蓋原文件中的內容;如果文件不存在,則創建一個新文件
二、非URL方式
1.比較陌生的是文件寫入的方法中,會有個atomically參數。
atomically(原子性):設置YES時,沒有寫完,則會全部撤銷(比較安全的做法)。
設置NO時,沒有寫完,不會撤銷。
2.NSError對象,可以通過localizedDescription方法獲取報錯信息。
例子:
// // main.m // 字符串練習2:讀寫文件 // // Created by Apple on 15/12/7. // Copyright © 2015年 Apple. All rights reserved. // #import <Foundation/Foundation.h> void readFile(NSString *path); void writeToFile(NSString *path, NSString *str); int main(int argc, const char * argv[]) { //讀取文件中的內容 //NSString *path1 = @"/Users/apple/Desktop/KeenApps/Object-C/Object-c-Test/字符串練習2:讀寫文件/1.txt"; NSString *path1 = @"/Users/apple/Desktop/1.txt"; NSLog(@"讀取文件:"); readFile(path1); //寫入文件內容 NSString *path2 = @"/Users/apple/Desktop/KeenApps/Object-C/Object-c-Test/字符串練習2:讀寫文件/2.txt"; NSLog(@"寫入文件"); NSString *str = @"這是一個測試"; writeToFile(path2,str); NSLog(@"讀取文件:"); readFile(path2); return 0; } //讀取文件 void readFile(NSString *path){ NSError *error = nil; NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error]; if (error != nil) { NSLog([error localizedDescription]);//將錯誤信息輸出來 } else{ NSLog(@"%@",str); } } //寫入文件 void writeToFile(NSString *path, NSString *str){ NSError *error = nil; //atomically : YES時,沒有寫完,則會全部撤銷;NO時候,沒有寫完,不會撤銷 //注意:這種寫入方式,如果文件補存在,則創建;如果文件存在,則覆蓋原文件的內容 BOOL flag = [str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];//一般error都設置為nil,保證寫入成功 if (flag) { NSLog(@"寫入成功"); } else{ NSLog(@"寫入失敗"); } }
三、URL方式
URL方式,根據URL對象的創建方式,又分為兩種:
一.通過URLWithString方法創建URL對象
1)路徑:協議 + ip + 文件路徑
訪問本地資源:file:// + ip + /文件路徑
訪問網絡資源:http:// + ip + /文件路徑
注意:(1)讀取本地資源的時候,ip地址可以省略,即:file:///文件路徑(ip地址后面/不能省略,代表著根路徑)
(2)路徑中如果包含中文的時候,需要使用stringByAddingPercentEscapesUsingEncoding方法或stringByAddingPercentEncodingWithAllowedCharacters方法進行路徑進行編碼,不然,會提示:The file couldn’t be opened because the specified URL type isn’t supported.(URL類型不支持)
例如:
NSString *path = @"file://192.168.1.103/Users/apple/Desktop/讀寫文件練習2/1.txt”; //NSString *path = @"file:///Users/apple/Desktop/讀寫文件練習2/1.txt”; //path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//比較老的方法,現在被下面的方法取代 path = [path stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; NSURL *url = [NSURL URLWithString:path]; NSString *str = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error]; if (error == nil) { NSLog(@"%@",str); } else{ NSLog(@"%@",[error localizedDescription]); }
二.通過fileURLWithPath方法創建URL對象,訪問本地資源
使用這個方法時,需要注意:
1)系統會幫我們自動加入file://,我們不需要再添加。再添加,路徑就不對了。
2)即使URL中包含中文,都可以訪問。系統會自動對包含的中文進行處理。所以一般開發中,訪問本地資源,都使用這個方法。
例子:
NSString *path = @"/Users/apple/Desktop/讀寫文件練習2/1.txt”; NSURL *url = [NSURL fileURLWithPath:path]; NSString *str = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error]; if (error == nil) { NSLog(@"%@",str); } else{ NSLog(@"%@",[error localizedDescription]); }
三、綜合例子:
// // main.m // 讀寫文件練習2 // // Created by Apple on 15/12/7. // Copyright © 2015年 Apple. All rights reserved. // #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { //一.文件讀取 //路徑使用URL //1.加載本地文件。注意:file://,不是file:/// NSString *path = @"file://192.168.1.103/Users/apple/Desktop/KeenApps/Object-C/Object-c-Test/讀寫文件練習2/1.txt"; //如果加載的是本地資源,那么URL上的主機地址可以不要 //注意:ip地址后面的斜杠不能省略!(其代表著跟路徑) //path = @"file:///Users/apple/Desktop/KeenApps/Object-C/Object-c-Test/讀寫文件練習2/1.txt"; //如果路徑中包含中文,先進行編碼 //不編碼的后果是:The file couldn’t be opened because the specified URL type isn’t supported.(URL類型不支持) //path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//比較老的方法,現在被下面的方法取代 //path = [path stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; //2.加載網絡資源 //path = @"http://www.baidu.com"; //NSURL *url = [NSURL URLWithString:path]; //3.使用fileURLWithPath創建URL對象,加載本地資源。 /* 使用這個方法時,需要注意: 1)系統會幫我們自動加入file://,我們不需要再添加。再添加,路徑就不對了。 2)即使URL中包含中文,都可以訪問。系統會自動對包含的中文進行處理。所以一般開發中,訪問本地資源,都使用這個方法。 */ path = @"/Users/apple/Desktop/KeenApps/Object-C/Object-c-Test/讀寫文件練習2/1.txt"; NSURL *url = [NSURL fileURLWithPath:path]; NSError *error = nil; NSString *str = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error]; if (error == nil) { NSLog(@"%@",str); } else{ NSLog(@"%@",[error localizedDescription]); } //二.文件寫入 //路徑使用URL NSError *error2 = nil; NSString *str2 = @"this is a test2"; /* //第一種方式 NSString *path2 =@"file:///Users/apple/Desktop/KeenApps/Object-C/Object-c-Test/讀寫文件練習2/2.txt"; path2 = [path2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSLog(@"path2 = %@",path2); NSURL *url2 = [NSURL URLWithString:path2]; */ //第二種方式 NSString *path2 = @"/Users/apple/Desktop/KeenApps/Object-C/Object-c-Test/讀寫文件練習2/2.txt"; NSURL *url2 = [NSURL fileURLWithPath:path2]; //注意:如果文件存在,則覆蓋原文件的內容;如果文件不存在,則新建 [str2 writeToURL:url2 atomically:YES encoding:NSUTF8StringEncoding error:&error2]; if (error2 != nil) { NSLog(@"%@", [error2 localizedDescription]); } else{ NSLog(@"文件寫入成功!"); } return 0; }
推薦閱讀:
文章列表
全站熱搜