Object-C 学习笔记(二十五)--- 文件操作(写文件/读文件/复制文件)

本文介绍了使用Objective-C语言通过NSFileHandle和NSFileManager类进行文件的基本操作,包括读取、写入和复制文件。详细步骤涵盖了文件路径的拼接、数据读写和文件管理的基本流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文件操作:读取文件,写入文件,复制文件

设计的主要类:

1.NSFileHandle ---- 文件写入/读取
2.NSFileManager --- 文件管理


实例:

//
//  main.m
//  appendData
//
//  Created by 5016 on 13-12-17.
//  Copyright (c) 2013年 dradon. All rights reserved.
//

#import <Foundation/Foundation.h>

/*
 *读取文件
 */
void readFile()
{
    //读取文件
    NSString *homePath = NSHomeDirectory();//获取根目录
    NSLog(@"%@",homePath);
    NSString *filePath = [homePath stringByAppendingPathComponent:@"Desktop/appendData/dragon.txt"];//拼接文件路径
    NSLog(@"%@",filePath);
    /*******************读文件********************/
    //1.实例化文件引用
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
    //2.寻找文件数据位
    NSUInteger len = [fileHandle availableData].length;//获取数据有效长度
    NSLog(@"%ld",len);
    [fileHandle seekToFileOffset:3];//寻找数据一半的地方
    //3.读取数据
    NSData *data =[fileHandle readDataToEndOfFile];
    //4.转换数据
    NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"str = %@",str);
    //5.关闭文件
    [fileHandle closeFile];
}


/*
 *写文件
 */
void writeFile()
{
    //写文件
    NSString *homePath = NSHomeDirectory();//获取根目录
    NSLog(@"%@",homePath);
    NSString *filePath = [homePath stringByAppendingPathComponent:@"Desktop/appendData/dragon.txt"];//拼接文件路径
    NSLog(@"%@",filePath);
    /*******************写文件********************/
    //1.实例化文件引用
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
    //2.寻找文件数据位置
    //[fileHandle seekToFileOffset:20];//设置偏移量
    [fileHandle seekToEndOfFile];
    NSString *str = @"追加的数据";
    //3.数据进行编码
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];//编码
    //4.写数据
    [fileHandle writeData:data];
    //5.关闭文件
    [fileHandle closeFile];
}

/*
 *拷贝文件
 */
void copyFile()
{
    //拷贝文件
    NSString *homePath = NSHomeDirectory();//获取根目录
    NSLog(@"%@",homePath);
    NSString *filePath = [homePath stringByAppendingPathComponent:@"Desktop/appendData/dragon.txt"];//拼接文件路径
    NSLog(@"%@",filePath);
    NSString *targetPath = [homePath stringByAppendingPathComponent:@"Desktop/test.txt"];//拼接文件路径
    /*******************复制文件********************/
    //1.实例化filemanager
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //2.创建文件
    BOOL flag = [fileManager createFileAtPath:targetPath contents:nil attributes:nil];
    if (flag) {
        NSLog(@"创建成功");
    }
    else{
        NSLog(@"创建失败");
    }

    NSFileHandle *outFileHandle = [NSFileHandle fileHandleForWritingAtPath:targetPath];//写管道
    NSFileHandle *inFileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath];//读管道
    //3.读数据
    NSData *data =[inFileHandle readDataToEndOfFile];
    //4.写数据
    [outFileHandle writeData:data];
    //5.关闭文件
    [outFileHandle closeFile];
    [inFileHandle closeFile];
}

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        //writeFile();
        //readFile();
        copyFile();
    }
    return 0;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值