16.文件管理二

1.工程一文件

AppDelegate.m文件

#import "AppDelegate.h"
@implementation AppDelegate

- (void)dealloc
{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

/***1.沙盒的获取**/
/*******第一种获取沙盒目录的方式********/
//获取沙盒的根路径
NSString *homePath = NSHomeDirectory();
NSLog(@"%@",homePath);//--->Users/dlios/Library/Developer/CoreSimulator/Devices/090A37F4-1475-48F2-A155-8A3D764E055C/data/Containers/Data/Application/5A0266F2-02E5-4422-B4F4-E6D9790A282E

/*******第二种获取沙盒目录的方式********/
//获取沙盒下的Documents 目录
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *p = [paths lastObject];
NSLog(@"%@",p);//--->Users/dlios/Library/Developer/CoreSimulator/Devices/090A37F4-1475-48F2-A155-8A3D764E055C/data/Containers/Data/Application/27925127-6166-43DC-B563-C71EADCBABCF/Documents

//获取Library目录
paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
p = [paths lastObject];
NSLog(@"%@",p);//--->/Users/dlios/Library/Developer/CoreSimulator/Devices/090A37F4-1475-48F2-A155-8A3D764E055C/data/Containers/Data/Application/2C9D8E4D-ADB7-412B-A5C9-C21D03420527/Library

//获取沙盒下的temp目录
NSString *tempPath = NSTemporaryDirectory();
NSLog(@"%@",tempPath);//--->/Users/dlios/Library/Developer/CoreSimulator/Devices/090A37F4-1475-48F2-A155-8A3D764E055C/data/Containers/Data/Application/2C9D8E4D-ADB7-412B-A5C9-C21D03420527/tmp/

/****2、NSString类路径处理方法****/
NSString *path = @"/Users/apple/testfile.text";
NSArray *pathComps = [path pathComponents];
NSLog(@"%@",pathComps);//--->("/",Users,apple,"testfile.text")

NSString *homePath = NSHomeDirectory();
NSString *documents = [homePath stringByAppendingPathComponent:@"Documents"];
NSLog(@"%@",documents);//--->/Users/dlios/Library/Developer/CoreSimulator/Devices/090A37F4-1475-48F2-A155-8A3D764E055C/data/Containers/Data/Application/CAAB3D53-F5A3-4894-B34C-08D4BEDB2926/Documents

NSString *docu = [homePath stringByAppendingString:@"/Documents"];
NSLog(@"----%@",docu);//--->/Users/dlios/Library/Developer/CoreSimulator/Devices/090A37F4-1475-48F2-A155-8A3D764E055C/data/Containers/Data/Application/CAAB3D53-F5A3-4894-B34C-08D4BEDB2926/Documents

/*****3、NSdata的基本概念*****/
//data和string 之间的转换
NSString *s = @"我是字符串";
//string ---> data
NSData *data = [s dataUsingEncoding:NSUTF8StringEncoding];
//data ---> string
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",str);//--->我是字符串

//文件管理演示
[self fileManagerTest];
return YES;   
}
- (void)fileManagerTest {
    NSString *text = @"爱是恒久,忍耐有恩慈";
    NSString *homePath = NSHomeDirectory();
    NSString *filePath = [homePath stringByAppendingPathComponent:@"files.text"];
    NSData *textData = [text dataUsingEncoding:NSUTF8StringEncoding];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    //创建文件
    BOOL success = [fileManager createFileAtPath:filePath contents:textData attributes:nil];
    if (success) {
        NSLog(@"创建文件成功!");
    }
    //创建目录
    NSString *directPath = [homePath stringByAppendingPathComponent:@"test"];
    success = [fileManager createDirectoryAtPath:directPath withIntermediateDirectories:YES attributes:nil error:nil];
    if(success) {
        NSLog(@"创建目录成功!");
    }

    NSData *data = [fileManager contentsAtPath:filePath];
    NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@",s);
    //移动文件
    NSString *directPath = [homePath stringByAppendingPathComponent:@"test/file2.text"];
    BOOL success = [fileManager moveItemAtPath:filePath toPath:directPath error:nil];
    if (success) {
        NSLog(@"移动成功");
    }
    //复制文件
    if([fileManager copyItemAtPath:directPath toPath:filePath error:nil]) {
        NSLog(@"复制成功");
    }
    //删除文件
    if([fileManager fileExistsAtPath:filePath]) {
        [fileManager removeItemAtPath:filePath error:nil];
    }
    //获取文件的大小
    NSString *directPath = [homePath stringByAppendingPathComponent:@"test/file2.text"];
    NSDictionary *attrDic = [fileManager attributesOfItemAtPath:directPath error:nil];
   //NSNumber *fileSize = [attrDic objectForKey:NSFileSize];
    NSNumber *fileSize = attrDic[NSFileSize];
    NSLog(@"%@",fileSize);
    NSLog(@"%@",attrDic);
    //计算一个目录的容量大小
    NSString *directPath = [homePath stringByAppendingPathComponent:@"test"];
    NSDirectoryEnumerator *files = [fileManager enumeratorAtPath:directPath];

    NSString *path = [files nextObject];
    NSInteger fileNum = 0;
    while (path != nil) {
        NSLog(@"%@",path);
        NSString *path2 = [directPath stringByAppendingPathComponent:path];
        NSDictionary *attrDic = [fileManager attributesOfItemAtPath:path2 error:nil];
        NSNumber *fileSize = attrDic[NSFileSize];
        fileNum += [fileSize intValue];
        path = [files nextObject];
    }
    NSLog(@"目录的总大小:%ld",fileNum);
}
@end

