Specify byte copy File

Source: Internet
Author: User

#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

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.