UI文件操作

```
- (void)viewDidLoad {
    [super viewDidLoad];
#pragma mark - 沙盒
    //打印沙盒路径
    //代码是沙盒(sandbox)?
    //每一个iOS应用程序都会为自己创建一个文件目录系统(文件夹),这个独立、封闭、安全的空间,叫做沙盒
    //注:1.每一个应用程序都会拥有一个应用程序沙盒
    //2.应用程序沙盒就是一个文件目录系统
    //应用程序的沙盒目录下会有三个文件夹:1.Documents 2.library 3.tmp
    //document:保存应用运行时生成的需要持久化的数据,itunes会自动备份该目录

    //获取Library目录
//    NSArray *librarypath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
//    NSString *libraryPath = [librarypath objectAtIndex:0];
//    NSLog(@"%@",libraryPath);
//    NSLog(@"%@",NSHomeDirectory());
    //对文件进行操作 核心 就是文件路径
    //操作借助"文件管理者"
    //获取document路径
    NSString *docpath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSLog(@"%@",docpath);

#pragma mark -代码获取应用程序包目录与内容
//    NSString *imagePath = [NSBundle mainBundle].resourcePath;
//程序包文件(包含所有的资源文件和执行文件AppName.app)
//获取程序包中一个图片资源(apple.png)路径的方法
//    NSString *imagePath1 = [[NSBundle mainBundle] pathForResource:@"apple" ofType:@"png"];


    //创建文件路径
    NSString *novepath = [docpath stringByAppendingPathComponent:@"三国演义.txt"];
    //创建文件管理者 判断当前路径下有没有这个文件 如果有可以直接写入 如果没有就先创建再写入
    NSFileManager *manage = [NSFileManager defaultManager];
    if ([manage fileExistsAtPath:novepath]) {
        NSLog(@"文件存在");
    }else
    {
        [manage createFileAtPath:novepath contents:nil attributes:nil];
    }
    //向文件写入数据
    NSString *str = @"滚滚长江东逝水,浪花淘金英雄";
    BOOL result = [str writeToFile:novepath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    if (result) {
        NSLog(@"写入成功");
    }else
    {
        NSLog(@"写入失败");
    }
    //读文件
    NSString *newstr = [NSString stringWithContentsOfFile:novepath encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"%@",newstr);
    //找一张图片的网址 保存进document文件夹下 名为:美女.jpg
    UIImageView *imagev = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
    [self.view addSubview:imagev];
    //创建文件路径
    NSString *imagepath = [docpath stringByAppendingPathComponent:@"美女.jpg"];
    //转url ->转nsdata ->写入
    NSURL *url = [NSURL URLWithString:@"http://g.hiphotos.baidu.com/image/pic/item/241f95cad1c8a7866f726fe06309c93d71cf5087.jpg"];
    //转data
    NSData *data = [NSData dataWithContentsOfURL:url];
    //写入
    BOOL result1 = [data writeToFile:imagepath atomically:YES];
    if (result1) {
        NSLog(@"写入成功");
    }else
    {
        NSLog(@"写入失败");
    }

    //读出来显示到图片上
    NSData *redData = [NSData dataWithContentsOfFile:imagepath];
    imagev.image = [UIImage imageWithData:redData];

    //其他文件路径
    //缓存文件夹
    NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
    NSLog(@"%@",cachPath);
    //临时文件夹
    NSString *temPath = NSTemporaryDirectory();
    NSLog(@"%@",temPath);
    //文件的拷贝
    NSString *copypath = [temPath stringByAppendingPathComponent:@"三国演义复制版"];
   [manage copyItemAtPath:novepath toPath:copypath error:nil];
    NSLog(@"%@",copypath);
    //文件的移动
    //从tem 移动到document
    NSString *movePath = [docpath stringByAppendingPathComponent:@"三国演义复制版"];
    [manage moveItemAtPath:copypath toPath:movePath error:nil];
    NSLog(@"%@",movePath);
    //文件的删除
    //把doc里面的三国演义复制版给删除
    if ([manage removeItemAtPath:movePath error:nil]) {
        NSLog(@"删除成功");
    }
    NSLog(@"%@",movePath);


    //在document文件下创建一个"世界名著"文件夹 在这个文件夹下 创建雾都孤儿.txt文件 并向里面写入一段话
    //1.创建文件夹
    NSString *wordPath = [docpath stringByAppendingPathComponent:@"世界名著"];
   BOOL b1 = [manage createDirectoryAtPath:wordPath withIntermediateDirectories:YES attributes:nil error:nil];
    if (b1) {
        NSLog(@"创建文件夹成功");
    }else
    {
        NSLog(@"创建文件夹失败");
    }
    //2.创建文件
    NSString *bookPath = [wordPath stringByAppendingPathComponent:@"雾都孤儿.txt"];
    BOOL b2 = [manage createFileAtPath:bookPath contents:nil attributes:nil];
    if (b2) {
        NSLog(@"创建文件成功");
    }
    NSLog(@"%@",bookPath);
    //写入
    NSString *cotents1 = @"雾都孤儿是一本好小说";
    BOOL b3 = [cotents1 writeToFile:bookPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    if (b3) {
        NSLog(@"写入成功");
    }else
    {
        NSLog(@"写入失败");

    }
    //复制到缓存文件夹
    NSString *copyPath1 = [cachPath stringByAppendingPathComponent:@"雾都孤儿复制版.txt"];
    BOOL result7 = [manage copyItemAtPath:bookPath toPath:copyPath1 error:nil];
    if (result7) {
        NSLog(@"复制成功");
    }

#pragma mark -文件管理器与文件连接器之间的区别
    //文件管理器(NSFileManager)
    //此类主要是对文件进行的操作(创建/删除/改名等)以及文件信息的获取


    //文件连接器(NSFileHandle)
    //此类主要是对文件内容进行读取和写入操作
    //是非常基础的只针对文件内容的操作(写入,读取,更新)
    //使用场景:对文件内容进行局部的修改,追加内容;



#pragma mark -复杂对象的读写

    /*
     什么是复杂对象?
     在Foundation框架内不存在的数据类,如自定义person类,无法在程序内通过writeTofile:这个方法写入到文件夹内

     */

#pragma mark -归档和反归档
    /*
     如何将复杂对象写入文件?
     复杂对象无法通过writeTofile:方法进行数据持久化,只能通过将复杂对象转化为NSData(这个步骤就是归档)然后再通过writeTOfile:写入文件

    如何从文件中读取复杂对象
     从文件读取NSData数据,将NSData转化为复杂对象(这个步骤就是反归档)

     记住:
     1.复杂对象写入文件的过程(复杂对象->归档->NSData->writeTofile)
     2.从文件中读取复杂对象过程(读取文件->NSData->反归档->复杂对象)

   /如何进行归档和反归档?
    //(创建一个学生类,对学生类进行归档和反归档)
    //创建个学生
    Student *stu = [[Student alloc] init];
    stu.name = @"王尼玛";
    stu.sex = @"男";
    stu.address = @"东大街56号";

    //归档(写入)
    //保存的时候自定义 后缀名随便写
    NSString *stupath = [docpath stringByAppendingPathComponent:@"stu.sqlite"];
    //归档(写入)
    [NSKeyedArchiver archiveRootObject:stu toFile:stupath];
    //反归档(读出
    Student *stu1 = [NSKeyedUnarchiver unarchiveObjectWithFile:stupath];
    NSLog(@"%@",stu1.name);


     */





}

//继承nsobject ,student.h文件
--------------------------

#import <Foundation/Foundation.h>

@interface Student : NSObject<NSCoding>
//三条属性
@property(nonatomic,strong)NSString *name;
@property(nonatomic,strong)NSString *sex;
@property(nonatomic,strong)NSString *address;

@end

//student.m文件
-------------

#import "Student.h"

@implementation Student
//写(归档)
-(void)encodeWithCoder:(NSCoder *)aCoder
{
    //依次对属性进行写入操作
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeObject:self.sex forKey:@"sex"];
    [aCoder encodeObject:self.address forKey:@"address"];
}
//读取(反归档)
-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (self) {
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.sex = [aDecoder decodeObjectForKey:@"sex"];
        self.address = [aDecoder decodeObjectForKey:@"address"];
    }
    return self;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值