UI - SaveFile

<AppDelegate.m>

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    
    //沙盒 其实就是每个应用程序对应的一个本地文件,文件的名字是由系统随机产生的,保证所有应用程序沙盒名字不重复
    //沙盒机制 其实就是每一个应用程序资源起到了一个保护的作用,当前的应用程序不允许访问别的应用程序沙盒内的资源,同样也保证别的应用程序 访问不了当前应用程序沙盒中文件内的资源
    
    //对于每一个沙盒文件都包括以下几个文件:
    //1.documents : 用来存储数据持久化文件
    //2.library : 包含两个文件
        //(1) Caches : 存储缓冲文件,比如下载的视频,音频,图片,一般情况下,我们会分别创建 Videoes,Audios,Images 文件进行存储
        //(2) Prefrences : 存放用户偏好设置,它内部是一个 Plist 文件, virus 判断用户是否第一次启动程序
    //3.temp : 存放临时缓存文件,比如未下载完成的视频,一般对于下载完成的我们会手动将其移动到 Caches 文件夹下
    // XXX.app : 应用程序的包,应用程序的资源一般从包内获取,而且应用程序的包也是我们上传到 AppStore 上的文件.对于包内的文件我们一般不进行修改
    
    
    
    
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

<ViewController.h>

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end

<ViewController.m>

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
UI - SaveFile_第1张图片


<FirstViewController.h>

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController

@end

<FirstViewController.m>

#import "FirstViewController.h"

@interface FirstViewController ()
@property (retain, nonatomic) IBOutlet UITextField *tf1;
@property (retain, nonatomic) IBOutlet UITextField *tf2;

@end

@implementation FirstViewController
//存储
- (IBAction)saveBt:(id)sender {
    
    
//    //1.获取 document 文件路径
//    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
//    NSLog(@"%@",documentPath);
//    
//    //2.拼接文件路径
//    NSString *filePath = [documentPath stringByAppendingPathComponent:@"aa.txt"];
//    //3.存储
//    //若给定的文件名不存在,就创建一个文件,如果存在就会覆盖之前的内容
//    [self.tf1.text writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
   
//    
//    //1.获取 document 路径
//    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
//    //2.拼接文件路径
//    NSString *filePath = [documentPath stringByAppendingPathComponent:@"sb250.xml"];
//    //3.存储数据
//    NSArray *arr = @[self.tf1.text,self.tf2.text];
//    [arr writeToFile: filePath atomically:YES];

    
    //NSDictionary 存储
//    NSDictionary *dic = @{@"first":_tf1.text,@"second":_tf2.text};
//    //获取 document 文件路径
//   NSString *documentPath =  [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
//    //拼接文件路径
//    NSString *filePath = [documentPath stringByAppendingPathComponent:@"dic.plist"];
//    //存储数据
//    [dic writeToFile:filePath atomically:YES];
//
    
    //NSData 存储
    //1.获取 document 文件路径
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    //2.拼接路径
    NSString *filePath = [documentPath stringByAppendingPathComponent:@"data.txt"];
    //3.存储数据
    NSData *data = [_tf1.text dataUsingEncoding:NSUTF8StringEncoding];
    [data writeToFile:filePath atomically:YES];
    
    
}

//读取
- (IBAction)readBt:(id)sender {

//    //1.获取 document 文件路径
//    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
//    //2.拼接文件路径
//    NSString *filePath = [documentPath stringByAppendingPathComponent:@"aa.txt"];
//    //3.读取数据
//    NSString *str = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
//    _tf1.text = str;
//    
    
    //读取数组
    //1.获取 document
//    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
//    //2.拼接路径
//    NSString *filePath = [documentPath stringByAppendingPathComponent :@"sb250.xml"];
//    //3.读取数据
//    NSArray *array = [NSArray arrayWithContentsOfFile:filePath];
//    _tf2.text = array[0];
//    
    
    //读取字典
    
//    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
//    
//    NSString *filePath = [documentPath stringByAppendingPathComponent:@"dic.plist"];
//    
//    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
//    
//    _tf1.text = [dic valueForKey:@"first"];
//
    
    //读取 NSData
    
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *filePath = [documentPath stringByAppendingPathComponent:@"data.txt"];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    _tf1.text = [NSString stringWithFormat:@"%@",data];
    
    
    
}

