IOS NSFileHandle, iosnsfilehandle

Source: Internet
Author: User

IOS NSFileHandle, iosnsfilehandle

NSFileHandle is mainly used to read and write the file content.

NSFileMange is mainly used to perform operations on files and obtain file information.

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

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.