Archive NSKeyedArchiver unarchive NSKeyedUnarchiver and file management class NSFileManager (File Operations ),
======================================
File Operations
======================================
I. Archive NSKeyedArchiver
1. The first method is to store data.
// Archive
// Method 1
// Object -- File
NSArray * array = [[NSArray alloc] initWithObjects: @ "zhang", @ "wang", @ "li", nil];
NSString * filePath = [NSHomeDirectory () stringByAppendingPathComponent: @ "array.txt"];
BOOL success = [NSKeyedArchiver archiveRootObject: array toFile: filePath];
If (success ){
NSLog (@ "saved successfully ");
}
// Archive
NSArray * arr = [NSKeyedUnarchiver unarchiveObjectWithFile: filePath];
NSLog (@ "% @", arr );
2. Method 2: store parallel data (store multiple types of data ).
// Method 2:
NSArray * array = @ [@ "one", @ "two", @ "three"];
NSDictionary * dic =@{@ "key": @ "value "};
NSString * str = @ "I am a Chinese, I love China ";
// NSData data stream class
// It is used to store complex data and convert the complex data into a data stream format for convenient storage and transmission.
// For example, images and large files
// Resumable upload. The size of the fake part is 20 mb,
// Send an email: add an attachment
NSMutableData * data = [NSMutableData alloc] init];
// InitForWritingWithMutableData specifies the data stream file to be written.
NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData: data];
// Encode the data. Parameter 1: The data to be encoded; parameter 2: The key and key of the encoded data.
[Archiver encodeObject: array forKey: @ "array"];
[Archiver encodeObject: dic forKey: @ "dic"];
[Archiver encodeObject: str forKey: @ "str"];
// Complete Encoding
[Archiver finishEncoding];
// Specify the file path
NSString * filePath = [NSHomeDirectory () stringByAppendingPathComponent: @ "file2"];
// Write a file
[Data writeToFile: filePath atomically: YES];
// ================================================ =
// Lower part
// Read the file in the path into the data stream first
NSMutableData * fileData = [[NSMutableData alloc] initWithContentsOfFile: filePath];
// Read the data stream file into the archive.
NSKeyedUnarchiver * unArchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData: fileData];
// Archive
NSArray * UnArray = [unArchiver decodeObjectForKey: @ "array"];
NSDictionary * UnDic = [unArchiver decodeObjectForKey: @ "dic"];
NSString * UnStr = [unArchiver decodeObjectForKey: @ "str"];
// Print
NSLog (@ "% @ \ n", UnArray, UnDic, UnStr );
3. Third archive method: Archive class objects
1. Implement the <NSCoding> protocol in the Class header file first.
2. recode and decode the protocol in. m.
// Re-initWithCoder Decoding Method
-(Id) initWithCoder: (NSCoder *) aDecoder
{
NSLog (@ "I am the decoding method and I am responsible for decoding ");
Self = [super init];
If (self ){
_ Name = [aDecoder decodeObjectForKey: @ "name"];
_ Phone = [aDecoder decodeObjectForKey: @ "phone"];
_ Address = [aDecoder decodeObjectForKey: @ "address"];
}
Return self;
}
// Re-encoding method
-(Void) encodeWithCoder: (NSCoder *) ACO
{
[Ecoder encodeObject: _ name forKey: @ "name"];
[Ecoder encodeObject: _ phone forKey: @ "phone"];
[Ecoder encodeObject: _ address forKey: @ "address"];
}
// [Note] forKey: A string. The encoding method and decoding method must be consistent.
Ii. NSFileManager file management class
1.1 file path
// Root path
NSString * homePath = NSHomeDirectory ();
NSLog (@ "% @", homePath );
Three directories in oc can be operated.
1. Documents // document directory
2. tmp // temporary directory: when the program exits, the contents of the temporary directory may be
3. library ---> Cache // Cache directory // under the app directory
// Obtain the Documents directory
// Write method 1
NSString * Documents = [NSHomeDirectory () stringByAppendingPathComponent: @ "Documents"];
NSLog (@ "Documents: % @", Documents );
// Statement 2
NSString * Documents1 = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDirectory, YES) objectAtIndex: 0];
NSLog (@ "Documents: % @", Documents1 );
// Obtain the main directory of the application
NSString * userName = NSUserName ();
NSString * rootPath = NSHomeDirectoryForUser (userName );
NSLog (@ "app root path: % @", rootPath );
// Obtain the cache directory
NSString * cachePath = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex: 0];
NSLog (@ "cache: % @", cachePath );
// Obtain the tmp directory
NSString * tmpPath = NSTemporaryDirectory ();
NSLog (@ "tmp: % @", tmpPath );
1.2 create directories and files
// Obtain the root directory
NSString * homePath = NSHomeDirectory ();
// Creates a file manager.
NSFileManager * fileMgr = [NSFileManager defaultManager];
// Concatenate the path of the file to be created
NSString * filePath = [NSString stringWithFormat: @ "% @/myFolder", homePath];
// Create a file directory
// The first parameter is used to input the directory of the file to be created. The second parameter is used to determine whether to create a folder that does not exist. yes indicates creation.
BOOL isOk = [fileMgr createDirectoryAtPath: filePath withIntermediateDirectories: YES attributes: nil error: nil];
If (isOk ){
NSLog (@ "successfully created the file directory ");
}
NSString * string = @ "I love to remember the lyrics ";
// Write the content to the specified file in the specified path
BOOL isWriteOk = [string writeToFile: [NSString stringWithFormat: @ "% @/1.txt", filePath] atomically: YES encoding: NSUTF8StringEncoding error: nil];
If (isWriteOk ){
NSLog (@ "file written successfully ");
}
// Save the Array
NSArray * array = @ [@ "I am a", @ "I Am a three", @ "I Am a week 7"];
BOOL isWriteOK1 = [array writeToFile: [NSString stringWithFormat: @ "% @/2.txt", filePath] atomically: YES];
If (isWriteOK1 ){
NSLog (@ "array file written successfully ");
}
// Save the dictionary
NSDictionary * dic =@{@ "key": @ "value "};
BOOL isWriteOK2 = [dic writeToFile: [NSString stringWithFormat: @ "% @/3.txt", filePath] atomically: YES];
If (isWriteOK2 ){
NSLog (@ "dictionary file written successfully ");
}
2. Rename the file
// Obtain the root directory
NSString * homePath = NSHomeDirectory ();
// Creates a file manager.
NSFileManager * fileMgr = [NSFileManager defaultManager];
// Concatenate the path of the file to be created
NSString * filePath = [NSString stringWithFormat: @ "% @/myFolder/1.txt", homePath];
[FileMgr moveItemAtPath: filePath toPath: [NSString stringWithFormat: @ "% @/ai.txt", homePath] error: nil];
3. delete an object
// Object that declares an error message
NSError * error;
// Obtain the root directory
NSString * homePath = NSHomeDirectory ();
// Creates a file manager.
NSFileManager * fileMgr = [NSFileManager defaultManager];
// Concatenate the path of the file to be created
NSString * filePath = [NSString stringWithFormat: @ "% @/myFolder/3.txt", homePath];
// Delete an object
// If NO is returned for method execution, error will save the error message. If YES is returned for method execution, error = nil
BOOL isok = [fileMgr removeItemAtPath: filePath error: & error];
If (isok ){
NSLog (@ "File deleted ");
}
Else
{
NSLog (@ "failed to delete file ");
// Print the error message
NSLog (@ "% @", error. localizedDescription );
}
Delta [extension] NSError class, which is an error information class
// Delete an object
// If NO is returned for method execution, error will save the error message. If YES is returned for method execution, error = nil
BOOL isok = [fileMgr removeItemAtPath: filePath error: & error];
4. Delete all files in the directory
// Obtain the root directory
NSString * homePath = NSHomeDirectory ();
// Creates a file manager.
NSFileManager * fileMgr = [NSFileManager defaultManager];
// Concatenate the path of the file to be created
NSString * filePath = [NSString stringWithFormat: @ "% @/myFolder/3.txt", homePath];
// If the deleted directory does not contain specific files, the entire directory is deleted.
[FileMgr removeItemAtPath: [NSString stringWithFormat: @ "% @/myFolder/", homePath] error: nil];
5. Get all files in the directory
// Obtain the root directory
NSString * homePath = NSHomeDirectory ();
// Creates a file manager.
NSFileManager * fileMgr = [NSFileManager defaultManager];
// Concatenate the path of the file to be created
NSString * filePath = [NSString stringWithFormat: @ "% @/myFolder/", homePath];
// Obtain all files in the current directory, including hidden files
NSArray * allFile = [fileMgr contentsOfDirectoryAtPath: filePath error: nil];
NSLog (@ "% @", allFile );
// Obtain all files in the current directory and subdirectory
NSArray * subAllFile = [fileMgr subpathsOfDirectoryAtPath: filePath error: nil];
6. File Attributes
// Obtain the root directory
NSString * homePath = NSHomeDirectory ();
// Creates a file manager.
NSFileManager * fileMgr = [NSFileManager defaultManager];
// Concatenate the path of the file to be created
NSString * filePath = [NSString stringWithFormat: @ "% @/myFolder/3.txt", homePath];
NSDictionary * fileAttribute = [fileMgr fileAttributesAtPath: filePath traverseLink: YES];
// Obtain the Object Attributes
NSData * data = [fileAttribute objectForKey: NSFileModificationDate];
NSLog (@ "file creation date: % @", data );
// Get file attributes 2 (*)
NSDictionary * dic1 = [fileMgr attributesOfItemAtPath: filePath error: nil];
NSLog (@ "Print attributes % @", dic1 );
// File size in bytes
NSNumber * number = [fileAttribute objectForKey: NSFileSize];
NSLog (@ "file size: % @", number );
// Determine whether a file exists
NSFileManager * manager = [NSFileManager defamanager manager];
BOOL isExist = [manager fileExistsAtPath: filePath];