Nsfilehandle&&nsfilemanage

Source: Internet
Author: User

/*********** Read all file contents ************/

nsfilehandle *handle1 = [nsfilehandle filehandleforreadingatpath: FilePath]; returns the filehandle of the Read file

nsdata *data1 = [handle1 readdatatoendoffile]; read to the end of the file to get the data in the file

nsstring *readstring=[[nsstring alloc]initwithdata:d ata1 encoding: Nsutf8stringencoding]; //Convert data into a string

NSLog(@ "%@", readString);

/************* Read half of the file to the end ************/

nsfilehandle *handle=[nsfilehandle filehandleforreadingatpath: FilePath];

nsinteger length=[handle availabledata]. Length; returns the length of valid data in a file

[Handle Seektofileoffset: length/2]; jump to the content of the file half of the place

nsdata *data=[handle readdatatoendoffile]; read to end of file

nsstring *string=[[nsstring alloc]initwithdata:d ata encoding: nsutf8stringencoding];

NSLog(@ "%@", String);

}

-(void) Writeingfile: (nsstring *) filePath

{

// write data to the end of the file (append)

nsfilehandle *handle2=[nsfilehandle filehandleforupdatingatpath: FilePath]; Prepare for updates

[Handle2 seektoendoffile]; jump to end of file

nsstring *addstring=@ " Shang education ";

nsdata *writedata=[addstring datausingencoding:nsutf8stringencoding]; converting a string into data

[Handle2 writedata: writedata]; Write Data

[Handle2 closefile]; Close file

}

-(void) Copyingfile: (nsstring *) filePath

{

// Find the original file path

nsstring *old_filepath = filePath;

// Specify a new file path (file does not exist)

nsstring *new_filepath = @ "/users/scjy/hello/ios.txt";

// Create a File manager to prepare for creating a new file

nsfilemanager *filemanager=[nsfilemanager defaultmanager];

// Create a new file (no data in the file at this time)

BOOL issuccess=[filemanager createfileatpath: New_filepath contents:nil Attributes:nil];

if (issuccess) {

NSLog(@ " Create success ");

//1. read data from the original file:filehandleforreadingatpath

nsfilehandle *old_handle=[nsfilehandle filehandleforreadingatpath: Old_filepath];

nsdata *old_data=[old_handle readdatatoendoffile];

//2. writes the data obtained from the original file to a new file:filehandleforwritingatpath

nsfilehandle *new_handle=[nsfilehandle filehandleforwritingatpath: New_filepath];

[New_handle writedata: old_data];

[Old_handle closefile];

[New_handle closefile];

}

Else

{

NSLog(@ " Create failed ");

}

}

-(void) Creatplistfile

{

nsfilemanager *filemanager=[nsfilemanager defaultmanager];

nsstring *path=@ "/users/scjy/hello/student.plist";

//fileexistsatpath: Judging if the file is not present

If (![ FileManager fileexistsatpath:p Ath]) {

NSLog(@ " file does not exist, start creating file ");

[FileManager createfileatpath:p ath contents:nil attributes:nil] ; // Create file

//nsdictionary *dic=[nsdictionary dictionarywithobject:@ "Li heping " forkey:@ "name"];

nsarray *array=[nsarray arraywithobjects:@ "A",@ "B", Nil];

[Array writetofile:p ath atomically:YES]; Write file

}

Else

{

NSLog(@ " file already exists ");

}

nsarray *array1=[nsarray arraywithcontentsoffile:p ath];

NSLog(@ "%@", array1);

}

class Notes/////////////////////////////////////////////////// /

#if 0

Moving Files

Nsfilemanager *manager=[nsfilemanager Defaultmanager];

NSString *path1=@ "/users/liheping/desktop/move.txt";

NSString *path2=@ "/users/liheping/desktop/test1/move.txt";

Nserror *error=nil;

can modify the name of the file (path2 cannot exist, indicating the file moved over)

BOOL isok=[manager moveitematpath:path1 topath:path2 error:&error];

If (IsOK) {

NSLog (@ " operation succeeded ");

}Else

NSLog (@ " operation failed :%@", [Error localizeddescription]);

**/

// renaming

Nsfilemanager *manager=[nsfilemanager Defaultmanager];

NSString *path1= @ "/USERS/LIHEPING/DESKTOP/1";

NSString *path2=@ "/USERS/LIHEPING/DESKTOP/2";

Nserror *error=nil;

BOOL isok=[manager moveitematpath:path1 topath:path2 error:&error];

if (IsOK) {

NSLog (@ " operation succeeded ");

}Else

NSLog (@ " operation failed :%@", [Error localizeddescription]);

// copy:

Nsfilemanager *manager=[nsfilemanager Defaultmanager];

NSString *path1=@ "/users/liheping/desktop/move/test.txt";

NSString *path2=@ "/users/liheping/desktop/2/test1.txt";

Nserror *error=nil;

BOOL isok=[manager copyitematpath:path1 topath:path2 error:&error];

if (IsOK) {

NSLog (@ " operation succeeded ");

}Else

NSLog (@ " operation failed :%@", [Error localizeddescription]);

}

#endif

Nsfilehandle&&nsfilemanage

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.