iOS read file classes and common methods

Source: Internet
Author: User

First, Nsfilehandle

The Nsfilemanager class mainly operates on files (delete, modify, move, copy, etc.)

The Nsfilehandle class mainly reads and writes the contents of a file

Second, the Nsfilehandle class processes the file the step

Create a Nsfilehandle object

To i/0 an open file

Close File

You can use Nsfilehandle to make a breakpoint continuation

Third, the code to implement the lookup function:

NSString *homepath=nshomedirectory ();

NSString *filepath=[homepath stringbyappendingpathcomponent:@ "Desktop/hello.rtf"];

Nsfilehandle *filehandle=[nsfilehandle Filehandleforreadingatpath:filepath];

Nsuinteger Length=[filehandle availabledata].length;

[FileHandle SEEKTOFILEOFFSET:LENGTH/2];

NSData *data=[filehandle Readdatatoendoffile];

NSString *str=[[nsstring alloc] Initwithdata:data encoding:nsutf8stringencoding];

NSLog (@ "%@", str);

Implementation of append file code:

NSString *homepath=nshomedirectory ();

NSString *filepath=[homepath stringbyappendingpathcomponent:@ "Desktop/hello.rtf"];

Nsfilehandle *filehandle=[nsfilehandle Filehandleforupdatingatpath:filepath];

[FileHandle Seektoendoffile];

[FileHandle Seektofileoffset:10];

NSString *str=@ "Data";

NSData *data=[str datausingencoding:nsutf8stringencoding];

[FileHandle Writedata:data];

[FileHandle CloseFile];

Insert code here ...

NSLog (@ "Hello, world!");

The code to implement the copied file:

NSString *homepath=nshomedirectory ();

NSString *filepath=[homepath stringbyappendingpathcomponent:@ "Desktop/hello.rtf"];

NSString *objpath=[homepath stringbyappendingpathcomponent:@ "Desktop/copy.rtf"];

Nsfilemanager *filemanager=[nsfilemanager Defaultmanager];

BOOL Success=[filemanager Createfileatpath:objpath Contents:nil Attributes:nil];

Nsfilehandle *writefile=[nsfilehandle Filehandleforreadingatpath:filepath];

Nsfilehandle *objfile=[nsfilehandle Filehandleforwritingatpath:objpath];

[objFILE Readdatatoendoffile];

NSData *data=[writefile Readdatatoendoffile];

[objFILE Writedata:data];

[WriteFile CloseFile];

[objFILE 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.