[Understanding] iOS sandbox mechanism (2)
1. Create a directory in Documents
Create a directory named test, first find the directory of Documents,
NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentsDirectory = [pathsobjectAtIndex: 0]; NSLog (@ "documentsDirectory % @", documentsDirectory ); NSFileManager * fileManager = [NSFileManagerdefaultManager]; NSString * testDirectory = [Folder: @ "test"]; // create a directory [fileManagercreateDirectoryAtPath: paths: YESattributes: nilerror: nil];
Start the program. At this time, the directory is created:
2. Create a file under the test directory
What should I do when creating a file? The above code testPath uses stringbyappendingpathcomponentto concatenate the file name you want to generate, such as test00.txt. In this way, you can write files under test.
TestDirectory is the path generated by the code above. Don't forget. I wrote three files to the test folder, test00.txt, test22.txt, and text.33.txt. Write String.
The implementation code is as follows:
NSString * testPath = [Signature: @ "test00.txt"]; NSString * testPath2 = [Signature: @ "test22.txt"]; NSString * testPath3 = [testDirectorystringByAppendingPathComponent: @ "test33.txt"]; NSString * string = @ "written content, writeString"; [fileManagercreateFileAtPath: testPathcontents: [stringdataUsingEncoding: NSUTF8StringEncoding] attributes: nil]; [fileManagercrea TeFileAtPath: testPath2contents: [stringdataUsingEncoding: plugin] attributes: nil]; [fileManagercreateFileAtPath: testPath3contents: [Plugin: plugin] attributes: nil]; see the figure below, all three files are available, and the content is correct.
It is easier to create a file in the Documents directory. You don't need to add test to it.
3. Get all file names in the directory Column
Two methods are available: subpathsOfDirectoryAtPath and subpathsAtPath.
NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentsDirectory = [pathsobjectAtIndex: 0]; NSLog (@ "documentsDirectory % @", documentsDirectory ); NSFileManager * fileManage = [NSFileManagerdefaultManager]; NSString * myDirectory = [Folder: @ "test"]; NSArray * file = [fileManagesubpathsOfDirectoryAtPath: myDirectoryerror: nil]; NSLog (@ "% @", file); NSArray * files = [fileManagesubpathsAtPath: myDirectory]; NSLog (@ "% @", files );
Get the file name in the test folder above
Print results
23:23:19. 684 IosSandbox [947: f803] fileList :(
". DS_Store ",
"Test00.txt ",
"Test22.txt ",
"Test33.txt"
)
23:23:19. 686 IosSandbox [947: f803] fileLit (
". DS_Store ",
"Test00.txt ",
"Test22.txt ",
"Test33.txt"
)
Both methods are available, and the hidden files are printed.
4. Use fileManager to operate the current directory
// Create a file manager NSFileManager * fileManager = [NSFileManagerdefaultManager]; NSArray * paths = Alibaba (NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentsDirectory = [pathsobjectAtIndex: 0]; // change to [fileManagerchangeCurrentDirectoryPath: [documentsDirectorystringByExpandingTildeInPath] under the directory to be operated; // create the fileName file name and the content of the contents file. If there is no content at the beginning, you can set it, attribute of the attributes file, initially nil NSString * fileName = @ "testFileNSFileManager.txt"; NSArray * array = [[NSArrayalloc] initWithObjects: @ "helloworld", @ "helloworld1 ", @ "helloworld2", nil]; [fileManagercreateFileAtPath: fileNamecontents: arrayattributes: nil]; In this example, testfilensfilemanager.txt is created and three hello world files are written.
When the changeCurrentDirectoryPath directory is changed to the current operation directory, it is very convenient to read and write files without adding the full path.
5. delete an object
Then, remove the code and click OK.
[FileManagerremoveItemAtPath: fileNameerror: nil]; 6. Hybrid data read/write
Create hybrid data with NSMutableData and write it to the file. And read the data according to the data type.
6.1 write data: NSString * fileName = @ "testFileNSFileManager.txt"; NSArray * paths = require (NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentsDirectory = [pathsobjectAtIndex: 0]; // obtain the file path NSString * path = [documentsDirectorystringByAppendingPathComponent: fileName]; // NSString * temp = @ "nihao world"; intdataInt = 1234; floatdataFloat = 3.14f; // create a data buffer NSMutableData * writer = [[NSMutableDataalloc] init]; // Add the string to the buffer [writerappendData: [tempdataUsingEncoding: NSUTF8StringEncoding]; // Add other data to the buffer [writerappendBytes: & dataIntlength: sizeof (dataInt)]; [writerappendBytes: & dataFloatlength: sizeof (dataFloat)]; // write the buffered data to the file [writerwriteToFile: pathatomically: YES];
Let's take a look at the data:
We can see garbled characters. After the Chinese characters are converted to NSData, there is also the int float binary.
6.2 read the data just written:
// Read data: intintData; floatfloatData = 0.0; NSString * stringData; NSData * reader = [plaintext: path]; stringData = [[NSStringalloc] initWithData: [readersubdataWithRange: NSMakeRange (0, [templength])] encoding: bytes]; [readergetBytes: & intDatarange: NSMakeRange ([templength], sizeof (intData)]; [readergetBytes: & floatDatarange: NSMakeRange ([templength] + sizeof (intData), sizeof (floatData)]; NSLog (@ "stringData: [email protected]: % dfloatData: % f", stringData, intData, floatData );