———————————————————————————————————————————
Read and write a string from a file (read/write directly/through Nsurl)
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
@autoreleasepool {
Writes a string directly to a file ***************
NSString *str1=[[nsstring Alloc]init];
[Email protected] "Lalalalala";
Not through Nsurl, and directly to the string str1 write to an address file, as long as the creation of the completion of STR1, then directly with the STR1 to call the WriteToFile method can be. Note that in the address of the place to be written in the form of a string, and to create the file itself (write the filename of the string to write their own), there is a point, in the Atomic (atomically) column is to choose whether to select Multi-threaded management mechanism, so is to write yes or no, Rather than atomic and nonatomic. The last place, the error message whether the processing, do not deal with the words we have to fill nil, and not anything else!
When we choose to handle the error message, note that there is a need to define a level two pointer to accept the error message, so we have to remember how level two pointers are created
Nserror *err;//can be created directly, nothing more complicated than anything else. (with the Nserror class, here to Remember)
Path Atomic encoding format (General UTF-8)
STR1 writetofile:<# (NSString *) #> atomically:<# (BOOL) #> encoding:<# (nsstringencoding) #> error: <# (nserror *__autoreleasing *) #>//receive error message (can choose nil, that is, do not handle error messages)
Note: When writing a string directly to a file, the address of the file is not added to the protocol header.
[Str1 writetofile:@ "/users/wangzhongyao/desktop/1.txt" Atomically:no encoding:nsutf8stringencoding Error:&err] ;//Careful observation, here is a pointer to the variable address, that is, a level two pointer
if (ERR)//If there is a value in err, indicating that an error has occurred, it is possible to write to the file failed
{
NSLog (@ "Write Failed");
}
else//If err is the same as nil when created, then there is no error, that is, writing the file successfully
{
NSLog (@ "write success");
}
Read the string directly from the file ***************
Create a string to accept the string information in the file
NSString *str11=[nsstring stringwithcontentsoffile:@ "/users/wangzhongyao/desktop/1.txt" encoding: Nsutf8stringencoding Error:nil];
Print a string directly
NSLog (@ "%@", Str11);
Writes a string to a file by Nsurl ***************
NSString *str2=[[nsstring Alloc]init];
[Email protected] "hahahahaha";
Use the Nsurl class to create an object to hold the address of the file we want to write, and then use Nsurl to call the class method of the Class URLWithString (a method is required to store the address of the string type in an object of type Nsurl)
Note that once the address is used, the protocol header (file://) must be added.
Note: The basic URL contains the protocol, host domain name (server name, IP address), path
Can be simple to think: URL = Protocol Header://Host domain name/path
Nsurl *url1=[nsurl urlwithstring:@ "File:///Users/wangzhongyao/Desktop/2.txt"];
Because this is written in the URL, so the method we call is Writetourl.
[str2 writetourl:url1 atomically:no encoding:nsutf8stringencoding Error:nil];
Read a string from a file by Nsurl ***************
Create an object of type Nsurl to accept the address of the file (URL1, of course, when it is created directly)
Nsurl *url2=[nsurl urlwithstring:@ "File:///Users/wangzhongyao/Desktop/2.txt"];
Create a String object Str22 to accept string information from the file
NSString *str22=[nsstring stringwithcontentsofurl:url2 encoding:nsutf8stringencoding Error:nil];
NSLog (@ "%@", str22);
}
return 0;
}
———————————————————————————————————————————
Objective-c "read and write strings from a file (read/write directly/Read through Nsurl)"