ios获取文件的路径,创建文件

获取文件的路径,即获取documents的路径

//获取文件路径
NSArray *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath=[path objectAtIndex:0];

NSLog(@"%@",documentsPath);

补充:

. NSSearchPathForDirectoriesInDomains和NSHomeDirectory
    iPhone和symbian 3rd一样,会为每一个应用程序生成一个私有目录,这个
目录位于/Users/sundfsun2009/Library/Application Support/iPhone         Simulator/User/Applications下,并随即生成一个数字字母串作为目录名,在每一次应用程序启动时,这个字母数字串都是不同于上一次。


    通常使用Documents目录进行数据持久化的保存,而这个Documents目录可以通过 NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserdomainMask,YES) 得到,代码如下:


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
   // NSString *path = [documentsDirectory stringByAppendingPathComponent:@"aa.plist"];
    NSLog(@"path:   %@",path);


    打印结果如下:
    path:   /Users/apple/Library/Application Support/iPhone Simulator/4.3/Applications/550AF26D-174B-42E6-881B-B7499FAA32B7/Documents


    而这个目录还可以通过 NSHomeDirectory()来得到,代码如下:

    NSString *destPath = NSHomeDirectory();
    NSLog(@"path:   %@",destPath);
    //destPath = [destPath stringByAppendingPathComponent: @"Documents"];
    //NSString *xmlpath = [destPath stringByAppendingPathComponent: @"menu/menu.xml"];


    打印结果如下:


    path:   /Users/apple/Library/Application Support/iPhone Simulator/4.2/Applications/6F4BC466-C5D6-440C-BAAC-BE20FA468C61

   看看两者打印出来的结果,我们可以看出这两种方法的不同。


    

在documents的路径下创建文件:

首先要获取documents的路径,如上所说。

其次就是下面的语句了:

NSString *writePath=[NSString stringWithFormat:@"%@/%@.txt",documentsPath,@"aaa"];
NSData *data = [@"" dataUsingEncoding:NSUTF8StringEncoding];//新文件的初始数据,设为空
[[NSFileManager defaultManager] createFileAtPath:writePath contents:data attributes:nil];//创建文件的命令在这里

这样就可以在documents文件夹下创建一个aaa.txt的文件了。哈哈哈哈哈。

或者是用下面的语句,

NSString *writePath=[NSString stringWithFormat:@"%@/%@.txt",documentsDirectory,@"bbb"];
NSError *error;
[@"fasdfasdasddaa" writeToFile:writePath atomically:YES encoding:NSUTF8StringEncoding error:&error];

这样就可以在documents文件夹下创建一个bbb.txt的文件了。并且,文件中的有"fasdfasdasddaa"字符。

总结起来就是,先给要存储的东西取一个名字,然后,找到它的路径。然后以这个名字创建。创建的时候可以添加内容。

当然,图片也可以批量的生成,假如说让你把一张图片复制500遍,并且给他按照(1-500)重命名。

你可以用上面的方法,用一个循环批量的生成500张图片。

 

复制代码
    UIImage *image = [UIImage imageNamed:@"240*360.png"];
NSData *data = UIImagePNGRepresentation(image);
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSMutableArray *muArray;
NSString *filePath = nil;
for (int j = 1; j<=500; j++) {
filePath = [[NSString alloc] initWithFormat:@"%@/%d.png",documentPath,j];
[data writeToFile:filePath atomically:YES];
NSString *newPath = [[NSString alloc] initWithFormat:@"%@",documentPath];
muArray = [[NSMutableArray alloc] initWithContentsOfFile:newPath];
[muArray addObject:filePath];
[filePath release];
复制代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值