zoukankan      html  css  js  c++  java
  • 文件处理

    使用目录 

    目录方法: 

    - (NSString *)currentDirectoryPath 
    获取当前目录 

    - (BOOL)changeCurrentDirectoryPath:(NSString *)path 
    更改当前目录 

    - (BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error 
    复制目录或文件 

    - (BOOL)createDirectoryAtPath:(NSString *)path withIntermediateDirectories:(BOOL)createIntermediates attributes:(NSDictionary *)attributes error:(NSError **)error 
    创建一个新的目录 

    - (BOOL)fileExistsAtPath:(NSString *)path isDirectory:(BOOL *)isDirectory 
    测试文件是不是目录 

    - (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error 
    列目录(不会递归) 
    - (NSDirectoryEnumerator *)enumeratorAtPath:(NSString *)path 
    枚举目录内容(会递归) 
    - (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error 
    删除一个文件,文件夹,链接 

    - (BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error 
    移动(重命名)目录到一个指定的路径 

    NSPathUtilities.h 包含了NSString的函数和分类扩展,用于操作路径名. 
    #import <Foundation/NSObject.h> 
    #import <Foundation/NSAutoreleasePool.h> 
    #import <Foundation/NSString.h> 
    #import <Foundation/NSFileManager.h> 
    #import <Foundation/NSArray.h> 
    #import <Foundation/NSPathUtilities.h> 

    int main (int argc, const char * argv[]) { 

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 

    NSFileManager *fm; 
    //NSFileManager对象 
    NSString *dirName = @"testdir"; 
    NSString *path; 

    fm = [[NSFileManager alloc]init]; 

    path = [fm currentDirectoryPath]; 
    NSLog(@"%@",path); 
    //获取当前目录路径. 

    if ([fm createDirectoryAtPath:dirName withIntermediateDirectories:YES attributes:nil error:NULL] == NO) { 
    NSLog(@"Couldn't create dir"); 
    return 1; 

    //新建目录 

    if ([fm moveItemAtPath:dirName toPath"newdir" error:NULL] == NO) { 
    NSLog(@"Directory rename failed"); 
    return 2; 

    //重命名目录 

    if ([fm changeCurrentDirectoryPath"newdir"] == NO) { 
    NSLog(@"change directory failed"); 
    return 3; 

    //修改路径到新目录 

    path = [fm currentDirectoryPath]; 
    //返回当前路径 

    NSLog(@"Current directory path is %@",path); 
    //打印 

    NSLog(@"successful"); 

    [pool drain]; 
    return 0; 


    枚举目录中的内容: 

    NSPathUtilities.h 包含了NSString的函数和分类扩展,用于操作路径名. 
    #import <Foundation/NSObject.h> 
    #import <Foundation/NSAutoreleasePool.h> 
    #import <Foundation/NSString.h> 
    #import <Foundation/NSFileManager.h> 
    #import <Foundation/NSArray.h> 
    #import <Foundation/NSPathUtilities.h> 

    int main (int argc, const char * argv[]) { 
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 

    NSFileManager *fm; 
    NSString *path; 
    NSDirectoryEnumerator *dirEnum; 
    NSArray *dirArray; 

    fm = [[NSFileManager alloc]init]; 
    path = [fm currentDirectoryPath]; 
    //当前目录 
    dirEnum = [fm enumeratorAtPath:path]; 
    //开始枚举过程,将其存入dirEnum中. 
    NSLog(@"路径:%@",path); 
    //打印当前路径 

    while ((path = [ dirEnum nextObject]) != nil) { 
    NSLog(@"%@",path); 

    //向dirEnum发送nextObject消息,返回下一个文件路径,当没有可供枚举时,返回nil. 
    //enumeratorAtPath:方法会递归打印. 

    dirArray = [fm contentsOfDirectoryAtPath:[fm currentDirectoryPath] error:NULL]; 
    NSLog(@"内容为:"); 
    //使用contentsOfDirectoryAtPath:方法枚举当前路径中的文件并存入数组dirArray. 

    for (path in dirArray) 
    NSLog(@"%@",path); 

    //快速枚举数组中的内容并打印. 

    [pool drain]; 
    return 0; 


    使用路径: 
    NSPathUtilities.h 包含了NSString的函数和分类扩展,用于操作路径名. 
    #import 
    #import 
    #import 
    #import 
    #import 
    #import 

    int main (int argc, const char * argv[]) { 
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 

    NSFileManager *fm; 
    NSString *fName = @"path.m"; 
    NSString *upath = @"~s0s0/123/xxx/../321/./path.m"; 
    NSString *path, *tempdir, *extension, *homedir, *fullpath; 
    NSArray *components; 

    fm = [[NSFileManager alloc]init]; 
    tempdir = NSTemporaryDirectory(); 
    //NSTemporaryDirectory()函数返回可以用来创建临时文件的目录路径名,如果要创建文件,完成任务后要删除;确保文件名是唯一的. 

    NSLog(@"临时文件夹路径为:%@",tempdir); 

    path = [fm currentDirectoryPath]; 
    //返回当前目录路径 

    NSLog(@"父目录名: %@",[path lastPathComponent]); 
    //lastPathComponent方法用与提取路径中最后一个目录名. 

    fullpath = [path stringByAppendingPathComponent:fName]; 
    //stringByAppendingPathComponent:方法将文件名插入到路径的末尾,这样就能显示一个文件的完整路径. 
    NSLog(@"完整路径为:%@ to %@",fName,fullpath); 
    //打印一个文件的完整路径. 

    extension = [fullpath pathExtension]; 
    //pathExtension方法返回一个完整路径中的文件扩展名,如果没有扩展名,就返回空字符. 
    NSLog(@"extension for %@ is %@ ",fullpath, extension); 

    homedir = NSHomeDirectory(); 
    //NSHomeDirectory()函数返回当前用户的主目录 
    //NSHomeDirectoryForUser(username)函数可以提供用户名做参数,并返回主目录名 
    NSLog(@"主目录为 : %@",homedir); 

    components = [homedir pathComponents]; 
    //pathComponents方法返回一个数组,数组中包含一个路径中的每个组成部分. 

    for (path in components) 
    NSLog(@"%@",path); 
    //依次输出数组components中保存的元素. 

    NSLog(@"%@ -> %@",upath, [upath stringByStandardizingPath]); 
    //stringByStandardizingPath方法将原路径中的代字符转化为完整路径. 
    //如果是路径名字中出现代字符,可以使用stringByExpandingTildeInPath方法. 
    [pool drain]; 
    return 0; 

    }

  • 相关阅读:
    线圈与触发器
    线圈
    sourceinsight 宏
    linu  micro time
    删除 .svn 文件夹
    !!!
    ACE_MAIN
    窗体的一些主要属性
    http协议的几个概念
    保留每个name的最新日期的数据
  • 原文地址:https://www.cnblogs.com/lkjson/p/4337756.html
Copyright © 2011-2022 走看看