ios获取文件的路径,创建文件
来源:互联网 发布:苹果电脑office软件 编辑:程序博客网 时间:2024/05/18 00:12
获取文件的路径,即获取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];
}
- ios获取文件的路径,创建文件
- iOS 关于Pch文件的创建及自动获取路径
- iOS获取文件的目录路径方法
- IOS获取文件路径的方法
- 获取iOS各种文件路径
- 获取iOS各种文件路径
- 获取iOS各种文件路径
- ios 获取文件路径方法
- 获取iOS各种文件路径
- IOS 各种文件路径获取
- 获取iOS各种文件路径
- iOS 获取项目文件路径
- 获取盘符的路径/文件/创建时间
- IOS中获取各种文件的目录路径的方法
- IOS中获取各种文件的目录路径的方法
- IOS中获取各种文件的目录路径的方法
- IOS中获取各种文件的目录路径的方法
- IOS中获取各种文件的目录路径的方法
- SWFUpload在Chrome、Firefox浏览器下session找不到的问题
- jsp中include html中文乱码问题
- 第一课:GPIO操作——LED & 按键
- pl/sql 不能对存储过程进行单步调试的问题解决方法
- JS设置页面按钮可用不可用
- ios获取文件的路径,创建文件
- struts2的输入校验框架及DTD文件配置
- Java 编程中关于异常处理的 10 个最佳实践
- tomcat配置连接池
- v$osstat 收集系统信息
- Android 中的AsyncTask的简单使用心得
- 解决html向aspx页面传值汉字乱码问题
- DG physical standby 搭建
- bakefile 缺少python.h的错误