存取文件

本文详细介绍了iOS中如何使用Objective-C进行文件操作,包括获取不同类型的目录路径、创建和删除文件、读写文件内容等,并提供了具体的代码实现。

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

    //获取Documents目录

    NSString *home=NSHomeDirectory();

    NSLog(@"%@",home);

    NSString *documents=[home stringByAppendingPathComponent:@"Documents/text.txt"];

    NSLog(@"%@",documents);

    

    

    //获取主包目录

    NSString *path=[[NSBundle mainBundle]bundlePath];

    NSLog(@"%@",path);

    

    NSLog(@"-----------");

    NSString *tmp=NSTemporaryDirectory();

    NSLog(@"%@",tmp);

    NSLog(@"++++++++++");

    

    

    //查找某类型文件的路径

    NSArray *array=NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);

    NSLog(@"%@",array);

    NSString *str=array[0];

    NSLog(@"%@",str);

    

    

    

    

    

    NSLog(@"---create---");

    

    //创建文件

    NSFileManager *file=[NSFileManager defaultManager];

    if ([file createFileAtPath:documents contents:[documents dataUsingEncoding:NSUTF8StringEncoding] attributes:nil]) {

        NSLog(@"--yse");

    }

    

    

    //删除文件

//    if ([file removeItemAtPath:documents error:nil]) {

//        NSLog(@"yse");

//    }

    

    

    

    //创建文件夹

//    if ([file createDirectoryAtPath:documents withIntermediateDirectories:YES attributes:nil error:nil]) {

//        NSLog(@"++yes");

//    }

    

//    NSString *str1= [[NSString alloc]initWithData:[file contentsAtPath:documents] encoding:NSUTF8StringEncoding];

//    NSLog(@"---%@",str1);

//    

    

    //以只写方式打开文件

    NSFileHandle *write=[NSFileHandle fileHandleForWritingAtPath:documents];

    

    

    //跳到文件末尾

    [write seekToEndOfFile];

    

    //写入文件

    [write writeData:[documents dataUsingEncoding:NSUTF8StringEncoding]];

    NSLog(@"%llu",write.offsetInFile);

    

    //关闭文件

    [write closeFile];

    

    

    //以只读方式打开文件

    NSFileHandle *read=[NSFileHandle fileHandleForReadingAtPath:documents];

    

    

    //读取文件内容

    NSString *s=[[NSString alloc]initWithData:[read readDataToEndOfFile] encoding:NSUTF8StringEncoding];

    NSLog(@"%@",s);

    

    //关闭文件

    [read closeFile];

    

    

    //以可读可写的方式打开文件

    NSFileHandle *update=[NSFileHandle fileHandleForUpdatingAtPath:documents];

    

    //跳到指定位置

    [update seekToFileOffset:178];

    

    //读取文件

    NSString *s1=[[NSString alloc]initWithData:[update readDataToEndOfFile] encoding:NSUTF8StringEncoding];

    NSLog(@"%@",s1);

    

    //写入文件

    [update writeData:[documents dataUsingEncoding:NSUTF8StringEncoding]];

    

    //跳到文件开始

    [update seekToFileOffset:0];

    

    //从文件读取100个字节

    NSString *s2=[[NSString alloc]initWithData:[update readDataOfLength:100] encoding:NSUTF8StringEncoding];

    NSLog(@"%@",s2);

    

    //打印当前读写指针位置

    NSLog(@"%llu",update.offsetInFile);

    

    //关闭文件

    [update closeFile];

转载于:https://www.cnblogs.com/a514875560/p/4033730.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值