Objective-c file and directory operations, iOS file operations, Nsfilemanager using file operations

Source: Internet
Author: User

Transfer from http://blog.csdn.net/swingpyzf/article/details/15185767

Objective-c uses the Nsfilemanager class to manage and manipulate files, directories, Nsfilemanager, files, or directories that are unique to the path name of the file used. Each pathname is a NSString object.

Nsfilemanager objects create instances by using the Defaultmanager method
Gca

Nsfilemanager *FM = [Nsfilemanager Defaultmanager];


Delete a file
[FM removeitematpath:@ "filename" error:null];

Error: The parameter is a pointer to the Nserror object that can provide the wrong information. If NULL is specified, the default behavior is used, and the return value is a method of type bool, and the operation successfully returns YES instead of returning no


Determine if a file has been deleted
if ([FM removeitematpath:@ "filename" error:null]==no) {
NSLog (@ "File deletion failed");
return 1;
}


Nsfilemanager commonly used file methods:

-(nsdata*) Contentsatpath:path reading data from a file

-(BOLL) Createfileatpath:path contents: (nsdata*) data attributes:attr write to a file

-(BOOL) Removeitematpath:path error:err Delete a file

-(BOOL) moveitematpath:from topath:to error:err Rename or move a file (to cannot be existing)

-(BOOL) copyitematpath:from topath:to error:err Copy file (to cannot be existing)

-(BOOL) contentsequalatpath:path1 andpath:path2 Compare the contents of these two files

-(BOOL) fileexistsatpath:path test file exists

-(BOOL) Isreadablefileatpath:path test file exists and can perform read operations

-(BOOL) Iswritablefileatpath:path test whether the file exists and whether it can perform a write operation

-(nsdictionary*) Attributesofitematpath:path Error:err Get the properties of the file

The property dictionary allows you to specify the permissions of the file to be created, and if the parameter is specified as nil, the file is set to the default permissions.


1, through a program to operate the file:

[CPP]View Plaincopyprint?

//

Main.m

Nsfilemanager_01

//

Created by Swinglife on 13-11-10.

Copyright (c) 2013 Swinglife. All rights reserved.

//

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])

{

@autoreleasepool {

Filename

NSString *filename = @"testfile";

NSString *filecontent = @"This is the file content!!!!";

NSData *filedata = [Filecontent datausingencoding:nsutf8stringencoding];

Creating an Nsfilemanager instance

Nsfilemanager *FM = [Nsfilemanager Defaultmanager];

Create a file

[FM createfileatpath:filename Contents:filedata Attributes:nil];

End the program by judging whether the file exists or not

if ([FM fileexistsatpath:filename]==no) {

NSLog (@"file does not exist");

return 1;

}

Copy files

if ([FM copyitematpath:filename topath:@"NewFile" Error:null]==no) {

NSLog (@"copy failed");

return 2;

}

Test if two files are the same

if ([FM contentsequalatpath:filename andpath:@"NewFile"]==no) {

NSLog (@"file is not the same");

return 3;

}

Renaming NewFile

[FM moveitematpath:@ "newFile" topath:@"NewFile2" error:null];

Get the size of the NewFile2

Nsdictionary *FILEATTR = [fm attributesofitematpath:@"NewFile2" error:null];

if (Fileattr!=nil) {

NSLog (@"File Size:%llu bytes", [[FileAttr Objectforkey:nsfilesize] unsignedlonglongvalue]);

}

deleting files

[FM removeitematpath:filename Error:null];

Display the contents of NewFile2

NSString *data = [NSString stringwithcontentsoffile:@"NewFile2" encoding:nsutf8stringencoding Error:NULL];

NSLog (@"%@", data);

}

return 0;

}


Nsfilemanager Common directory Methods


-(nsstring*) Currentdirectorypath get current directory

-(BOOL) Changecurrentdirectorypath:path change the current directory

-(BOOL) copyitematpath:from topath:to error:err Copy directory structure

-(BOOL) Createdirectoryatpath:path withintermediatedirectories: (BOOL) flag attributes:attr Create a new directory

-(BOOL) Fileexistsatpath:path isdirectory: (bool*) flag test file is not a directory (store results in flag)

-(nsarray*) contentsofdirectoryatpath:path Error:err Listing directory contents

-(nsdirectoryenumerator*) Enumeratoratpath:path The contents of the enumeration directory

-(BOOL) Removeitematpath:path error:err Delete Empty directory

-(BOOL) moveitematpath:from topath:to error:err Rename or move a directory


2, through a program to operate the directory:

[CPP]View Plaincopyprint?

//

Main.m

Nsfilemanager_02

//

Created by Swinglife on 13-11-10.

Copyright (c) 2013 Swinglife. All rights reserved.

//

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])

{

@autoreleasepool {

File directory

NSString *dirname = @"TestDir";

Creating an Nsfilemanager instance

Nsfilemanager *FM = [Nsfilemanager Defaultmanager];

Get current directory

NSString *PATH = [FM Currentdirectorypath];

NSLog (@"path:%@", Path);

Create a new directory

[FM createdirectoryatpath:dirname Withintermediatedirectories:yes Attributes:nil Error:null];

Rename a new directory

[FM moveitematpath:dirname topath:@"Newdir" error:null];

Change the current directory to a new directory

[FM changecurrentdirectorypath:@"Newdir"];

Get Current working directory

PATH = [FM Currentdirectorypath];

NSLog (@"path:%@", Path);

}

return 0;

}

Nsfilemanager's Subpathsatpath: and Contentsofdirectoryatpath:error: The same points and differences

Nsfilemanager-(Nsarray *) Contentsofdirectoryatpath: (NSString *)path error: (NSERROR *)error

and -(Nsarray *) Subpathsatpath: (NSString *)path

Can be used to get a list of subkeys (Files or folders) under the specified directory , and they will ignore the directory when they get the list of subkeys. And ' ... ', the order of the list of subkeys obtained is not fixed (two times the order of the child items returned by the same operation may be different).

The difference is that the former gets a list of children in a non-recursive way, while the latter gets a list of children recursively.

Objective-c file and directory operations, iOS file operations, Nsfilemanager using file operations

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.