2.第二工程文件

main.m文件

/**
 *数组、字典只能将Bool、NSNumber、NSString、NSData、NSDate、NSArray、NSDictionary 写入属性列表plist文件
 */
//NSValue *value = [NSValue valueWithRange:NSMakeRange(1, 5)];该对象不可以写入plist文件
NSDate *date = [NSDate date];
NSDictionary *dic = @{@"key1":@12345,@"key2":@"tttxxx",@"key3":date};
NSString *homePath = NSHomeDirectory();
NSString *path = [homePath stringByAppendingPathComponent:@"test.plist"];
BOOL success = [dic writeToFile:path atomically:YES];
if (success) {
    NSLog(@"write success");
}

NSString *homePath = NSHomeDirectory();
NSString *path = [homePath stringByAppendingPathComponent:@"test.plist"];
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];
NSLog(@"%@",dic);//--->{key1 = 12345;key2 = tttxxx;key3 = "2015-08-02 08:42:54 +0000";}

3.工程三文件

要求:在根目录下创建一个文件”mobile.text”, 内容为”iPhone, android, windows phone”, 然后再次读取内容, 并将内容改为”iPhone,windows phone, android”,并且将文件名称改为”cellPhon.text”. 然后再根目录下创建一个文件夹名为”phone”, 再将文件剪切到该目录下
main.m文件

NSString *str = @"iPhone、android、windows phone";
//字符串转NSData
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"mobile.text"];

NSFileManager *fileManager = [NSFileManager defaultManager];
//创建mobile.text文件、并且将数据写入
BOOL success = [fileManager createFileAtPath:filePath contents:data attributes:nil];
if (success) {
    NSLog(@"file create success");
}
//读取mobile.text文件的数据
NSData *fileData = [fileManager contentsAtPath:filePath];
NSString *fileString = [[NSString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];
//使用"、" 对字符串进行分割
NSArray *strArray = [fileString componentsSeparatedByString:@"、"];
NSMutableArray *mutableArray = [NSMutableArray arrayWithArray:strArray];
//调换数组上元素的位置
[mutableArray exchangeObjectAtIndex:1 withObjectAtIndex:2];
//使用"、"将数组上的字符串元素拼接成一个字符串
NSString *newstring = [mutableArray componentsJoinedByString:@"、"];

NSString *newFilePath = [NSHomeDirectory() stringByAppendingPathComponent:@"cellPhone.text"];
NSData *newData = [newstring dataUsingEncoding:NSUTF8StringEncoding];
success = [fileManager createFileAtPath:filePath contents:newData attributes:nil];
if (success) {
    NSLog(@"覆盖源文件");
}
//通过移动文件重命名文件名
success = [fileManager moveItemAtPath:filePath toPath:newFilePath error:nil];
if (success) {
    NSLog(@"重名");
}

NSString *director = [NSHomeDirectory() stringByAppendingPathComponent:@"phone"];
NSError *error;
//创建phone目录
success = [fileManager createDirectoryAtPath:director withIntermediateDirectories:YES attributes:nil error:&error];
if (!success) {
    NSLog(@"%@",error);
} else {
    NSLog(@"创建目录成功");
}

NSString *tagetPath = [director stringByAppendingPathComponent:[newFilePath lastPathComponent]];
//剪切文件
success = [fileManager moveItemAtPath:newFilePath toPath:tagetPath error:nil];
if (success) {
    NSLog(@"over");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值