//

//  ViewController.m

//  沙盒操作

//

//  Created by mncong on 15/11/26.

//  Copyright © 2015年 mancong. All rights reserved.

//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

//到达沙盒根路径

[self getHomePath];

//获取Document路径

[self getDocumentsPath];

//获取Library目录路径

[self getLibraryPath];

//获取Library中的Cache路径

[self getCachePath];

//获取temp路径

[self getTempPath];

//NSString类路径的处理

[self dealWithPath];

//NSData处理

[self dealWithData];

//文件管理

[self dealWithNSaFileManager];

//获取文件中文件和文件列表

[self getDirectorys];

}

- (void)getHomePath

{

NSString * homePath = NSHomeDirectory();

NSLog(@"app_home: %@",homePath);

}

- (NSString *)getDocumentsPath

{

NSArray * pathsArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSLog(@"%@",pathsArr);

NSString * documentsPath = [pathsArr lastObject];

NSLog(@"app_documentsPath: %@",documentsPath);

return documentsPath;

}

- (void)getLibraryPath

{

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

NSString * documentsPath = [pathsArr lastObject];

NSLog(@"app_documentsPath: %@",documentsPath);

}

- (void)getCachePath

{

NSArray * pathsArr = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);

NSString * cachePath = [pathsArr lastObject];

NSLog(@"cachePath: %@",cachePath);

}

- (void)getTempPath

{

NSString * tempPath = NSTemporaryDirectory();

NSLog(@"app_tempPath: %@",tempPath);

}

- (void)dealWithPath

{

//操作的路径

NSString * path = @"/Users/apple/testfile.text";

//获取此路径的各个组成部分

NSArray * arr = [path pathComponents];

NSLog(@"%@",arr);

//提取此路径的最后一个组成部分

NSString * lastComponent = [path lastPathComponent];

NSLog(@"%@",lastComponent);

//删除路径的最后一个组成部分

NSString * deleteLastComponent = [path stringByDeletingLastPathComponent];

NSLog(@"%@",deleteLastComponent);

//将path添加到先邮路径的末尾

NSString * addPathToPath = [path stringByAppendingPathComponent:path];

NSLog(@"%@",addPathToPath);

//去除路径最后部分的扩展名

NSString * extension = [path pathExtension];

NSLog(@"%@",extension);

//删除路径最后部分的扩展名

NSString * deletePathExtension = [path stringByDeletingPathExtension];

NSLog(@"%@",deletePathExtension);

//路径最后部分追加扩展名(注意:方法已经拼接了一个点号了,不要再加了)

NSString * appendExtension = [path stringByAppendingPathExtension:@"jpg"];

NSLog(@"%@",appendExtension);

}

- (void)dealWithData

{

//1.NSString与NSData转换

NSString * string1 = @"1234abcd";

NSData * data1 = [string1 dataUsingEncoding:NSUTF8StringEncoding];

NSLog(@"data1: %@",data1);

NSString * string2 = [[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding];

NSLog(@"string2: %@",string2);

//2.NSData与UIImage

//拼接在本工程下的路径

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

NSString * name = [NSString stringWithFormat:@"1.jpg"];

NSString * finalPath = [path stringByAppendingPathComponent:name];

//把路径改为NSData类型

NSData * imageData = [NSData dataWithContentsOfFile:finalPath];

//获取图片

UIImage * image = [UIImage imageWithData:imageData];

//显示在UIimageView上

UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200)];

imageView.image = image;

[self.view addSubview:imageView];

}

- (void)dealWithNSaFileManager

{

NSError * error;

//创建文件管理

NSFileManager * fileManager = [NSFileManager defaultManager];

//指向文档目录

NSString * documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

NSLog(@"documentsDirectory: %@",documentsDirectory);

//创建一个目录

#warning format之后什么意思 以及后面的参数

[[NSFileManager defaultManager] createDirectoryAtPath:[NSString stringWithFormat:@"%@/myFolder",NSHomeDirectory()] withIntermediateDirectories:YES attributes:nil error:&error];

//1.创建一个文件

NSString * filePath = [documentsDirectory stringByAppendingPathComponent:@"file1.txt"];

NSLog(@"filePath: %@",filePath);

NSString * str = @"需要写入的字符串";

//写入文件

[str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];

//显示文件目录的内容

NSLog(@"documentsDirectory: %@",[fileManager contentsOfDirectoryAtPath:documentsDirectory error:&error]);

//2.对一个文件重命名

NSString * filePath2 = [documentsDirectory stringByAppendingPathComponent:@"file2.text"];

NSLog(@"%@",filePath2);

if ([fileManager moveItemAtPath:filePath toPath:filePath2 error:&error] != YES)

NSLog(@"Unable to mnove file: %@",error.localizedDescription);

NSLog(@"DocumentsDirectory: %@",[fileManager contentsOfDirectoryAtPath:documentsDirectory error:&error]);

//3.删除一个文件

if ([fileManager removeItemAtPath:filePath2 error:&error] != YES)

NSLog(@"Unable to delete file:%@",error.localizedDescription);

NSLog(@"Documentsdirectory: %@",[fileManager contentsOfDirectoryAtPath:documentsDirectory error:&error]);

}

- (void)getDirectorys

{

NSFileManager * fileManager = [NSFileManager defaultManager];

NSArray * documentsPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString * documentsDirectory = [documentsPaths objectAtIndex:0];

NSError * error = nil;

NSArray * fileList = [NSArray array];

fileList = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:&error];

NSLog(@"fileList: %@",fileList);

//列出给定一个文件夹里的所有子文件夹名

