IOS development-file operations and ios development operations

Source: Internet
Author: User

IOS development-file operations and ios development operations

Directory operations and file management

 

Learning Objectives

1. Understanding Singleton

2. Familiar with common file management operations of NSFileManager

3. Master common file data operations of the NSFileHandle class

4. Understand common NSData operations

5. Master Plist file read/write

----------------------

Generally, when a program is running or the program ends, some information needs to be stored persistently, such as login information, video playback records, and favorite records, we can use the following methods to persistently store data.

 

1.1 Singleton mode (the current object has only one instance)

Benefit: only one instance is available for data sharing.

Singleton mode is a common design mode. When this mode is applied,

The class of the single-instance object must ensure that only one instance exists, and it is self-instantiated and

The system provides this instance. If you want to have only one class object in the system,

Singleton mode is the best solution.

 

In fact, the Singleton is similar to the global variable in C language.

During the entire program declaration period, only one copy of the object exists in the memory.

Data can be shared among multiple objects.

 

 

 

<1> create a single instance

(1) the method for creating a single instance usually starts with default/shared/standard.

 

 

(2) release or autorelease is not required for a singleton because the life cycle of the Singleton is the whole program.

 

 

2. Method:

2.1 NSFileManager

<1> create a file manager singleton object [NSFileManager defaultManager] <2> traverse the contents in the directory // retrieve the files in the current directory/* contentsOfDirectoryAtPath: Path to be traversed error: error Message */NSArray * array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: path error: & error]; // in-depth traversal array = [fileManger subpathsOfDirectoryAtPath: path error: nil]; <3> when determining whether a file exists/* when determining whether the file exists, you must add a suffix. If there is a Suffix in path to indicate a file, without a suffix to indicate a folder. */BOOL isExist = [fileManger fileExistsAtPath: path]; if (isExist) {NSLog (@ "");} else {NSLog (@ "");} <4> create a file and directory // create a file/* createFileAtPath: Path of the file to be created contents: File Content (NSData type) attributes: file attribute, generally nil, nil indicates that the default attribute is used. * // Note: if the file already exists, it will overwrite BOOL createOK = [fileManger createFileAtPath: path contents: nil attributes: nil]; // create the directory/* createDirectoryAtPath: the directory to be created. If the folder already exists, it will not overwrite withIntermediateDirectories: whether the middle directory attributes: folder attribute exists. nil indicates the default attribute error: error message */createOK = [fileManger createDirectoryAtPath: path withIntermediateDirectories: YES attributes: nil error: & error]; <5> copy file/directory // directory and directory copy [fileManger copyItemAtPath: fromFilePath toPath: toDirPath error: & error]; // copy files and files [fileManger copyItemAtPath: fromFilePath toPath: toFilePath error: & error]; <6> move the file/directory [fileManger moveItemAtPath: frompath toPath: toPath error: nil]; <7> delete the file/directory [fileManger removeItemAtPath: toPath error: nil]; <8> obtain the File Attribute NSDictionary * attributes = [fileManger attributesOfItemAtPath: path error: nil]; NSLog (@ "file attributes: % @", attributes );

 

 

 

2.2 NSData (binary data)

<1> convert NSString to NSData

NSData * data = [string dataUsingEncoding:

NSUTF8StringEncoding]

 

<2> convert NSData to NSString

NSString* ConvertString = [[NSString alloc]

InitWithData: data encoding: NSUTF8StringEncoding]

 

 

2.3NSFileHandle(File handle class)

To read and write files, NSFileHandle is required to open the file,

NSFileHandle reads and writes NSData binary data.

 

<1> file opening method // read-only handle NSFileHandle * readOnlyHandle = [NSFileHandle handle: path]; // write-only handle NSFileHandle * writeOnlyHandle = [NSFileHandle fileHandleForWritingAtPath: path]; // read/write handle NSFileHandle * readAndWriteHandle = [NSFileHandle fileHandleForUpdatingAtPath: path]; <2> Read data of the specified length (in bytes) // read five bytes of data NSData * data = [readOnlyHandle readDataOfLength: 5]; <3> read from the current offset to the end of the file [readOnlyHandle readDataToEndOfFile] [readAndWriteHandle readDataToEndOfFile] <4> set the file offset (in bytes) [readOnlyHandle seekToFileOffset: the number of bytes of offset <5> locate the file offset to the end of the file [readOnlyHandle seekToEndOfFile]; <6> write the file (set the offset when not overwriting) // 1. first, point the offset to the end of the file [readAndWriteHandle seekToEndOfFile]; // 2. write the data to the specified path [readAndWriteHandle writeData: [@ "abcdef" dataUsingEncoding: NSUTF8StringEncoding]; <7> close the file handle // close the file handle (not required) [readAndWriteHandle closeFile]; [readOnlyHandle closeFile]; [writeOnlyHandle closeFile];

 

 

 

 

 

 

 

 

 

 

3 Plist

<1> what is a plist file? What is the role of a plist file?

1. plist file: property list file. The file content can only be NSString, NSNumber, NSDate, NSData, NSArray, or NSDictionary objects. Other types of data cannot be saved.

 

2. Role: persistently store login registration information or program configuration information (small data)

 

<2> How to Create a plist file and edit a plist File

Plist file content is in XML syntax format

 

1. Create Xcode

1. Right-click and choose New File. A dialog box is displayed.

2. In iOS, select Resource in iOS or Mac, and select resource in OS X.

3. Click the Property List in Resource to create the plist file.

4. Click '+' in the file to add data.

 

2. Code Creation

If you want to write NSString NSNumber NSDate NSData NSArray NSDictionary objects to a file, plist files are generally used.

We need to save the data to an array or dictionary, and then call the functions related to the array and dictionary to write the number group NSArray or dictionary NSDictionary into the plist file.

// NSArray and NSDictionary file writing methods

-(BOOL) writeToFile :( NSString *) path atomically:

(BOOL) useAuxiliaryFile;

 

<3> how to read plist file data in a program

The root node of the Plist file (the outermost layer of the data) is usually an array or dictionary.

If the root node of the Plist file is a dictionary, use the dictionary class method.

+ (Id) dictionaryWithContentsOfFile :( NSString *) path;

Read and Write Plist files

If the root node is an array, use the array class method.

+ (Id) arrayWithContentsOfFile :( NSString *) path;

Read Plist files.

Note: The above two methods can only read Plist files, but cannot read files in other formats.

 

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.