本文转载至:http://www.cnblogs.com/pengyingh/articles/2350345.html

天牛 感谢原创作者的硕果

//file
文件操作
NSFileManager 
常见的NSFileManager文件的方法:
-(BOOL)contentsAtPath:path                从文件中读取数据
-(BOOL)createFileAtPath:path contents:(BOOL)data attributes:attr      向一个文件写入数据
-(BOOL)removeFileAtPath: path handler: handler   删除一个文件
-(BOOL)movePath: from toPath: to handler: handler 重命名或移动一个文件(to可能已经存在)
-(BOOL)copyPath:from toPath:to handler: handler 复制文件 (to不能存在) 
-(BOOL)contentsEqualAtPath:path1 andPath:path2    比较两个文件的内容 
-(BOOL)fileExistsAtPath:path    测试文件是否存在 
-(BOOL)isReadablefileAtPath:path 测试文件是否存在,且是否能执行读操作 
-(BOOL)isWritablefileAtPath:path 测试文件是否存在,且是否能执行写操作 
-(NSDictionary *)fileAttributesAtPath:path traverseLink:(BOOL)flag   获取文件的属性 
-(BOOL)changeFileAttributes:attr atPath:path                        更改文件的属性 
 
NSFileManager对象的创建 :
    NSFileManager *fm;
    fm = [NSFileManager defaultManager]; 
NSDictionary *attr =[fm fileAttributesAtPath: fname traverseLink: NO] ; //文件属性
             NSLog(@"file size is:%i bytes ",[[attr objectForKey:NSFileSize] intValue]);
NSData *data =[fm contentsAtPath:@"filename"];//文件内容
 
 
常见的NSFileManager目录的方法:
-(NSString *)currentDirectoryPath                      获取当前目录 
-(BOOL)changeCurrentDirectoryPath:path                更改当前目录 
-(BOOL)copyPath:from toPath:to handler:handler      复制目录结构,to不能已经存在 
-(BOOL)createDirectoryAtPath:path attributes:attr    创建目录 
-(BOOL)fileExistsAtPath:path isDirectory:(BOOL *)flag       测试文件是否为目录 (flag存储结构yes/no) 
-(NSArray *)contentsOfDirectoryAtPath:path              列出目录的内容 
-(NSDirectoryEnumerator *)enumeratorAtPath:path  枚举目录的内容 
-(BOOL)removeFileAtPath:path handler:handler    删除空目录 
-(BOOL)movePath:from toPath:to handler:handler    重命名或移动一个目录,to不能是已经存在的 
 
path= [fm currentDirectoryPath] ;
NSArray *dirarray;
NSDirectoryEnumerator *direnu;
 
direnu = [fm enumeratorAtPath:path];
NSLog(@"contents of %@\n",path);
BOOL flag;
while((path = [direnu nextObject])!=nil)
{
            NSLog(@"%@ ",path);
            [fm fileExistsAtPath:path isDirectory:&flag];
            if(flag == YES)
            [direnu skipDescendents]; //跳过子目录 
}
path= [fm currentDirectoryPath] ;
dirarray = [fm contentsOfDirectoryAtPath:path];
NSLog(@"%@ ",dirarray);
 
常用路径工具函数
NSString * NSUserName(); 返回当前用户的登录名 
NSString * NSFullUserName(); 返回当前用户的完整用户名 
NSString * NSHomeDirectory(); 返回当前用户主目录的路径 
NSString * NSHomeDirectoryForUser(); 返回用户user的主目录 
NSString * NSTemporaryDirectory(); 返回可用于创建临时文件的路径目录 
 
