#import "SizeCopy.h"
@implementation Sizecopy
-(void) Accordingbytecopy: (nsinteger) size
{
Gets the file path on the desktop for copying
NSString *[email protected] "/users/scjy/desktop/video.mp4";
Specify where you want to copy
NSString *[email protected] "/users/scjy/desktop/movie.mp4";
Create a File manager, prepare to create a file
Nsfilemanager *filemanager=[nsfilemanager Defaultmanager];
Determine if the file that will be created already exists
BOOL Ishave=[filemanager Fileexistsatpath:newpath];
if (!ishave) {
Does not exist, start execution creation and determine if the creation was successful
BOOL Issec=[filemanager Createfileatpath:newpath Contents:nil Attributes:nil];
if (issec) {
NSLog (@ "File creation succeeded, start copying");
}
Else
{
Return
}
}
Nsfilehandle *oldhandle=[nsfilehandle filehandleforreadingatpath:oldpath];//Read the handle of the file
Nsfilehandle *newhandle=[nsfilehandle filehandleforupdatingatpath:newpath];//Writing to a file handle
Indicates that the file has been read and the pointer has moved to the last digit of the data
NSLog (@ "%ld", [Oldhandle availabledata].length);--with accurate values
NSLog (@ "%ld", [Oldhandle availabledata].length);--value 0
Attributesofitematpath get the file size, content, etc methods
Nsdictionary *dictionary=[filemanager Attributesofitematpath:oldpath Error:nil];
Returns the valid content size of the file
NSNumber *lennum=[dictionary Valueforkey:nsfilesize];
Nsinteger filelength=[lennum integervalue];//to basic data types
Nsinteger readlength=0;
BOOL Isend=no;
while (!isend) {
NSData *data=nil;
Get the remaining non-copied file length
Nsinteger sublegth=filelength-readlength;
Determine if the last node copied the file
if (sublegth<size) {
Isend=yes;
[Oldhandle readdatatoendoffile];//read to end of file
NSLog (@ "Copy complete: 100%@", @ "%");
}
Else
{
Data=[oldhandle readdataoflength:size];//reads several bytes
Readlength+=size;
Jump to the end of the Copy file node
[Oldhandle Seektofileoffset:readlength];
Calculate Copy Scale
NSNumber *readnum=[nsnumber Numberwithinteger:readlength];
NSLog (@ "Copying:%.3f%@", [readnum doublevalue]/[lennum doublevalue]*100,@ "%");
}
[Newhandle writedata:data];//Write file
}
[Oldhandle closefile];//Close File
[Newhandle CloseFile];
}
@end
Specify byte copy File