Basic oc file read/write and operations, oc file read/write operations

Source: Internet
Author: User

Basic oc file read/write and operations, oc file read/write operations

Code:

# Import <Foundation/Foundation. h> // NSString Write File void stringWriteToFile () {NSString * path = [NSHomeDirectory () stringByAppendingPathComponent: @ "/Documents/test.txt"]; NSString * s = @ "test "; [s writeToFile: path atomically: YES encoding: NSUTF8StringEncoding error: nil]; NSString * str = [[NSString alloc] handler: path encoding: NSUTF8StringEncoding error: nil]; NSLog (@ "\ nstring = % @", str); NSS Tring * testtxt = [[NSString alloc] initWithContentsOfFile: path encoding: NSUTF8StringEncoding error: nil]; NSLog (@ "\ ntest. text = % @ ", testtxt);} // NSArray Write File void arrayWriteToFile () {NSArray * arr = @ [@" a ", @" B ", @ "B", @ "c"]; NSString * path = [NSHomeDirectory () stringByAppendingPathComponent: @ "/Documents/test2.txt"]; [arr writeToFile: path atomically: YES]; NSArray * a = [NSArray alloc] initWithContentsOfFi Le: path]; NSLog (@ "array = \ n % @", a); NSString * s = [[NSString alloc] initWithContentsOfFile: path encoding: NSUTF8StringEncoding error: nil]; NSLog (@ "\ ntest2.txt = \ n % @", s);} // NSDictionary Write File void dictionaryWriteToFile () {NSDictionary * dic ={ @ "": @ "1", @ "B": @ "2", @ "c": @ "3", @ "d": @ "4 "}; NSString * path = [NSHomeDirectory () stringByAppendingPathComponent: @ "/Documents/test3.txt"]; [dic writeToFile: path Atomically: YES]; NSDictionary * d = [[NSDictionary alloc] initWithContentsOfFile: path]; NSLog (@ "\ ndictionary = \ n % @", d ); NSString * s = [[NSString alloc] initWithContentsOfFile: path encoding: NSUTF8StringEncoding error: nil]; NSLog (@ "\ ntest3.txt = \ n % @", s );} // whether atomically writes data at the atomic level, that is, transactional data. // NSNumber, NSDate, and NSData can all write data to a file through writeToFile, which is of the plain text type, if you change the suffix to plist, The xcode attribute list file int main (int argc, const char * argv []) {// This method creates an instance, but the NSFileManager Singleton mode will be ineffective. NSFileManager * fm0 = [[NSFileManager alloc] init]; // defaultManager creates the NSFileManager object NSFileManager * fm1 = [NSFileManager defaultManager] in singleton mode; NSFileManager * fm2 = [NSFileManager defaultManager]; NSLog (@ "\ nfm0 = % p, fm1 = % p, fm2 = % p ", fm0, fm1, fm2); NSData * data = [[NSString stringWithFormat: @" main "] dataUsingEncoding: NSUTF8StringEncoding]; NSString * path = [NSHomeDirectory () stringByAppendingPathComponent: @ "events/main.txt"]; if (! [Fm1 fileExistsAtPath: path]) {if ([fm1 createFileAtPath: path contents: data attributes: nil]) {NSLog (@ "create success "); NSDictionary * d = [fm1 attributesOfItemAtPath: path error: nil]; NSLog (@ "\ nattributesOfItemAtPath = \ n % @", d ); // NSFileSize is the predefined File Attribute key. You can view the system file to obtain other attribute keys and obtain the attribute NSNumber * filesize = [d valueForKey: NSFileSize] using the following methods: NSLog (@ "\ nfilesize = % @", filesize); // read the file NSData * data1 = [fm1 contentsAtPath: path]; NSString * s = [[NSString alloc] initWithData: data1 encoding: NSUTF8StringEncoding]; // attributes of the file system // total space, used space, available space, number of files, NSLog (@ "\ nattributesOfFileSystemForPath = \ n % @", [fm1 attributesOfFileSystemForPath: path error: nil]); NSLog (@ "\ nmain.txt =%@", s) ;}} else {NSString * copypath = [NSHomeDirectory () stringByAppendingPathComponent: @ "/Documents/main_copy.txt"]; [fm1 copyItemAtPath: path toPath: copypath error: nil]; // rename: The target path is the same as the main path, but the file name is different. NSString * movepath = [NSHomeDirectory () stringByAppendingPathComponent: @ "/Documents/main_move.txt"]; [fm1 moveItemAtPath: path: movepath error: nil]; // Delete the object if ([fm1 removeItemAtPath: path error: nil]) {NSLog (@ "remove % @ success", path );} if ([fm1 removeItemAtPath: copypath error: nil]) {NSLog (@ "remove % @ success", copypath);} if ([fm1 removeItemAtPath: movepath error: nil]) {NSLog (@ "remove % @ success", movepath) ;}} stringWriteToFile (); arrayWriteToFile (); dictionaryWriteToFile (); return 0 ;}

Result:

2015-03-08 21:18:26.064 NSFileManagerDemo[1686:79942] fm0 = 0x1001145d0,fm1 = 0x100114620,fm2 = 0x1001146202015-03-08 21:18:26.075 NSFileManagerDemo[1686:79942] remove /Users/yoran_yang/Documents/main_copy.txt success2015-03-08 21:18:26.076 NSFileManagerDemo[1686:79942] remove /Users/yoran_yang/Documents/main_move.txt success2015-03-08 21:18:26.076 NSFileManagerDemo[1686:79942] string = test2015-03-08 21:18:26.077 NSFileManagerDemo[1686:79942] test.text = test2015-03-08 21:18:26.078 NSFileManagerDemo[1686:79942] array = (    a,    b,    b,    c)2015-03-08 21:18:26.078 NSFileManagerDemo[1686:79942] test2.txt = <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><array>    <string>a</string>    <string>b</string>    <string>b</string>    <string>c</string></array></plist>2015-03-08 21:18:26.079 NSFileManagerDemo[1686:79942] dictionary = {    a = 1;    b = 2;    c = 3;    d = 4;}2015-03-08 21:18:26.079 NSFileManagerDemo[1686:79942] test3.txt = <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict>    <key>a</key>    <string>1</string>    <key>b</key>    <string>2</string>    <key>c</key>    <string>3</string>    <key>d</key>    <string>4</string></dict></plist>Program ended with exit code: 0

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.