1 /**2 * Initwithcontentsoffile: Get content from File3 * Initwithcontentsofurl: Get content from URL4 * WriteToFile: Write file5 * atomically: atomicity (the file will not be interrupted when it is written, and will not be interrupted by any other tasks or events until the execution is completed.) )6 */7 #import<Foundation/Foundation.h>8 9 intMainintargcConst Char*argv[]) {Ten @autoreleasepool { One A //get a string from a file or URL -Nserror *error;//error:<# (nserror *__autoreleasing *) #> level Two pointers, so just define an object and pass it in. - //encoding:<# (nsstringencoding) #> character encoding, defined as an enumeration type, so it can be represented by a number (nsutf8stringencoding = 4) theNSString *str1=[[nsstring Alloc]initwithcontentsoffile:@"/users/apple/desktop/test.txt"Encoding4error:&ERROR]; - -NSLog (@"%@", str1);//Prints the result if the file does not exist (NULL) or if there is no permission there will be a problem - + //URL Basic Syntax - //protocol Name://domain or IP: port/file path? parameters + //http://www.weather.com.cn/adat/sk/101010100.html(Beijing Weather) ANSString *str2=[[nsstring Alloc]initwithcontentsofurl:[nsurl urlwithstring:@"http://www.weather.com.cn/adat/sk/101010100.html"] Encoding:4error:&error];//printing results will be related to the page atNSLog (@"%@", str2); - - //the results of the query are as follows: - /*{"Weatherinfo": {"City": "Beijing", "Cityid": "101010100", "temp": "9", "WD": "Southwest Wind", "WS": "Level 2", "SD": "22%", "WSE": "2", " Time ":" 10:35 "," Isradar ":" 1 "," Radar ":" JC_RADAR_AZ9010_JB "," NJD ":" No Live "," qy ":" 1015 "}} - */ - in //Write File -NSString *str3=@"This phrase will be written to the file"; toBOOL ISTRUE=[STR3 WriteToFile:@"/users/chengaojian/desktop/newfile.txt"Atomically:yes Encoding:4error:&error];//automically means write all at once, and if there is an error in the middle, it will not be written +NSLog (@"Write%@", Istrue==true?@"Success":@"failed"); - } the return 0; *}
OC gets a string from a file or URL, and writes a file