//
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