oc--File Management

Source: Internet
Author: User

-----------File Management----------------

1, File Management class

File Management classes: This is a class that is designed to manage files (via singleton)

Nsfilemanager *manager = [Nsfilemanager Defaultmanager];

NSString *path = @ "/users/desktop/shared data";

Error can be used to find out if the contents of the directory is successfully error: This parameter to write the object pointer address

Nserror *error = nil;

Get the contents of the file in the directory Note: 1 can only get one layer, 2. To give the full path

Nsarray *array1 = [Manager Contentsofdirectoryatpath:path error:&error];

Find a file name with a suffix named RTF

For (NSString *name in array1) {

if ([Name hassuffix:@ ". rtf"]) {

NSLog (@ "%@", name);

}

}

Method Two

To take the file extension

For (NSString *name in array1) {

NSString *str = [name pathextension];

NSLog (@ "%@", str);

}

Take each part of the path

Nsarray *comarr = [path pathcomponents];

Take all the files in the current directory

Get all the files under the current directory (files in the current directory and files within subfolders) to get a multi-layered note: should be the full path

Nsarray *array2 = [manager Subpathsofdirectoryatpath:path Error:nil];       

2. Create a file

NSString *path = @ "/users/desktop/shared data";

First parameter: The path to a file

Second parameter: The contents of a file

Third parameter: The properties of the file, if empty, that is the default property

The file name created is preferably new, and if it has the same name as the original, it will be overwritten

BOOL ISSUC = [manager Createfileatpath:path Contents:nil Attributes:nil];

Gets the properties of the current file

Nsdictionary *dic = [manager Attributesofitematpath:path Error:nil];

Create path

/**

First: the path to create

Second: When no, if the newly created path is added in the middle of the original part, it will not allow the creation when Yes, will automatically allow the increase, and create

*/

NSString *path1 = @ "/users/desktop/shared data/good/";

BOOL ISSUC1 = [Manager createdirectoryatpath:path1 withintermediatedirectories:yes Attributes:nil Error:nil];

3. Moving, deleting and copying files

BOOL ISSUC1 = [manager copyitematpath:path1 topath:path2 Error:nil]; Copy

BOOL ISSUC2 = [manager moveitematpath:path3 Topath:path4 Error:nil]; Move

BOOL ISSUC3 = [manager Removeitematpath:path4 Error:nil]; Delete

-----------nsdata--------

NSData: Represents binary Data--the reason: storage, the transfer of data are binary.

NSString *string = @ "study";

String converted to NSData type

NSData *data = [string datausingencoding:nsutf8stringencoding];         

NSData type to String type

NSString *newstring = [[NSString alloc]initwithdata:data encoding:nsutf8stringencoding];

--Open File----------

OC Open method: File Handle (Nsfilehandle): The file handle is used to manipulate the contents of the file.

Three ways: Read only, write only, read and write

Read-Only open file

1. Create a handle

Nsfilehandle *handle = [[Nsfilehandle alloc]init];

Path to the file

NSString *path3 = @ "/users/desktop/method list. rtf";

Nsfilehandle *handle = [Nsfilehandle filehandleforreadingatpath:path3];

if (handle) {NSLog (@ "file exists");        }else{NSLog (@ "does not exist"); }

Open file with write-only mode

Nsfilehandle *handle2 = [Nsfilehandle filehandleforwritingatpath:path3];

if (handle2) {NSLog (@ "file exists");        }else{NSLog (@ "does not exist"); }

Read-write mode open file

Nsfilehandle *handle3 = [Nsfilehandle filehandleforupdatingatpath:path3];

if (handle3) {NSLog (@ "file exists");        }else{NSLog (@ "does not exist"); }

---Read file

Nsfilehandle *handle = [Nsfilehandle filehandleforupdatingatpath:path3];

2. First read: Read length from the beginning of the character

NSData *data1 = [handle readdataoflength:10];

NSLog (@ "%@", [[NSString alloc]initwithdata:data1 encoding:nsutf8stringencoding]);

Second reading: The end of the above is the beginning, reading 10 characters

NSData *DATA2 = [handle readdataoflength:10];

NSLog (@ "%@", [[NSString alloc]initwithdata:data2 encoding:nsutf8stringencoding]);

Read the third read from the last time to the end

NSData *data3 = [handle readdatatoendoffile];

NSLog (@ "%@", [[NSString alloc]initwithdata:data3 encoding:nsutf8stringencoding]);

Empty file

[Handle truncatefileatoffset:0];

Go back to the beginning of the file (or a location)

[Handle seektofileoffset:0];

Can only go back to the beginning

[Handle offsetinfile];

To the end

[Handle seektoendoffile];

Close file (can be omitted)

[Handle closefile];

---Write file

Note: To prevent overwriting the original content, 1. First to the end of the file content 2. Start writing (unless you want to overwrite the original content)

To the end of the content

[Handle seektoendoffile];

NSData *data = [@ "Our Stories" datausingencoding:nsutf8stringencoding];

Write Data note: Write data to the cache

[Handle writedata:data];

Write data to disk

[Handle synchronizefile];

Close file (can be omitted)

[Handle closefile];

oc--File Management

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.