文件夹管理器

1、文件管理器(NSFileManager)

 1> 创建文件夹

  创建所需的方法在头文件的声明:

/* createDirectoryAtPath:withIntermediateDirectories:attributes:error: creates a directory at the specified path. If you pass ‘NO‘ for createIntermediates, the directory must not exist at the time this call is made. Passing ‘YES‘ for ‘createIntermediates‘ will create any necessary intermediate directories. This method returns YES if all directories specified in ‘path‘ were created and attributes were set. Directories are created with attributes specified by the dictionary passed to ‘attributes‘. If no dictionary is supplied, directories are created according to the umask of the process. This method returns NO if a failure occurs at any stage of the operation. If an error parameter was provided, a presentable NSError will be returned by reference.

    This method replaces createDirectoryAtPath:attributes:
 */ // 参数1:创建的文件夹的路径// 参数2:是否创建媒介的布尔值,一般为YES// 参数3: 属性,没有就置为nil// 参数4: 错误信息
- (BOOL)createDirectoryAtPath:(NSString *)path withIntermediateDirectories:(BOOL)createIntermediates attributes:(nullable NSDictionary<NSString *, id> *)attributes error:(NSError **)error NS_AVAILABLE(10_5, 2_0);

  实例代码:

    // 创建对象
    NSFileManager *manager = [NSFileManager defaultManager];
    // 创建路径
    NSString *path = NSHomeDirectory();

    path = [path stringByAppendingPathComponent:@"test/myApp"];

    NSLog(@"%@", path);

    NSError *error = nil;

    // 创建文件夹
    BOOL success = [manager createDirectoryAtPath:path
                      withIntermediateDirectories:YES
                                       attributes:nil
                                            error:&error];
    NSLog(@"success = %d,error = %@", success,error);

 2> 向文件夹中添加文件

  内容写入方法在头文件的声明:

// 参数1:要写入内容的文件的文件路径
// 参数2:一个BOOL值,一般为YES
// 参数3: 编码方式,一般为UTF8
// 参数4:错误信息
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError **)error;

  实例代码:

    //向文件夹中添加字符串
    path = [path stringByAppendingPathComponent:@"zifucuan.txt"];

    //初始化一个字符串
    NSString *string = @"hello";

    BOOL success1 = [string writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];

    if (success1) {

        NSLog(@"成功:%@",path);
    }else{
        NSLog(@"失败");
    }

 3> 删除文件夹中文件

  删除文件方法在头文件的声明:

// 参数1:路径
// 参数2:错误信息
- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error NS_AVAILABLE(10_5, 2_0);

  实例代码:

    // 删除path目录下的所有文件
    [manager removeItemAtPath:path error:nil];

 4> 文件移动

  文件移动方法在头文件的声明:

// 参数1:要移动的文件路径
// 参数2:要移动到的文件路径(目的地)
// 参数3:错误信息
- (BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error NS_AVAILABLE(10_5, 2_0);

  实例代码:

    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    // 创建一个文件夹
    NSString *copyPath = [documentPath stringByAppendingPathComponent:@"备份/test.txt"];

    // stringByDeletingLastPathComponent 删除最后一个路径
    [manager createDirectoryAtPath:[copyPath stringByDeletingLastPathComponent]
       withIntermediateDirectories:YES
                        attributes:nil
                             error:nil];
    // 定义一个字符串
    NSString *testStr = @"Hello World";

    NSData *data = [testStr dataUsingEncoding:NSUTF8StringEncoding];

    // 将内容写入文件
    [manager createFileAtPath:copyPath
                     contents:data
                   attributes:nil];

    // 创建一个toPath
    NSString *toPath = [documentPath stringByAppendingPathComponent:@"hello/copyTest.txt"];

    // 创建一个移动到的文件夹及文件
    [manager createDirectoryAtPath:[toPath stringByDeletingLastPathComponent]
       withIntermediateDirectories:YES
                        attributes:nil
                             error:nil];

    BOOL result = [manager moveItemAtPath:copyPath
                                   toPath:toPath
                                    error:nil];
    NSLog(@"result = %d", result);

 5> 文件copy(拷贝)

  文件copy(拷贝)方法在头文件的声明:

// 参数1:要拷贝的文件路径 // 参数2:要拷贝到的文件路径(目的地) // 参数3:错误信息- (BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error NS_AVAILABLE(10_5, 2_0);

  实例代码:

// 路径使用上面的路径
[manager copyItemAtPath:copyPath
                     toPath:toPath
                      error:nil];

2、文件夹处理器(NSFileHandle)

 1> 使用NSFileHandle向文件夹追加内容

  • 通过fileHandle更新
// 参数为文件路径
+ (nullable instancetype)fileHandleForUpdatingAtPath:(NSString *)path;
  • 搜索到文本内容末尾方法
// 搜索到文件内容的末尾
- (unsigned long long)seekToEndOfFile;
  • 实例代码:(使用上面的路径)

    // 创建handle对象
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:path];

    // 搜索到文本内容末尾
    [fileHandle seekToEndOfFile];

    NSString *appendStr = @"我是后来的";

    NSData *appendData = [appendStr dataUsingEncoding:NSUTF8StringEncoding];

    // 将数据写入到对接起
    [fileHandle writeData:appendData];

    // 关闭对接起
    [fileHandle closeFile];

 2> 定位数据

  • 通过fileHandle读取
// 参数为文件路径+ (nullable instancetype)fileHandleForReadingAtPath:(NSString *)path;
  • 获取文件中可获得的数据(所有数据)
@property (readonly, copy) NSData *availableData;
  • 设置文件的偏移量
