//
// 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