IOS File Management 2 and ios File Management
IOS development-File Management (2)
5. Plist files
Add in String Mode
NSString * path = [NSHomeDirectory () stringByAppendingPathComponent: @ "Array. plist"];
NSString * content = @ "abcd ";
[Contect writeToFile: path atomically: YES encoding: NSUTF8StringEncoding error: nil];
Array
NSString * path = [NSHomeDirectory () stringByAppendingPathComponent: @ "Array. plist"];
[NSArray * array = [[NSArray alloc] initWithObjects: @ "123", @ "798", @ "000", nil]; [array writeToFile: path atomically: YES];
Add in Dictionary Mode
NSString * path = [NSHomeDirectory () stringByAppendingPathComponent: @ "Dic. plist"];
NSDictionary * dic = [NSDictionary alloc] initWithObjects: @ "first", @ "second", @ "third" forKeys: @ "123", @ "456 ", @ "798"]; [dic writeToFile: path atomically: YES];
Arrays and dictionaries can only write BOOL, NSNumber, NSString, NSData, NSDate, NSArray, and NSDictionary to plist files in the property list.
6. Reading files and common methods
The NSFileHandle class mainly reads and writes the file content.
NSFileManager class is mainly used for file operations (delete, modify, move, copy, and so on)
Common Handling Methods
+ (Id) fileHandleForReadingAtPath :( NSString *) path open a file to prepare for reading
+ (Id) fileHandleForWritingAtPath :( NSString *) path to open a file to prepare for writing
+ (Id) fileHandleForUpdatingAtPath :( NSString *) path to open a file and prepare for update
-(NSData *) availableData; returns available data from the device or channel
-(NSData *) readDataToEndOfFile; read from the current node to the end of the file
-(NSData *) readDataOfLength :( NSUInteger) length; read the specified length data from the current node
-(Void) writeData :( NSData *) data; write data
-(Unsigned long) offsetInFile; get the offset of the current file
-(Void) seekToFileOffset :( unsigned long) offset; jump to the offset of the specified file
-(Unsigned long) seekToEndOfFile; jump to the end of the file
-(Void) truncateFileAtOffset :( unsigned long) offset; set the object length to offset bytes.
-(Void) closeFile; close the file
Append data to a file
NSString * homePath = NSHomeDirectory ();
NSString * sourcePath = [homePath stringByAppendingPathConmpone: @ "testfile. text"];
NSFileHandle * fielHandle = [NSFileHandle fileHandleForUpdatingAtPath: sourcePath];
[FileHandle seekToEndOfFile]; jump the node to the end of the file
NSString * str = @ "append data"
NSData * stringData = [str dataUsingEncoding: NSUTF8StringEncoding];
[FileHandle writeData: stringData]; append and write data
[FileHandle closeFile];
Locate data
NSFileManager * fm = [NSFileManager defamanager manager];
NSString * content = @ "abcdef ";
[Fm createFileAtPath: path contents: [content dataUsingEncoding: NSUTF8StringEncoding] attributes: nil];
NSFileHandle * fileHandle = [NSFileHandle fileHandleForReadingAtPath: path];
NSUInteger length = [fileHandle availabelData] length]; get the Data length
[FileHandle seekToFileOffset; length/2]; half of the Offset File
NSData * data = [fileHandle readDataToEndOfFile];
[FileHandle closeFile];
Copy a file
NSFileHandle * infile, * outfile; input file and output file
NSData * buffer; read buffer data
NSFileManager * fileManager = [NSFileManager defaultManager];
NSString * homePath = NSHomeDirectory ();
NSString * sourcePath = [homePath stringByAppendingPathComponent: @ "testfile.txt"]; source file path
NSString * outPath = [homePath stringByAppendingPathComponent: @ "outfile.txt"]; output file path
BOOL sucess = [fileManager createFileAtPath: outPath contents: nil attributes: nil];
If (! Success)
{
Return N0;
}
Infile = [NSFileHandle fileHandleForReadingAtPath: sourcePath]; create a file to read The Source Path
If (infile = nil)
{
Return NO;
}
Outfile = [NSFileHandle fileHandleForReadingAtPath: outPath]; create a disease to open the file to be output
If (outfile = nil)
{
Return NO;
}
[Outfile truncateFileAtOffset: 0]; set the length of the output file to 0
Buffer = [infile readDataToEndOfFile]; read data
[Outfile writeData: buffer]; write Input
[Infile closeFile]; close writing and input files
[Outfile closeFile];