//创建
- (IBAction)creat:(id)sender {
    //1.获取沙盒文件路径
    NSString *Caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)firstObject];
    NSLog(@"%@",Caches);
    
    //2.拼接文件路径
    NSString *filePath = [Caches stringByAppendingString:@"/Images-caches"];
    //3.创建文件管理对象
    NSFileManager *manager = [NSFileManager defaultManager];
    //4.根据路径创建文件
    [manager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
    
}
//复制
- (IBAction)copybt:(id)sender {
    //获取 document 路径
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    //拼接路径
    NSString *newString = [document stringByAppendingString:@"/iamges-document"];
    
    //创建 Images 文件路径
    //(1)获取 Caches 文件路径
    NSString *CachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    //(2)拼接Image 文件路径
    NSString *imagesPath = [CachesPath stringByAppendingString:@"/images-caches"];
    //创建文件管理对象
    NSFileManager *manager = [NSFileManager defaultManager];
    //复制
    [manager copyItemAtPath:imagesPath toPath:newString error:nil];
    
    
}
//移动
- (IBAction)move:(id)sender {
    //获取 Caches 路径
    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    //拼接路径
    NSString *newCP = [cachesPath stringByAppendingString:@"/images-caches"];
    
    //获取 temp 路径
    NSString *tempPath = NSTemporaryDirectory();
    //拼接路径
    NSString *newTP = [tempPath stringByAppendingString:@"/images-temp"];
    
    //创建文件管理对象
    NSFileManager *manager = [NSFileManager defaultManager];
    
    //移动
    [manager moveItemAtPath:newCP toPath:newTP error:nil];
    
}
//删除
- (IBAction)remove:(id)sender {
    //获取 temp 文件路径
    NSString *tempPath = NSTemporaryDirectory();
    //拼接文件路径
    NSString *filePath = [tempPath stringByAppendingString:@"/images-temp"];
    //移除
    [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
    
    
}
//存在
- (IBAction)exist:(id)sender {
    
    //获取 temp 文件路径
    NSString *tempPath = NSTemporaryDirectory();
    //拼接文件路径
    NSString *filePath = [tempPath stringByAppendingPathComponent:@"images-temp"];
    //判断是否存在
    BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
    
    if (isExist) {
        _tf1.text = @"存在";
    }else {
        _tf1.text = @"不存在";
    } 
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_tf1 release];
    [_tf2 release];
    [super dealloc];
}
@end

<SecondViewController.h>

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController
@property (retain, nonatomic) IBOutlet UITextField *name;
@property (retain, nonatomic) IBOutlet UITextField *sex;

@end

<SecondViewController.m>

#import "SecondViewController.h"
#import "Student.h"
@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//归档
- (IBAction)achiver:(id)sender {
    Student *stu = [[Student alloc] init];
    stu.name = self.name.text;
    stu.sex = self.sex.text;
    
    //归档操作
    
    //1.创建 NSMutableData
    NSMutableData *data = [NSMutableData data];
    //2.创建归档对象
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    //3.进行归档
    [archiver encodeObject:stu forKey:@"sanmei"];
    //4.结束归档
    [archiver finishEncoding];
    
    //获取 document 文件路径
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    //拼接文件路径
    NSString *newPath = [documentPath stringByAppendingString:@"/Student.txt"];
    //写入
    [data writeToFile:newPath atomically:YES];
    [stu release];
    [archiver release];
    
    
}

//反归档
- (IBAction)unarchiver:(id)sender {

    //获取文件路径
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    //拼接文件路径
    NSString *filePath = [documentPath stringByAppendingString:@"/Student.txt"];
    
    //反归档
    //1.获取数据
    NSMutableData *data = [NSMutableData dataWithContentsOfFile:filePath];
    
    //2.创建反归档对象
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
    //3.反归档操作
    Student *stu = [unarchiver decodeObjectForKey:@"sanmei"];
    //4.结束反归档
    [unarchiver finishDecoding];
    
    self.name.text = stu.name;
    self.sex.text = stu.sex;
    [unarchiver release];
    
}
- (void)dealloc {
    [_name  release];
    [_sex release];
    [super dealloc];
}
@end

<Student.h>

#import <Foundation/Foundation.h>

@interface Student : NSObject <NSCoding>
@property (nonatomic,copy)NSString *name;
@property (nonatomic,copy)NSString *sex;
@end

<Student.m>

#import "Student.h"

@implementation Student

-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if ([super init]) {
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.sex = [aDecoder decodeObjectForKey:@"sex"];
    }
    return self;
}
-(void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeObject:self.sex forKey:@"sex"];
    
}

@end

你可能感兴趣的:(本地化,文件操作,归档,SaveFile,反归档)