指定字节拷贝文件
2015-11-06 21:14
351 查看
#import "SizeCopy.h"
@implementation SizeCopy
-(void)accordingByteCopy:(NSInteger)size
{
//获取桌面上的文件路径,以便拷贝
NSString *oldPath=@"/Users/scjy/Desktop/video.mp4";
//指定将要拷贝到哪里
NSString *newPath=@"/Users/scjy/Desktop/movie.mp4";
//创建文件管理者,准备创建文件
NSFileManager *fileManager=[NSFileManager defaultManager];
//判断将要创建的文件是不是已经存在
BOOL isHave=[fileManager fileExistsAtPath:newPath];
if (!isHave) {
//不存在的话,开始执行创建,并判断是不是创建成功
BOOL isSec=[fileManager createFileAtPath:newPath contents:nil attributes:nil];
if (isSec) {
NSLog(@"文件创建成功,开始复制");
}
else
{
return;
}
}
NSFileHandle *oldHandle=[NSFileHandle fileHandleForReadingAtPath:oldPath];//读取文件的handle
NSFileHandle *newHandle=[NSFileHandle fileHandleForUpdatingAtPath:newPath];//写入文件的handle
//表示文件已经读取过,指针已经移动到数据的最后一位
//NSLog(@"%ld",[oldHandle availableData].length);--有准确值
//NSLog(@"%ld",[oldHandle availableData].length);--值为0
//attributesOfItemAtPath获取文件的大小,内容等方法
NSDictionary *dictionary=[fileManager attributesOfItemAtPath:oldPath error:nil];
//返回文件的有效内容大小
NSNumber *lenNum=[dictionary valueForKey:NSFileSize];
NSInteger fileLength=[lenNum integerValue];//转成基本数据类型
NSInteger readLength=0;
BOOL isEnd=NO;
while (!isEnd) {
NSData *data=nil;
//获取剩余未拷贝文件长度
NSInteger subLegth=fileLength-readLength;
//判断是不是最后一次节点复制文件
if (subLegth<size) {
isEnd=YES;
[oldHandle readDataToEndOfFile];//读取到文件末尾
NSLog(@"拷贝完成:100%@",@"%");
}
else
{
data=[oldHandle readDataOfLength:size];//读取若干字节
readLength+=size;
//跳转到拷贝结束的文件节点
[oldHandle seekToFileOffset:readLength];
//计算拷贝比例
NSNumber *readNum=[NSNumber numberWithInteger:readLength];
NSLog(@"正在拷贝:%.3f%@",[readNum doubleValue]/[lenNum doubleValue]*100,@"%");
}
[newHandle writeData:data];//写入文件
}
[oldHandle closeFile];//关闭文件
[newHandle closeFile];
}
@end
@implementation SizeCopy
-(void)accordingByteCopy:(NSInteger)size
{
//获取桌面上的文件路径,以便拷贝
NSString *oldPath=@"/Users/scjy/Desktop/video.mp4";
//指定将要拷贝到哪里
NSString *newPath=@"/Users/scjy/Desktop/movie.mp4";
//创建文件管理者,准备创建文件
NSFileManager *fileManager=[NSFileManager defaultManager];
//判断将要创建的文件是不是已经存在
BOOL isHave=[fileManager fileExistsAtPath:newPath];
if (!isHave) {
//不存在的话,开始执行创建,并判断是不是创建成功
BOOL isSec=[fileManager createFileAtPath:newPath contents:nil attributes:nil];
if (isSec) {
NSLog(@"文件创建成功,开始复制");
}
else
{
return;
}
}
NSFileHandle *oldHandle=[NSFileHandle fileHandleForReadingAtPath:oldPath];//读取文件的handle
NSFileHandle *newHandle=[NSFileHandle fileHandleForUpdatingAtPath:newPath];//写入文件的handle
//表示文件已经读取过,指针已经移动到数据的最后一位
//NSLog(@"%ld",[oldHandle availableData].length);--有准确值
//NSLog(@"%ld",[oldHandle availableData].length);--值为0
//attributesOfItemAtPath获取文件的大小,内容等方法
NSDictionary *dictionary=[fileManager attributesOfItemAtPath:oldPath error:nil];
//返回文件的有效内容大小
NSNumber *lenNum=[dictionary valueForKey:NSFileSize];
NSInteger fileLength=[lenNum integerValue];//转成基本数据类型
NSInteger readLength=0;
BOOL isEnd=NO;
while (!isEnd) {
NSData *data=nil;
//获取剩余未拷贝文件长度
NSInteger subLegth=fileLength-readLength;
//判断是不是最后一次节点复制文件
if (subLegth<size) {
isEnd=YES;
[oldHandle readDataToEndOfFile];//读取到文件末尾
NSLog(@"拷贝完成:100%@",@"%");
}
else
{
data=[oldHandle readDataOfLength:size];//读取若干字节
readLength+=size;
//跳转到拷贝结束的文件节点
[oldHandle seekToFileOffset:readLength];
//计算拷贝比例
NSNumber *readNum=[NSNumber numberWithInteger:readLength];
NSLog(@"正在拷贝:%.3f%@",[readNum doubleValue]/[lenNum doubleValue]*100,@"%");
}
[newHandle writeData:data];//写入文件
}
[oldHandle closeFile];//关闭文件
[newHandle closeFile];
}
@end
相关文章推荐
- 360我要接单登陆-Token解密Java代码
- hdoj4920Matrix multiplication【矩阵相乘】
- hdu 4436 str2int(后缀自动机)
- Learning Multi-Domain Convolutional Neural Networks for Visual Tracking 笔记
- 学习贪吃蛇JS项目实战笔记3
- 典型用户,用户故事,用例图,练习题
- def函数之另类用法
- Fragment基础点滴学习---Activity传递数据到Fragment(Fragment使用静态加载的方式)
- Spring字符编码过滤器
- 菜鸟win7双系统安装教程 两个系统都用Win7
- Remoting基本原理及其扩展机制(上)
- 成长编码4
- Hive与传统数据库比较
- 【Codeforces Round 329 (Div 2) A】【暴力枚举】2Char 选单词最多只含2字符
- HTML5 表单元素
- 树上最长距离 hdu5527、cf592D
- Spring中使用JDBC做的增删改查
- 成长编码3
- java日期和时间格式转换符
- 冬瓜你只会煮汤吗?其实还能这样吃!