文件管理类

输出结果

这里写图片描述

//
//  ViewController.m
//  文件-复习
//
//  Created by Dream or want some on 2017/4/17.
//  Copyright © 2017年 刘桂根. All rights reserved.
//

#import "ViewController.h"
//宏定义桌面路径
#define PATH @"/Users/user/Desktop"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //文件管理类
    NSFileManager* fm = [NSFileManager defaultManager];
    //创建文件
    [fm createFileAtPath:[NSString stringWithFormat:@"%@/%@", PATH, @"1.txt"] contents:nil attributes:nil];
    //创建文件夹
    [fm createDirectoryAtPath:[NSString stringWithFormat:@"%@/%@", PATH, @"1"] withIntermediateDirectories:YES attributes:nil error:nil];
    //判断文件是否存在
    BOOL have = [fm fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", PATH, @"1.txt"]];
    NSLog(@"判断文件是否存在 == %d", have);
    //判断文件夹是否存在
    BOOL isDir = YES;
    BOOL have1 = [fm fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", PATH, @"1"] isDirectory:&isDir];
    NSLog(@"判断文件夹是否存在 == %d", have1);

    //写入内容
    NSString* str = @"此内容于(2017/04/17)写入 写入人:天才刘";
    NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
    [data writeToFile:[NSString stringWithFormat:@"%@/%@", PATH, @"1.txt"] atomically:YES];
    //复制文件
    [fm copyItemAtPath:[NSString stringWithFormat:@"%@/%@", PATH, @"1.txt"] toPath:[NSString stringWithFormat:@"/Users/user/Downloads/%@",@"2.txt"] error:nil];
    //移动文件
    [fm moveItemAtPath:[NSString stringWithFormat:@"%@/%@", PATH, @"1.txt"] toPath:[NSString stringWithFormat:@"/Users/user/Downloads/%@",@"1.txt"] error:nil];
    //读文件内容
    NSData* data1 = [NSData dataWithContentsOfFile:[NSString stringWithFormat:@"/Users/user/Downloads/%@",@"1.txt"]];
    NSString* str1 = [[NSString alloc]initWithData:data1 encoding:NSUTF8StringEncoding];
    NSLog(@"文件内容 == %@", str1);
    //移除文件
    [fm removeItemAtPath:[NSString stringWithFormat:@"/Users/user/Downloads/%@",@"1.txt"] error:nil];

    //深度遍历
    NSLog(@"深度遍历 == %@", [fm subpathsAtPath:PATH]);
    //浅度遍历
    NSLog(@"浅度遍历 == %@", [fm contentsOfDirectoryAtPath:PATH error:nil]);

    //以只读的方式创建文件操作类
    NSFileHandle* fh = [NSFileHandle fileHandleForReadingAtPath:[NSString stringWithFormat:@"/Users/user/Downloads/%@",@"2.txt"]];
    //读取6个长度的文本内容(中文每个字的长度为3)
    NSData* data3 = [fh readDataOfLength:6];
    NSString* str2 = [[NSString alloc]initWithData:data3 encoding:NSUTF8StringEncoding];
    NSLog(@"读取6个长度的文本内容为 == %@", str2);
    //调整文件指针到某个位置
    [fh seekToFileOffset:6];
    //把文件指针调整到末尾,默认是在开始的位置
    [fh seekToEndOfFile];


    //以只写的方式打开文件
    NSFileHandle* fh1 = [NSFileHandle fileHandleForWritingAtPath:[NSString stringWithFormat:@"/Users/user/Downloads/%@",@"2.txt"]];
    [fh1 seekToEndOfFile];
    //写入内容,写入时默认是从开始的位置写入,出现的效果就是从开头替换之前的内容展示目前写人的内容,要想从末尾写入不覆盖之前的,必须调整文件指针到末尾
    [fh1 writeData:data3];

}


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


@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值