// 参数为一个和文件长度有关的数值
- (void)seekToFileOffset:(unsigned long long)offset;
  • 从文件的偏移量位置读取到最后
- (NSData *)readDataToEndOfFile;

实例代码:

    // 将“123456”写入file2.txt文件夹中
    NSString * content = @"123456";
    NSString * filePath2 = [documentPath stringByAppendingPathComponent:@"file2.txt"];
    [fileManager createFileAtPath:filePath2 contents:[content dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];

    // 通过fileHandle读取
    fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath2];
    // 获取数据长度
    NSUInteger length = [[fileHandle availableData] length];
    // 设置文件的偏移量为文件的一半
    [fileHandle seekToFileOffset:length/2.0];
    // 从文件的偏移量位置读取到最后
    NSData * data = [fileHandle readDataToEndOfFile];
    [fileHandle closeFile];
    // 打印读取的字符串
    NSString * string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@",string);

时间: 2024-07-30 13:50:13

文件夹管理器的相关文章

仿手机文件夹管理器

仿手机文件夹管理器,可以打开显示SD卡下是文件,可以进入下一级和返回上一级. 效果图: 关键代码入下: import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.ut

Android 编程之入门开发文件夹管理器开发详细讲解-1

在我们的手持设备中,一般都会自带设备公司自己开发的文件管理系统.拍照系统之类的东东,今天我给大伙说说入门级开发的文件夹管理器,代码贼少 总共就6个类吧,没有夹杂其他外部应用,就是一个纯文件夹管理器 APP主要功能设计:文件复制.文件夹复制.批量文件/文件夹复制.删除文件.删除文件夹.批量删除文件/文件夹.文件分类.文件搜索暂时没有写, 有兴趣的可以自己写写 APP主要应用:DrawerLayout .ListView.Fragment.IO.序列化.文件过滤.文件排序.ActionBar.Ada

Android 编程之入门开发文件夹管理器开发文件的过滤与排序-3

前面说了文件操作和主界面,接下来说说文件的过滤和排序,我们都知道在我们的设备里,不管是PC还是手机,总有一些我们 看不到的文件夹,那就是所谓的隐藏文件,大部分的隐藏文件,我们是没有权限操作的,所有对我们来说没必要,必须干掉, 还有就是给文件列表排序,方面查找,其实在现在的文件夹管理里,不知道大家有没有发现,就是在界面的最右或者最左边有 一个从A-Z竖向排列的选项,点击每个字母,它会跳到以那个字开头的文件列表项去,这是比较好用的一款东西,因为APP是之 前写的,也就没用到新式的东西了,咱们就说说粗

Android 编程之入门开发文件夹管理器开发抽屉与文件分类-4

在此文件夹管理APP里,我们可以尝试引用一些新的元素,在这里我给打击介绍一个叫抽屉的布局,QQ就用到了抽屉布局,不 过他们又在原有的基础上自己开发了新的抽屉布局,并且还蛮高大上的,顺便说说分类管理,这些都只是很初步的一些写法, 因为是前期写的,后期就没做完善了,适合一般入门级伙伴学习,首先给大家说说抽屉布局,还是以图形的方式介绍比较好 抽屉布局示例,点击红色方框按钮即可弹出抽屉布局,或者顺势向右划屏也可以弹出抽屉布局: 弹出布局: 文件管理文类: 图片分类: 音乐分类: 视频分类: 对后缀类型匹

Android文件夹管理器源码实现

一.资源管理器介绍 现在在一些移动终端上面都会有自带的资源管理器,其实其并非是Android系统自带,而是手机产商与app开发商的合作而导致融合,借助第三方的开发软件预装在出厂的手机,是新时代下的另一个霸王条款,还不能自行删除,十分麻烦. 背景铺垫完毕,由于十分讨厌这种不公平的手段,为此自行写一个实现文件资源管理器,功能基本上实现,实用不美观,不喜勿喷! 二.实现函数详解 1.显示文件列表 /** * 扫描显示文件列表 * @param path */ private void showFile

C#WinForm treeview 简单文件夹管理器 查看文件夹下的文件,子文件下的文件

1 查看的文件夹中的内容 2 UI 3 代码 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.IO; 7 using System.Linq; 8 using System.Text; 9 using System.Threading.Tasks; 10 usi

Android 编程之入门开发文件夹管理器开发文件事件操作-2

上一篇博客,我们已经得到了文件夹列表,我们需要对文件列表子项添加事件,比如我们点击的是文件,就执行 打开操作,点击的是文件夹执行打开文件夹操作,遍历文件清单,以此类推直到最后一个是文件位置,关于文件 与文件夹的处理后面会讲到 在我的程序里,我写了一个类,对文件进行处理,FileOpreationUitl: package com.example.util; import java.io.File; import java.io.FileInputStream; import java.io.Fi

linux:文件及文件夹管理

http://blog.csdn.net/pipisorry/article/details/39854265 查看用户的信息 pika:~$id pikauid=1000(pika) gid=1000(pika) groups=1000(pika),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),108(lpadmin),124(sambashare),125(docker) 文件夹与路径 cd:变换目弽pwd:显示弼前目弽mkdir:建立一个新的文

文件夹管理

一,文件夹管理 1.文件夹的创建: mkdir  建立目录 mkdir [option] 目录名 -v  显示信息 -p 递归创建 [[email protected] 桌面]# mkdir -pv test/a/b/c mkdir: 已创建目录 "test" mkdir: 已创建目录 "test/a" mkdir: 已创建目录 "test/a/b" mkdir: 已创建目录 "test/a/b/c" 2.文件夹的删除: rm