Copy of IOS implementation files

Source: Internet
Author: User

//

Main.m

FileCopy

//

Created by Guoyule on 15/2/19.

Copyright (c) 2015 Guoyule. All rights reserved.

//

#import <Foundation/Foundation.h>

Implementing a copy of a file

#define Srcpath @ "/users/guoyule/desktop/emailreceipt_20150214r3887454299_new.pdf"

#define Dstpath @ "/users/guoyule/desktop/emailreceipt_20150214r3887454299_new1.pdf"

#define PERROR (Error) if (error) {NSLog (@ "%@", error); exit (-1);}

#define BUFF 100

Buffer size

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

@autoreleasepool {

The so-called file copy is read from the original document to the destination file.

Create a file first

NSFILEMANAGER * FM =[nsfilemanager Defaultmanager];

Nserror * error = NIL;

Get the properties of a source file

Nsdictionary *attributes = [FM attributesofitematpath:srcpatherror:&error];

Perror (Error);

if (Error) {NSLog (@ "%@", error); exit (-1);};

Create a new file

BOOL RET = [FM createfileatpath:dstpath contents:nil Attributes:attributes];

if (!ret) {

Perror ("CreateFile");

Exit (-1);

}

Open File Handle

Nsfilehandle * SRCFH = [Nsfilehandlefilehandleforreadingatpath:srcpath];

Nsfilehandle * DSTFH = [Nsfilehandle Filehandleforwritingatpath:dstpath];

Don't read the source file into memory in a breath

First to get the source file size

size_t size = [[Attributes objectforkey:@ "Nsfilesize"] integervalue];

unsigned long long size = [Attributes fileSize];

This is a method that only works if the dictionary loaded file property is actually a category

/*

@interface Nsdictionary (nsfileattributes)

-(unsigned long long) fileSize;

-(NSDate *) filemodificationdate;

-(NSString *) FileType;

-(Nsuinteger) fileposixpermissions;

-(NSString *) fileowneraccountname;

-(NSString *) filegroupowneraccountname;

-(Nsinteger) Filesystemnumber;

-(Nsuinteger) Filesystemfilenumber;

-(BOOL) Fileextensionhidden;

-(OSType) Filehfscreatorcode;

-(OSType) Filehfstypecode;

-(BOOL) fileisimmutable;

-(BOOL) fileisappendonly;

-(NSDate *) filecreationdate;

-(NSNumber *) Fileowneraccountid;

-(NSNumber *) Filegroupowneraccountid;

@end

*/

while (size) {

NSData * data = nil;

if (size <= BUFF) {

data = [SRCFH readdatatoendoffile];

size = 0;

}else{

Read 100 bytes First

data = [SRCFH Readdataoflength:buff];

Size-= BUFF;

}

[DSTFH Writedata:data];

}

}

return 0;

}

Copy of IOS implementation files

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.