IOS file Operations

Source: Internet
Author: User


The entire code below you can directly use, specifically to see a good look at the problem, I also look at others.

Who can be born skillfully.

NSString class, wrote a few simple operations called system functions.

%@ print NSString, Nsarray object.

%i Integer

%f floating Point


Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];    Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);    NSLog (@ "path=%@", paths);        NSString *documentsdirectory = [Paths objectatindex:0];    NSLog (@ "documentsdirectory=%@", documentsdirectory);    [FileManager changecurrentdirectorypath:[documentsdirectory Stringbyexpandingtildeinpath];    [FileManager createfileatpath:@ "FileName" Contents:nil Attributes:nil];        [FileManager removeitematpath:@ "Creatednewfile" error:nil];    NSString *path = [documentsdirectory stringbyappendingpathcomponent:@ "FileName"];        NSLog (@ "path=%@", Path);    NSString *temp = @ "Welcome to Blog.csdn.net/qianguozheng";    int data0 = 800;        float data1 = 12.34f;        Nsmutabledata *writer = [[Nsmutabledata alloc] init];    [writer Appenddata:[temp datausingencoding:nsutf8stringencoding]];    [Writer Appendbytes:&data0 length:sizeof (DATA0)]; [Writer Appendbytes:&data1 LenGth:sizeof (data1)];    [Writer Writetofile:path Atomically:yes];        [Writer release];    int gData0;    float gData1;        NSString *gdata2;    NSData *reader = [NSData Datawithcontentsoffile:path]; GDATA2 = [[NSString alloc] Initwithdata:[reader subdatawithrange:nsmakerange (0, [temp length])] Encoding:        Nsutf8stringencoding];    [Reader getbytes:&gdata0 range:nsmakerange ([temp length], sizeof (GDATA0))];    [Reader getbytes:&gdata1 range:nsmakerange ([temp length] + sizeof (GDATA0), sizeof (DATA1))]; NSLog (@ "gdata0:%i gdata1:%f gdata2:%@", gData0, GData1, GDATA2);


The output is as follows:

2015-01-08 22:45:01.423 oc[21613:303] path= (

"/users/weeds/documents"

)

2015-01-08 22:45:01.424 oc[21613:303] Documentsdirectory=/users/weeds/documents

2015-01-08 22:45:01.429 oc[21613:303] Path=/users/weeds/documents/filename

2015-01-08 22:45:01.430 oc[21613:303] gdata0:800 gdata1:12.340001 gdata2:welcome to Blog.csdn.net/qianguozheng



IOS file Operations

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.