获取DocumentsPath路径 简单文件写入

本文介绍了如何在iOS应用中使用Objective-C进行文件系统操作,包括访问不同类型的目录(如Home、Documents、Library等),以及如何对NSString、NSArray和NSDictionary进行写入和读取操作。

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

//访问沙盒路径
    
    
    //1.Home主目录,里面有 :Documents.Library.temp 和一个应用程序
    
   // NSLog(@"Home:%@",NSHomeDirectory());
  
    //2,Documents
    NSString *DocumentsPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSLog(@"Documents:%@",DocumentsPath);
    
    //3.Library
    NSString *LinraryPath=NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0];
    NSLog(@"Library:%@",LinraryPath);
    
    //4.temp

    NSLog(@"temp:%@",NSTemporaryDirectory());
    
    //5.caches
    NSString *cachesPath=NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    NSLog(@"caches:%@",cachesPath);
    
    
    //6.user
    NSString *userPath=NSUserName();
    NSLog(@"user:%@",userPath);
    
    //7.NSBoundle
    NSString *boundle=[[NSBundle mainBundle]pathForResource:@"89" ofType:@"png"];
    NSLog(@"bundle:%@",boundle);
   


NSString写入

    //简单文件写入,
    //NSString写入
    //1.写入的路径
    NSString *doucmentPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

    NSLog(@"%@",doucmentPath);
    //2.拼接文件路径
    NSString *filePath=[doucmentPath stringByAppendingString:@"/myText.txt"];
    
    //3.准备写入的内容
    NSString *content=@"Hello World.";
    //4.写入
    [content writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    //5.读取
    NSString *readString=[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"readString:%@",readString);


NSArray写入

 
    //NSArray写入
    //1.获取doucuments路径
    //2.拼接文件路径
    NSString *arrayFile=[doucmentPath stringByAppendingString:@"/array.plist"];
    //3.准备内容
    NSArray *array=@[@"123",@"456,",@"780"];
    //4.写入
    [array writeToFile:arrayFile atomically:YES];
    //读取
    NSArray *readArray=[NSArray arrayWithContentsOfFile:arrayFile];
    NSLog(@"readArray:%@",readArray);
    

NSDictionary写入

 
    //NSDictionary写入
    //1.获取doucuments路径
    //2.拼接文件路径
    NSString *dicFile=[doucmentPath stringByAppendingString:@"/dic.plist"];
    //3.准备内容
    NSDictionary *dic=[NSDictionary dictionaryWithObject:@"8888" forKey:@"a"];
    //4.写入
    [dic writeToFile:dicFile atomically:YES];
    //读取
    NSDictionary *readDic=[NSDictionary dictionaryWithContentsOfFile:dicFile];
    NSLog(@"readDic:%@",readDic);
    
    


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值