Management of iOS files (add, delete, copy, move)

Source: Internet
Author: User
Tags file copy

#import "ViewController.h"

@implementation Viewcontroller

-(void) Viewdidload {

[Super Viewdidload];

[Self handlensfilemanage];

}

File Management

-(void) handlensfilemanage{

Nsfilemanager is a singleton class, which we call a file management class, is a tool designed to manage files, the main functions are: File additions, file deletion, file movement, file copy;

Create a file Management object

Nsfilemanager *filemanage = [Nsfilemanager Defaultmanager];

//1. File Additions

For example, to create a File1 folder under the Documents folder

① first to obtain the Documents folder path

NSString *documentspath = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, NSUserDomainMask,YES) Lastobject];

② then need to prepare the file path to be created

NSString *file1path = [Documentspath stringbyappendingpathcomponent:@ "Flie1"];

③ Creating a Folder

Parameter 1: File path

Parameter 2: If there are already other directories in the file, do you want to create

Parameter 3, 4: attribute, error message, nil

Used to determine if the file to be created exists

BOOL Ishave = [Filemanage Fileexistsatpath:file1path];

if (Ishave) {

NSLog (@ "file already exists");

}else{

NSLog (@ "file does not exist");

BOOL issuccess = [filemanage createdirectoryatpath:file1path withintermediatedirectories:yes Attributes:nil Error:nil ];

NSLog (@ "%@", issuccess @ "Create succeeded": @ "Create failed");

}

NSLog (@ "%@", File1path);

//2. Deletion of files

Determine if the file to be deleted exists

if ([Filemanage Fileexistsatpath:file1path]) {

NSLog (@ "file exists");

Delete

BOOL issuccess = [Filemanage removeitematpath:file1path error:nil];

NSLog (@ "%@", issuccess @ "Delete succeeded": @ "Delete failed");

}else{

NSLog (@ "file does not exist");

}

//3. Copy of File

Simple demonstration: Prepare a Love.txt file to be dragged into the project and copied to the File1 folder

① get the file path to copy

NSString *lovepath = [[NSBundle mainbundle]pathforresource:@ "Love" oftype:txt];

② preparing to copy the past file path

NSString *tolovepath = [File1path stringbyappendingpathcomponent:@ "Love.txt"];

Simple to determine if a copy of the past file path exists

if (![ Filemanage Fileexistsatpath:tolovepath]) {

BOOL issuccess = [filemanage copyitematpath:lovepath topath:tolovepath Error:nil];

NSLog (@ "%@", issuccess @ "copy succeeded": @ "copy failed");

}

//4. File Movement

Example: Move the File1 file to the Library folder

① getting the library file path

NSString *librarypath = [Nssearchpathfordirectoriesindomains (nslibrarydirectory, Nsuserdomainmask, YES) lastObject];

Get the path to add file1 to the LibraryPath file

NSString *tofilepath = [LibraryPath stringbyappendingpathcomponent:@ "File1"];

if (![ Filemanage Fileexistsatpath:tofilepath]) {

BOOL issuccess = [filemanage moveitematpath:file1path topath:tofilepath Error:nil];

NSLog (@ "%@", Issuccess @ "mobile success": @ "Move failed");

}

Calculate the size of a file by Nsfilemanage

Calculate the file size under the Tolovepath path

Nsdictionary *info = [Filemanage attributesofitematpath:tolovepath error:nil];

NSLog (@ "%LFM", info.filesize/1024.0/1024.0);

}

Management of iOS files (add, delete, copy, move)

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.