常用路径工具方法
-(NSString *) pathWithComponents:components                         根据components中元素构造有效路径 
-(NSArray *)pathComponents                                          析构路径,获取路径的各个部分 
-(NSString *)lastPathComponent                                       提取路径的最后一个组成部分 
-(NSString *)pathExtension                                           路径扩展名 
-(NSString *)stringByAppendingPathComponent:path                    将path添加到现有路径末尾 
-(NSString *)stringByAppendingPathExtension:ext                     将拓展名添加的路径最后一个组成部分 
-(NSString *)stringByDeletingPathComponent                           删除路径的最后一个部分 
-(NSString *)stringByDeletingPathExtension                           删除路径的最后一个部分 的扩展名 
-(NSString *)stringByExpandingTildeInPath                            将路径中的代字符扩展成用户主目录(~)或指定用户主目录(~user) 
-(NSString *)stringByResolvingSymlinksInPath                         尝试解析路径中的符号链接 
-(NSString *)stringByStandardizingPath                               通过尝试解析~、..、.、和符号链接来标准化路径 
使用路径NSPathUtilities.h 
tempdir = NSTemporaryDirectory(); 临时文件的目录名 
path = [fm currentDirectoryPath];
[path lastPathComponent]; 从路径中提取最后一个文件名 
fullpath = [path stringByAppendingPathComponent:fname];将文件名附加到路劲的末尾 
extenson = [fullpath pathExtension]; 路径名的文件扩展名 
homedir = NSHomeDirectory();用户的主目录 
component = [homedir pathComponents];  路径的每个部分 
 
NSProcessInfo类:允许你设置或检索正在运行的应用程序的各种类型信息
(NSProcessInfo *)processInfo                                  返回当前进程的信息
-(NSArray*)arguments                                           以NSString对象数字的形式返回当前进程的参数
-(NSDictionary *)environment                                   返回变量/值对词典。描述当前的环境变量
-(int)processIdentity                                          返回进程标识
-(NSString *)processName                                       返回进程名称
-(NSString *)globallyUniqueString                              每次调用该方法都会返回不同的单值字符串,可以用这个字符串生成单值临时文件名   
-(NSString *)hostname                                          返回主机系统的名称 
-(unsigned int)operatingSystem                                 返回表示操作系统的数字 
-(NSString *)operatingSystemName                                     返回操作系统名称 
-(NSString *)operatingSystemVersionString                                     返回操作系统当前版本
-(void)setProcessName:(NSString *)name                                将当前进程名称设置为name 
过滤数组中的文件类型  : [fileList pathsMatchingExtensions:[NSArrayarrayWithObject:@"jpg"]];
///////////////////////////////////////////////////////////////////////////////////////////////////////////
基本文件操作NSFileHandle
常用NSFileHandle方法
(NSFileHandle *)fileHandleForReadingAtPath:path                        打开一个文件准备读取
(NSFileHandle *)fileHandleForWritingAtPath:path                        打开一个文件准备写入
(NSFileHandle *)fileHandleForUpdatingAtPath:path                        打开一个文件准备更新(读取和写入)
-(NSData *)availableData                                                  从设备或通道返回可用数据
-(NSData *)readDataToEndOfFile                                            读取其余的数据直到文件末尾(最多UINT_MAX字节)
-(NSData *)readDataOfLength:(unsigned int)bytes 从文件读取指定数目bytes的内容
-(void)writeData:data                  将data写入文件
-(unsigned long long) offsetInFile      获取当前文件的偏移量
-(void)seekToFileOffset:offset         设置当前文件的偏移量 
-(unsigned long long) seekToEndOfFile      将当前文件的偏移量定位的文件末尾
-(void)truncateFileAtOffset:offset        将文件的长度设置为offset字节
-(void)closeFile                           关闭文件
 

获取文件大小

 
Using the C FILE type:

int getFileSizeFromPath(char * path)
{
     FILE * file;
     int fileSizeBytes = 0;
     file = fopen(path,"r");
     if(file>0){
         fseek(file, 0, SEEK_END);
         fileSizeBytes = ftell(file);
         fseek(file, 0, SEEK_SET);
         fclose(file);
     }
     return fileSizeBytes;
}

or in XCode use the NSFileManager:

NSFileManager * filemanager = [[NSFileManager alloc]init];
if([filemanager fileExistsAtPath:[self getCompletePath] isDirectory:&isDirectory]){

NSDictionary * attributes = [filemanager attributesOfItemAtPath:[self getCompletePath] error:nil];

// file size
     NSNumber *theFileSize;
     if (theFileSize = [attributes objectForKey:NSFileSize])

_fileSize= [theFileSize intValue];
}

 
 
 

