File Operations for data storage

Source: Internet
Author: User

In ios development, if you want to access the folder of the app in the simulator, you can access the folder either directly through the program itself or through the command line. The access method in the command line is: first open the command, in the command line, enter the following command to access the directory of the specified app, the command is as follows: cd/Users/ren/Library/Application \ Support/iPhone \ Simulator/6.0/Applications/805D7CF0-5DBB-4FCD-9922-0D5C460F510E/command line explanation: cd/Users/Host Name/Library/ApplicationSupport/iPhone Simulator/SDK version/Applications/unique application ID/then, you can enter any folder of the application. To copy or delete a folder, you need the root permission. Second, access your own folders through the application itself. Create, delete, write, and read files through programs. 1. Access the folder in the specified location. Visit the Documents folder NSArray * paths = Alibaba (NSDocumentDirectory, NSUserDomainMask, YES); NSString * docPaths = [paths objectAtIndex: 0]; visit the Cache folder NSArray * paths1 = Alibaba (NSCachesDirectory, NSUserDomainMask, YES); NSString * cachePaths = [paths1 objectAtIndex: 0]; access the Library folder NSArray * paths2 = NSSearchPathForDirectoriesInDomains (NSLibraryDirectory, NSUserDom AinMask, YES); NSString * libraryPaths = [paths2 objectAtIndex: 0]; // The NSDocumentDirectory constant specifies that the search target is the Documents folder, the NSUuserDomainMask constant specifies to search from the main directory of the application, that is, from the Application folder, find the corresponding program folder. // YES indicates that you want to obtain the complete path NSString * documentsDirectory = [paths objectAtIndex: 0] for all directories found. // remember this sentence. There is only one unique Documents folder in each application folder, but the NSSearch method returns many Documents. We only need to take the first one. (The Cache and Library are the same as those of Documents ). 2. The following describes how to create, delete, write, and read files. Create and delete: // create the File Manager NSFileManager * fileManager = [NSFileManager defaultManager]; // obtain the path NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentsDirectory = [paths objectAtIndex: 0]; // change the required path to the directory to be operated [fileManager changeCurrentDirectoryPath: [documentsDirectorystringByExpandingTildeInPath]; // create the file fileName file name and the content of the contents file. If there is no content at the beginning, you can set it to nil. the attribute of the attributes file is initially nil [fileManager createFileAtPath: @ "fileName" contents: nilattributes: nil]; // delete the file to be deleted [fileManager removeItemAtPath: @ "createdNewFile" error: nil]; // write data: // Create File Manager NSFileManager * fileManager = [NSFileManagerdefaultManager]; // obtain the path NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES ); NSString * documentsDirectory = [pathsobjectAtIndex: 0]; // path NSLog (@ "documentsDirecrtory: % @", documentsDirectory ); // change to [fileManager changeCurrentDirectoryPath: [documentsDirectorystringByExpandingTildeInPath] under the directory to be operated; // create the fileName file name and the content of the contents file. If no content is available, set it to nil, attribute of the attributes file, initially nil [fileManager createFileAtPath: @ "fileone" contents: nil attributes: nil]; NSString * str = [documentsDirectorystringByAppendingPathComponent: @ "fileone"]; NSLog (@ "str: % @", str); BOOL isExsit = [fileManagerfileExistsAtPath: str]; if (isExsit) {NSLog (@ "file exists ");} else {NSLog (@ "file does not exist");} // obtain the file path NSString * path = [documentsDirectorystringByAppendingPathComponent: @ "fileone"]; NSLog (@ "path: % @", path); // NSString * temp = @ "Hello friend"; int data0 = 100000; float data1 = 23.45f; // create a data buffer NSMutableData * writer = [[NSMutableDataalloc] init]; // Add the string to the buffer [writer appendData: [temp dataUsingEncoding: NSUTF8StringEncoding]; // Add other data to the buffer [writer appendBytes: & data0 length: sizeof (data0)]; [writer appendBytes: & data1 length: sizeof (data1)]; // write the buffered data to the file [writer writeToFile: path atomically: YES]; // read data: int gData0; float gData1; NSString * gData2; NSData * reader = [handler: path]; gData2 = [[NSString alloc] initWithData: [readersubdataWithRange: NSMakeRange (0, [temp length])] encoding: NSUTF8StringEncoding]; // obtain the first written data [reader getBytes: & gData0 range: NSMakeRange ([templength], sizeof (gData0)]; // obtain the second written data [reader getBytes: & gData1range: NSMakeRange ([temp length] + sizeof (gData0), sizeof (gData1)]; // obtain the third written data // NSMakeRange indicates the data range. getBytes reads the data NSLog of the specified range from reader (@ "% @, % d, % f", gData2, gData0, gData1); // read files in the project: // when reading data, view the original file format of the read files, whether it is a bytecode or text // It can be in a bytecode format, or it can be used to read/the variable used to store data. Because it is a byte, It is UInt8 UInt8 B = 0; // obtain the file path NSString * path1 = [[NSBundle mainBundle] pathForResource: @ "key" ofType: @ "txt"]; // obtain data NSData * reader1 = [NSDatadataWithContentsOfFile: path1]; // get the number of bytes int length = [reader1 length]; // obtain the string NSString * ges in the text = [[NSString alloc] initWithData: [reader1subdataWithRange: NSMakeRange (0, length)] encoding: NSUTF8StringEncoding]; NSLog (@ "% @", ges); NSLog (@ "lenghth: % d", length ); for (int I = 0; I <length; I ++) {// read data [reader getBytes: & brange: NSMakeRange (I, sizeof (B)]; NSLog (@ "---> data % d: % d", I, B );}

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.