iOS learning iOS sandbox (sandbox) mechanism and file Operation Nsfilemanager

Source: Internet
Author: User

1. Create a directory in documents

Create a directory called Test, first find the documents directory,

[CPP]View Plaincopy
  1. Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
  2. NSString *documentsdirectory = [Paths objectatindex:0];
  3. NSLog (@"documentsdirectory%@", documentsdirectory);
  4. Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];
  5. NSString *testdirectory = [documentsdirectory stringbyappendingpathcomponent:@"test"];
  6. //Create a directory
  7. [FileManager createdirectoryatpath:testdirectory withintermediatedirectories:yes Attributes:nil Error:nil];

Start the program when the directory is created:

2. Create a file in the test directory

What about creating a file? Then the above code Testpath to use stringByAppendingPathComponent stitching on the file name you want to generate, such as Test00.txt. This will make it possible to write to the file under test.

Testdirectory is the path to the above code generation Oh, don't forget. I wrote three files to the test folder, Test00.txt, Test22.txt,text.33.txt. Content is written to content, write String.

The implementation code is as follows:

[CPP]View Plaincopy
  1. NSString *testpath = [testdirectory stringbyappendingpathcomponent:@"Test00.txt"];
  2. NSString *testpath2 = [testdirectory stringbyappendingpathcomponent:@"Test22.txt"];
  3. NSString *testpath3 = [testdirectory stringbyappendingpathcomponent:@"Test33.txt"];
  4. NSString *string = @"Write content, write string";
  5. [FileManager createfileatpath:testpath contents:[string datausingencoding:nsutf8stringencoding] attributes:nil];
  6. [FileManager createfileatpath:testpath2 contents:[string datausingencoding:nsutf8stringencoding] attributes:nil];
  7. [FileManager createfileatpath:testpath3 contents:[string datausingencoding:nsutf8stringencoding] attributes:nil];

Look at the following figure, three files are out, the content is also right.

It's even easier to create it in the documents directory, without adding test.

3. Get all the file names in the directory column

Two ways to get: Subpathsofdirectoryatpath and Subpathsatpath

[CPP]View Plaincopy
  1. Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
  2. NSString *documentsdirectory = [Paths objectatindex:0];
  3. NSLog (@"documentsdirectory%@", documentsdirectory);
  4. Nsfilemanager *filemanage = [Nsfilemanager Defaultmanager];
  5. NSString *mydirectory = [documentsdirectory stringbyappendingpathcomponent:@"test"];
  6. Nsarray *file = [Filemanage subpathsofdirectoryatpath:mydirectory error:nil];
  7. NSLog (@"%@", file);
  8. Nsarray *files = [Filemanage subpathsatpath:mydirectory];
  9. NSLog (@"%@", files);

Get the file name in the test folder just above

Print results

2012-06-17 23:23:19.684 iossandbox[947:f803] FileList: (

". Ds_store ",

"Test00.txt",

"Test22.txt",

"Test33.txt"

)

2012-06-17 23:23:19.686 iossandbox[947:f803] Filelit (

". Ds_store ",

"Test00.txt",

"Test22.txt",

"Test33.txt"

)

Two methods are available, hidden files are also printed out.

4. FileManager Use operation current directory

[CPP]View Plaincopy
  1. Creating a File Manager
  2. Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];
  3. Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
  4. NSString *documentsdirectory = [Paths objectatindex:0];
  5. //change to the directory you want to operate
  6. [FileManager changecurrentdirectorypath:[documentsdirectory Stringbyexpandingtildeinpath];
  7. //create file FileName file name, contents file contents, if Start no content can be set to Nil,attributes file properties, initially nil
  8. NSString * FileName = @"TestFileNSFileManager.txt";
  9. Nsarray *array = [[Nsarray alloc] initwithobjects:@"Hello World", @"Hello world1", @"Hello world2", nil];
  10. [FileManager createfileatpath:filename Contents:array Attributes:nil];

This creates the TestFileNSFileManager.txt and writes three Hello world to the file.

Changecurrentdirectorypath directory changes to the current operating directory, do the file read and write is very convenient, do not add full path

5. Delete Files

After the code above, remove is OK.

[CPP]View Plaincopy
    1. [FileManager Removeitematpath:filename Error:nil];

6. Mixed data reading and writing

Create mixed data with nsmutabledata and then write to the file. and read the data according to the type of data.

6.1 Write Data:

[CPP]View Plaincopy
  1. NSString * FileName = @"TestFileNSFileManager.txt";
  2. Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
  3. NSString *documentsdirectory = [Paths objectatindex:0];
  4. Get file path
  5. NSString *path = [Documentsdirectory stringbyappendingpathcomponent:filename];
  6. Data to be written
  7. NSString *temp = @"Nihao World";
  8. int dataint = 1234;
  9. float datafloat = 3.14f;
  10. Create a data buffer
  11. Nsmutabledata *writer = [[Nsmutabledata alloc] init];
  12. Add a string to the buffer
  13. [writer Appenddata:[temp datausingencoding:nsutf8stringencoding]];
  14. Add additional data to the buffer
  15. [Writer Appendbytes:&dataint length:sizeof (Dataint)];
  16. [Writer appendbytes:&datafloat length:sizeof (datafloat)];
  17. Write buffered data to a file
  18. [Writer Writetofile:path Atomically:yes];


Let's see how the data looks:


We see the back is garbled, it is Chinese was turned into a nsdata, and the binary of int float

6.2 Read the data just written:

[CPP]View Plaincopy
    1. Read data:
    2. int intdata;
    3. float floatdata = 0.0;
    4. NSString *stringdata;
    5. NSData *reader = [NSData Datawithcontentsoffile:path];
    6. StringData = [[NSString alloc] Initwithdata:[reader subdatawithrange:nsmakerange (0, [temp length])]
    7. Encoding:nsutf8stringencoding];
    8. [Reader Getbytes:&intdata range:nsmakerange ([temp length], sizeof (Intdata))];
    9. [Reader Getbytes:&floatdata range:nsmakerange ([temp length] + sizeof (intdata), sizeof (Floatdata))];
    10. NSLog (@"stringdata:%@ intdata:%d floatdata:%f", StringData, Intdata, Floatdata);

iOS learning iOS sandbox (sandbox) mechanism and file Operation Nsfilemanager

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.