NSFileManager和NSFileHandle(附:获取文件大小 )的更多相关文章

  1. ios NSFileManager和NSFileHandle(附:获取文件大小 )

    转自 http://blog.csdn.net/zhibudefeng/article/details/7795946 //file 文件操作 NSFileManager  常见的NSFileMana ...

  2. NSFileManager和NSFileHandle使用

    一.NSFileManager: 1.1.获取NSFileManager NSFileManager *manager = [NSFileManager defaultManager];     NS ...

  3. oc 根据文件路径获取文件大小

    第一种封装: -(NSInteger)getSizeOfFilePath:(NSString *)filePath{ /** 定义记录大小 */ NSInteger totalSize = ; /** ...

  4. IOS之NSFileManager 和NSFileHandle

    在现阶手机app的临时缓存文件渐渐增多,在app开发中对于移动设备文件的操作越来越多,我们IOS中对于文件的操作主要涉及两个类NSFileManager 和NSFileHandle,下面我们就看看如何 ...

  5. 客户端用javascript获取文件大小

    客户端用javascript获取文件大小 1 ie实现代码如下: <script type="text/javascript" language="javascri ...

  6. python获取文件大小

    python获取文件大小 # !/usr/bin/python3.4 # -*- coding: utf-8 -*- import os # 字节bytes转化kb\m\g def formatSiz ...

  7. C/C++多种方法获取文件大小(转)

    源码下载:点击下载 源码如下: #include <iostream> #include <io.h> #include <sys\stat.h> #include ...

  8. js获取文件大小

    var file = urlBox.doc.activeElement.files[0]||urlBox.files[0] ; if (file) { var fileSize = 0; if (fi ...

  9. [WinAPI] API 10 [创建、打开、读写文件,获取文件大小]

    在Windows系统中,创建和打开文件都是使用API函数CreateFile,CreateFile通过指定不同的参数来表示是新建一个文件,打开已经存在的文件,还是重新建立文件等.读写文件最为直接的方式 ...

随机推荐

  1. ID、Class和标签选择器优先级

    按一般论:ID > Class > 标签 1.如以下样式: <div id="id" class="class">选择器优先权</ ...

  2. git hook部署代码

    git 提供了钩子功能,当某个操作发生时,可以执行某个动作. ftp上传时没有文件比较,虽然可以winscp提供了同步功能但是不够强大,而且文件多了,会花费比较长的时间. 1.先在主机上搭建一个git ...

  3. Maven学习之(一)基本配置

    安装maven还几次没成功,不过最后还是查资料成功了,所以记录一下. 1.安装JDK,比较简单,就不说明了. 2.配置java的环境变量 JAVA_HOME (最开始因为java配置成C:\Progr ...

  4. 【Unity/Kinect】获取预制的手势信息KinectInterop.HandState

    Kinect使用了枚举KinectInterop.HandState来描述手势. 该手势指的是手掌的状态(张开/握拳),而不是说整个手臂的肢体动作(Gesture). 同样是需要嵌套在Kinect获取 ...

  5. 【WPF/C#】联网异步获取二进制文件(如图片)的流程

    步骤: 联网异步获取Json数据. 使用Json.NET工具,反序列化Json为对应的实体类,获得该实体类的对象. 从对象身上获取图片路径(实体类中定义了头像图片是string类型的文件路径). 根据 ...

  6. sqlserver被锁的表以及如何解锁

    查看sqlserver被锁的表以及如何解锁   查看被锁表: select   request_session_id   spid,OBJECT_NAME(resource_associated_en ...

  7. uboot在nandflash存储时内存和NandFlash存储空间

    硬件采用nandflash,nandflash为8位数据宽度,没有dataflash和norflash. Nandflash空间分配为 bootstrap + u-boot + env + linux ...

  8. win 10 桌面路径还原到C盘拒绝访问

    解决: 问是否更改那里 点 否 即可 题外话: win10桌面注册表路径 HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Exp ...

  9. CentOS6.5下安装Nexus

    一.环境准备 (1) CentOS6.5 (2) nexus-latest-bundle.tar.gz 下载地址:http://www.sonatype.org/nexus/archived/ 二.安 ...

  10. node-webkit播放目录下所有网页文件

    1.编写index.html文件 这里我们需要播放某一个目录下所有的网页文件,要做到切换不同的网页,可以在index.html中使用iframe.通过js动态改变iframe的src属性,从而动态地切 ...