iOS-文件夹操作-文件管理器(创建/删除/改名)-NSFileManager

2016-03-02 17:41:13


//
// ViewController.m
// iOS-文件夹操作-文件管理器(创建/删除/改名)-NSFileManager
//
// Created by YIem on 16/3/2.
// Copyright © 2016年 YIem. All rights reserved.
//

import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

  • (void)viewDidLoad {

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor greenColor];
    NSLog(@"%@", NSHomeDirectory());
      NSString *fileStr =  [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *stuPath = [fileStr stringByAppendingPathComponent:@"stu.txt"];
    NSString *str = @"111111";
    [str writeToFile:stuPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    
    
    
    // 文件操作部分
    NSString *cachStr = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
    
    NSString *cachPath = [cachStr stringByAppendingPathComponent:@"ace.txt"];
    
    // 创建文件管理器对象
    NSFileManager *fileManager = [NSFileManager defaultManager];
    // 将stu.txt 移动到cach文件夹下并且改名为ace.text
    [fileManager moveItemAtPath:stuPath toPath:cachPath error:nil];

    // 判断文件是否存在

    BOOL ret = [fileManager fileExistsAtPath:cachPath];
    NSLog(@"是否存在: %d", ret);
    // 拷贝文件

    // [fileManager copyItemAtPath:stuPath toPath:cachPath error:nil];

    // 删除文件

    // [fileManager removeItemAtPath:stuPath error:nil];

    }

  • (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

    }

@end

当前页面是本站的「Baidu MIP」版。发表评论请点击:完整版 »