Nsfilehandle, Nsfilemange

Source: Internet
Author: User

The use of Nsfilehandle has been not proficient, inadvertently found the detailed introduction, showings convenient usage

Nsfilehandle This class is primarily read and write to the file contents

Nsfilemange This class is primarily the operation of files and the acquisition of file information

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

Nsfilehandle, Nsfilemange

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.