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

//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];
}

  

时间: 2024-08-28 00:53:59

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

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

转自 http://blog.csdn.net/zhibudefeng/article/details/7795946 //file 文件操作 NSFileManager 常见的NSFileManager文件的方法: -(BOOL)contentsAtPath:path                从文件中读取数据 -(BOOL)createFileAtPath:path contents:(BOOL)data attributes:attr      向一个文件写入数据 -(BOOL)rem

NSFileManager和NSFileHandle

//file 文件操作 NSFileManager 常见的NSFileManager文件的方法: -(BOOL)contentsAtPath:path                从文件中读取数据 -(BOOL)createFileAtPath:path contents:(BOOL)data attributes:attr      向一个文件写入数据 -(BOOL)removeFileAtPath: path handler: handler   删除一个文件 -(BOOL)movePat

IOS之NSFileManager 和NSFileHandle

在现阶手机app的临时缓存文件渐渐增多,在app开发中对于移动设备文件的操作越来越多,我们IOS中对于文件的操作主要涉及两个类NSFileManager 和NSFileHandle,下面我们就看看如何使用这两个类: 1.文件创建 //初始化一个NSFileManager类defaultManager方法为单例模式,通过单例模式进行初始化 NSFileManager * fileManager =[NSFileManager defaultManager]; //拼接路径 NSString * p

win10 uwp 从StorageFile获取文件大小

本文主要:获取文件大小 private async Task<ulong> FileSize(Windows.Storage.StorageFile file) { var size = await file.GetBasicPropertiesAsync(); return size.Size; }//32ddd4227a66713e1329214424c4be9b 在群里看到有大神问我就写出,虽然少,在没看到他们说之前没想到,九幽开发者:53078485 参见:http://stackov

C/C++ 获取文件大小

面试的时候面试官提到了怎样获取文件的大小,现在总结如下: 在C语言中测试文件的大小,主要使用二个标准函数. 1.fseek 函数原型:int fseek ( FILE * stream, long int offset, int origin ); 参数说明:stream,文件流指针:offest,偏移量:orgin,原(始位置.其中orgin的可选值有SEEK_SET(文件开始).SEEK_CUR(文件指针当前位置).SEEK_END(文件结尾). 函数说明:对于二进制模式打开的流,新的流位置

Linux命令:cp、time、cut、awk 、获取文件大小命令

最近写了一个shell脚本,用到了cp.time.cut.awk命令,把他们的使用方法给写下来: 1.cp和mv cp命令:用来完成文件的复制,从源文件夹复制到目标文件夹.执行后源文件夹和目标文件夹均有源文件. mv 命令:用来移动文件.执行后,源文件夹中没有源文件.我经常用mv命令来重命名文件 注意:cp命令源文件夹与目标文件夹不能使同一个:而在mv中可以是同一个. 命令解析: #cp [-R] 源文件 目标文件夹 cp -R 参数 ---当复制整个文件夹时使用. #mv 源文件 目标文件夹

C# 获取文件大小,创建时间,文件信息,FileInfo类的属性表

C# 获取文件大小,创建时间,文件信息,FileInfo类的属性表 2011-12-13 09:00:40| 分类: C# |举报|字号 订阅 OpenFileDialog openFileDialog1 = new OpenFileDialog(); if(openFileDialog1.ShowDialog() == DialogResult.OK) { openFileDialog1.FileName; System.IO.FileInfo file = new System.IO.Fil

【MFC】获取文件大小的方法

[转载]原文地址:http://blog.csdn.net/coderwu/article/details/5652056 MFC 下可以通过 CFileStatus 获取文件大小. 1 ULONGLONG size; 2 CString strFilePath; 3 CFileStatus fileStatus; 4 5 if (CFile::GetStatus(strFilePath, fileStatus)) 6 { 7 size = fileStatus.m_size; 8 }

java获取文件大小的方法

目前Java获取文件大小的方法有两种: 1.通过file的length()方法获取: 2.通过流式方法获取: 通过流式方法又有两种,分别是旧的java.io.*中FileInputStream的available()方法和新的java..nio.*中的FileChannel 下面依次介绍这几种方法: 首先选择一个文件并查看这个文件在windows中显示的大小,为了测试准确性,我这里选取了一个大文件(超过2GB) 查看这个文件在windows中显示的大小: 使用在线转换工具将其转换成字节: 可以看