1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| NSFileHandle *infile, *outfile; //输入文件、输出文件 NSData *buffer; //读取的缓冲数据 NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *homePath = NSHomeDirectory( ); NSString *sourcePath = [homePath stringByAppendingPathComponent:@"testfile.txt"]; // 源文件路径 NSString *outPath = [homePath stringByAppendingPathComponent:@"outfile.txt"]; //输出文件路径 BOOL sucess = [fileManager createFileAtPath:outPath contents:nil attributes:nil]; if (!success) { return N0; } infile = [NSFileHandle fileHandleForReadingAtPath:sourcePath]; //创建读取源路径文件 if (infile == nil) { return NO; } outfile = [NSFileHandle fileHandleForReadingAtPath:outPath]; //创建并打开要输出的文件 if (outfile == nil) { return NO; } [outfile truncateFileAtOffset:0]; //将输出文件的长度设为0 buffer = [infile readDataToEndOfFile]; //读取数据 [outfile writeData:buffer]; //写入输入 [infile closeFile]; //关闭写入、输入文件 [outfile closeFile];
|