IOS File Management 2

Source: Internet
Author: User

iOS Development-file Management (ii)

V.. plist file

String method to add

NSString *path = [Nshomedirectory () stringbyappendingpathcomponent:@ "Array.plist"];

NSString *content = @ "ABCD";

[Contect Writetofile:path atomically:yes encoding:nsutf8stringencoding Error:nil];

Array mode add

NSString *path = [Nshomedirectory () stringbyappendingpathcomponent:@ "Array.plist"];

[Nsarray *array = [[Nsarray alloc] initwithobjects:@ "123", @ "798", @ "$", nil]; [Array Writetofile:path atomically:yes];

Dictionary ways to add

NSString *path = [Nshomedirectory () stringbyappendingpathcomponent:@ "Dic.plist"];                     

Nsdictionary *dic = [Nsdictionary alloc] initwithobjects:@ "First", @ "second", @ "third" forkeys:@ "123", @ "456", @ "798"]; [DiC Writetofile:path Atomically:yes];

    • Arrays, dictionaries can only write bool, NSNumber, NSString, NSData, NSDate, Nsarray, nsdictionary to the attribute list plist file

Six, read the file class and common methods

    • The Nsfilehandle class primarily reads and writes the contents of a file

    • The Nsfilemanager class primarily operates on files (delete, modify, move, copy, and so on)

Common processing methods

+ (ID) Filehandleforreadingatpath: (NSString *) path opens a file ready to read

+ (ID) Filehandleforwritingatpath: (NSString *) path opens a file ready to write

+ (ID) Filehandleforupdatingatpath: (NSString *) path opens a file ready for update

-(NSData *) Availabledata; Returning available data from a device or channel

-(NSData *) readdatatoendoffile; Reads from the current node to the end of the file

-(NSData *) Readdataoflength: (nsuinteger) length; Reads the specified length data starting from the current node

-(void) WriteData: (NSData *) data; Write Data

-(unsigned long long) offsetinfile; Gets the offset of the current file

-(void) Seektofileoffset: (unsigned long long) offset; Jumps to the offset of the specified file

-(unsigned long long) seektoendoffile; Jump to end of file

-(void) Truncatefileatoffset: (unsigned long long) offset; Set the length of the file to offset bytes

-(void) closefile; Close File

Append data to a file

NSString *homepath = Nshomedirectory ();         

NSString *sourcepath = [HomePath stringbyappendingpathconmpone:@ "Testfile.text"];

Nsfilehandle *fielhandle = [Nsfilehandle Filehandleforupdatingatpath:sourcepath];

[FileHandle Seektoendoffile]; To jump a node to the end of a file

NSString *str = @ "Append data"

nsdata* stringdata = [str datausingencoding:nsutf8stringencoding];

[FileHandle Writedata:stringdata]; Append Write Data

[FileHandle CloseFile];

Locating data

Nsfilemanager *FM = [Nsfilemanager Defaultmanager];

NSString *content = @ "abcdef";

[FM Createfileatpath:path contents:[content datausingencoding:nsutf8stringencoding] attributes:nil];

Nsfilehandle *filehandle = [Nsfilehandle Filehandleforreadingatpath:path];

Nsuinteger length = [filehandle availabeldata] length]; Get Data length

[FileHandle SEEKTOFILEOFFSET;LENGTH/2]; Half of the offset file

NSData *data = [FileHandle readdatatoendoffile];

[FileHandle CloseFile];

Copying files

Nsfilehandle *infile, *outfile; Input file, output file

NSData *buffer; Buffered data read

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 read source Path file

if (infile = = nil)

{

return NO;

}

outfile = [Nsfilehandle Filehandleforreadingatpath:outpath]; Create a disease open file to output

if (outfile = = nil)

{

return NO;

}

[OutFile truncatefileatoffset:0]; Set the length of the output file to 0

Buffer = [infile readdatatoendoffile]; Reading data

[OutFile Writedata:buffer]; Write input

[InFile CloseFile]; Close write, input file

[OutFile CloseFile];

IOS File Management 2

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.