zoukankan      html  css  js  c++  java
  • OC基础 文件管理

    OC基础  文件管理

    1.文件管理类NSFileManager对象的创建:

      NSFileManager *fm = [NSFileManager defaultManager];

    2.文件操作:

    (1)遍历查看目录下的文件:

      a.遍历查看目录下的文件:contentsOfDirectorAtPath:(NSString *)path error:(NSError **)error;

      b.深度遍历,子目录也遍历:subPathsOfDirectorAtPath:(NSString *)path error:(NSError **)error;

    (2)创建文件:createFileAtPath:(NSString *)path contents:(NSData *)data attributes:(NSDictionary*)attar;

    (3)创建目录:createDirectoryAtPath:(NSString *)path withIntermediateDirectories:

      attributes:(NSDictionary *)attributes error:(NSError **)error

    (4)拷贝文件/目录:copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dsPath error:  (NSError **)error;

    (5)移动文件/目录:moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dsPath error:  (NSError **)error;

    (6)删除文件/目录:removeItemAtPath:(NSString *)path error:(NSError **)error;

    (7)获取文件属性:attributesOfItemAtPath:(NSString *)path error:(NSError **)error;

    (8)判断文件是否存在:fileExistsAtPath:(NSString *)path;

    测试:

    #import <Foundation/Foundation.h>
    
    #define PATH       @"/Users/hwt/Desktop/test"
    #define FILEPATH   @"/Users/hwt/Desktop/test/1.txt"
    #define NEWPATH    @"/Users/hwt/Desktop/test/test1"
    #define TOPATH     @"/Users/hwt/Desktop/test/test2"
    #define MOVEPATH   @"/Users/hwt/Desktop/test/test2/2.txt"
    #define FILEPATH2  @"/Users/hwt/Desktop/test/2.txt"
    
    int main(int argc, const char * argv[])
    {
    
        @autoreleasepool {
            
            //文件管理类对象的创建
            NSFileManager *fm = [NSFileManager defaultManager];
            NSError *error;
            //文件遍历
            //浅度遍历(只遍历当前路径下的文件)
            NSArray *array = [fm contentsOfDirectoryAtPath:PATH error:&error];
            NSLog(@"array = %@",array);
            
            //深度遍历(遍历当前文件夹以及自文件夹里面的所有内容)
            NSArray *array1 = [fm subpathsOfDirectoryAtPath:PATH error:&error];
            NSLog(@"array1 = %@",array1);
            
            NSString *str = @"你好";
            NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
            //创建文件,并写入数据
            BOOL result = [fm createFileAtPath:FILEPATH contents:data attributes:nil];
            if (result) {
                NSLog(@"文件创建成功");
            }
            else {
                NSLog(@"文件创建失败");
            }
            
            //创建目录
            result = [fm createDirectoryAtPath:NEWPATH withIntermediateDirectories:YES attributes:nil error:&error];
            if (result) {
                NSLog(@"文件夹创建成功");
            }
            else {
                NSLog(@"文件夹创建失败");
            }
            
            //文件/目录拷贝
            result = [fm copyItemAtPath:NEWPATH toPath:TOPATH error:&error];
            if (result) {
                NSLog(@"文件拷贝成功");
            }
            else {
                NSLog(@"文件拷贝失败");
            }
            
            //文件/目录移动
            result = [fm moveItemAtPath:FILEPATH toPath:MOVEPATH error:&error];
            if (result) {
                NSLog(@"文件移动成功");
            }
            else {
                NSLog(@"文件移动失败");
            }
            
            //文件或目录删除
            result = [fm removeItemAtPath:TOPATH error:&error];
            if (result) {
                NSLog(@"文件删除成功");
            }
            else {
                NSLog(@"文件删除失败");
            }
            
            //获取文件属性
            NSDictionary *dic = [fm attributesOfItemAtPath:FILEPATH2 error:&error];
            NSLog(@"文件属性dic = %@",dic);
            
            //获取文件的大小
            unsigned long long filesize = [dic fileSize];
            NSLog(@"文件的大小filesize = %llu",filesize);
            
            //文件是否存在
            result = [fm fileExistsAtPath:FILEPATH2];
            if (result) {
                NSLog(@"文件存在");
            }
            else {
                NSLog(@"文件不存在");
            }
            
        }
        return 0;
    }

    测试结果:

    补充:

    文件句柄类NSFileHandle

    我们知道,对文件的读写都需要用NSFileHandle打开文件,并且读取和写入的数据都是NSData类型的二进制数据,下面让我们来看一下。

    1.打开文件的方法有三种:

    (1)只读方式打开:fileHandleForReadingAtPath:(NSString *)path;

    (2)只写方式打开:fileHandleForWritingAtPath:(NSString *)path;

    (3)读写方式打开:fileHandleForUpdatingAtPath:(NSString *)path;

    2.读指定长度的数据:readDataOfLength:(NSUinteger)length;

    3.从当前偏移量读到文件末尾:readDataToEndOfFile;

    4.设置文件的偏移量:seekToFileOffset:(unsigned long long)offset;

    5.将文件偏移量定位到文件末尾:seekToEndOfFile;

    6.将文件长度设置为offset:truncateFileAtOffset:(unsigned long long)offset;

    7.追加写入文件:writeData:(NSData *)data;

    8.将缓存区的内容立即同步到磁盘:synchronizeFile;

    #import <Foundation/Foundation.h>
    
    #define PATH @"/Users/hwt/Desktop/test/2.txt"
    
    int main(int argc, const char * argv[])
    {
    
        @autoreleasepool {
            
            //以只读的方式打开文件
            NSFileHandle *fh = [NSFileHandle fileHandleForReadingAtPath:PATH];
            //以只写的方式打开文件
            NSFileHandle *fh1 = [NSFileHandle fileHandleForWritingAtPath:PATH];
            //以可读写的方式打开文件
            NSFileHandle *fh2 = [NSFileHandle fileHandleForUpdatingAtPath:PATH];
            
            //读取数据
            //默认情况,光标位置是0
            NSData *data =[fh2 readDataToEndOfFile];
            //光标位置丁文
            [fh2 seekToFileOffset:0];
            //从光标位置开始,读取指定长度的内容
            data = [fh2 readDataOfLength:1000];
            NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"string = %@",string);
            
            
            NSString *str1 = @"你好,hello world";
            NSData *data1= [str1 dataUsingEncoding:NSUTF8StringEncoding];
            //将数据内容追加到缓存里
            [fh2 writeData:data1];
            //将缓存区的内容立即同步到文件中
            [fh2 synchronizeFile];
            
        }
        return 0;
    }
  • 相关阅读:
    以太坊测试网络搭建以及RPC服务开启-配置注意事项
    AD预测论文研读系列1
    DenseNet 论文阅读笔记
    Deep learning with Python 学习笔记(7)
    ADNI数据
    利用卷积神经网络进行阿尔茨海默病分类的神经影像模式融合 论文研读笔记
    阿尔茨海默病早期诊断的脑结构分级图 论文研读笔记
    GoogLeNetv4 论文研读笔记
    ResNet 论文研读笔记
    GoogLeNetv3 论文研读笔记
  • 原文地址:https://www.cnblogs.com/DWdan/p/5096461.html
Copyright © 2011-2022 走看看