NSMutableArray * dirArray = [NSMutableArray array];

BOOL isDir = NO;

for (NSString * file in fileList) {

NSString * path = [documentsDirectory stringByAppendingPathComponent:file];

[fileManager fileExistsAtPath:path isDirectory:(&isDir)];

if (isDir) {

[dirArray addObject:file];

}

isDir = NO;

}

NSLog(@"Every Thing in the Dir: %@",fileList);

NSLog(@"All folders: %@",dirArray);

}

@end

iOS-沙盒路径总结、文件管理NSFileManager总结的更多相关文章

  1. ios沙盒路径

    http://www.cnblogs.com/ios-wmm/p/3299695.html iOS沙盒路径的查看和使用 NSString *path = NSHomeDirectory();//主目录 ...

  2. iOS 沙盒路径获取,创建文件

    沙盒下主要有四个文件夹:document,caches,tmp,library document 的路径 程序运行时生成的文件,这个文件不要存比较放大的文件,比如音频,视频类,因为这里的东西会被上传 ...

  3. iOS沙盒路径的查看和使用

    1.模拟器沙盒目录 文件都在个人用户名文件夹下的一个隐藏文件夹里,中文叫资源库,他的目录其实是Library. 因为应用是在沙箱(sandbox)中的,在文件读写权限上受到限制,只能在几个目录下读写文 ...

  4. iOS沙盒路径变化的说明详解

    最近用沙盒存储文件的时候发现了一个奇怪的现象,由于业务需要,我会将保存的文件绝对路径保存以便下次读取. 于是发现一个找不到的现象,即上一次保存下的绝对路径,再第二次打开app去查找的时候,发现找不到. ...

  5. iOS 沙盒路径操作:新建/删除文件和文件夹

    http://blog.csdn.net/totogo2010/article/details/7671144

  6. iOS创建、删除文件夹、获取沙盒路径

    1.获取沙盒路径 // 获取沙盒路径 NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent: ...

  7. iOS之沙盒机制和如何获取沙盒路径

    iOS APP可以在自己的沙盒里读写文件,但是,不可以访问其他APP的沙盒.每一个APP都是一个信息孤岛,相互是不可以进行通信的,唯独可以通过URL Scheme.沙盒里面的文件可以是照片.声音文件. ...

  8. iOS - 沙盒机制(SandBox)和获取沙盒路径

    iOSAPP可以在自己的沙盒里读写文件,但是,不可以访问其他APP的沙盒.每一个APP都是一个信息孤岛,相互是不可以进行通信的,唯独可以通过URLScheme.沙盒里面的文件可以是照片.声音文件.文本 ...

  9. iOS沙盒(sandbox)机制及获取沙盒路径

    一.每个iOS应用SDK都被限制在沙盒中,沙盒相当于一个加了仅主人可见权限的文件夹,苹果对沙盒有以下几条限制. (1).应用程序可以在自己的沙盒里运作,但是不能访问任何其他应用程序的沙盒. (2).应 ...

  10. iOS沙盒(sandbox)机制及获取沙盒路径

    一. 每个iOS应用SDK都被限制在“沙盒”中,“沙盒”相当于一个加了仅主人可见权限的文件夹,苹果对沙盒有以下几条限制. (1)应用程序可以在自己的沙盒里运作,但是不能访问任何其他应用程序的沙盒. ( ...

随机推荐

  1. ASP.NET Web API的安全管道

    本篇体验ASP.NET Web API的安全管道.这里的安全管道是指在请求和响应过程中所经历的各个组件或进程,比如有IIS,HttpModule,OWIN,WebAPI,等等.在这个管道中大致分两个阶 ...

  2. java.lang.ClassNotFoundException: org.junit.Assume$AssumptionViolatedException

    java.lang.ClassNotFoundException: org.junit.Assume$AssumptionViolatedException 在spring框架中进行单元测试,出现标题 ...

  3. 使用Installutil安装系统服务方法

    系统必须装有.net Framework2.0然后点击开始-运行输入以下指令即可完成相应操作安装服务:C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/Ins ...

  4. Linux下tomcat部署

    进入Tomcat下的bin目录 cd /usr/local/tomcat/bin 如果你想直接干掉Tomcat,你可以使用kill命令,直接杀死Tomcat进程 kill -9 7010 然后继续查看 ...

  5. Scala 深入浅出实战经典 第77讲:模式匹配下的提取器动手构造实战

    王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...

  6. Codeforces Round #379 (Div. 2) E. Anton and Tree 缩点 直径

    E. Anton and Tree 题目连接: http://codeforces.com/contest/734/problem/E Description Anton is growing a t ...

  7. 错误名称:EntityCommandExecutionException

    错误名称:EntityCommandExecutionException 错误时间:2015/9/22 11:13:34 错误消息:执行命令定义时出错.有关详细信息,请参阅内部异常. 堆栈信息: 在 ...

  8. linux 系统性能指标采样脚本

    以下脚本写于redmine性能排查时,用于定位系统性能瓶颈的采样,源地址为~/performanceLog/collectLog.sh中,计划放入github的代码片段库中. 注: 如果mysql的地 ...

  9. HTTP - GET和POST的区别

    网上有很多文章介绍这两种HTTP请求的区别,我也不懂,主要还是看了一些文章,在这里写下一些笔记. 语义不同 在HTTP协议中,最初规定GET是用来查询或者获取资料,只读,POST用于修改数据,可写.因 ...

  10. EF Repository Update

    问题描述: 解决办法: http://www.cnblogs.com/scy251147/p/3688844.html 原理: Attaching an entity of type '' faile ...