IOS development-there are many things you don't know about File Management (2). ios File Management

Source: Internet
Author: User

IOS development-there are many things you don't know about File Management (2). ios File Management

Master haomeng is devoted to his contribution and respects the author's Labor achievements. Do not repost them.

If the article is helpful to you, you are welcome to donate to the author, support haomeng master, the amount of donation is free, focusing on your mind ^_^

I want to donate: Click to donate

Cocos2d-X source code download: point I send

Game official download: http://dwz.cn/RwTjl

Game video preview: http://dwz.cn/RzHHd

Game Development blog: http://dwz.cn/RzJzI

Game source code transfer: http://dwz.cn/Nret1


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];

Master haomeng is devoted to his contribution and respects the author's Labor achievements. Do not repost them.

If the article is helpful to you, you are welcome to donate to the author, support haomeng master, the amount of donation is free, focusing on your mind ^_^

I want to donate: Click to donate

Cocos2d-X source code download: point I send

Game official download: http://dwz.cn/RwTjl

Game video preview: http://dwz.cn/RzHHd

Game Development blog: http://dwz.cn/RzJzI

Game source code transfer: http://dwz.cn/Nret1

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.