概述
//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对象的创建 :
NSDictionary *attr =[fm fileAttributesAtPath: fname traverseLink: NO] ; //文件属性
NSData *data =[fm contentsAtPath:@"filename"];//文件内容
常见的NSFileManager目录的方法:
-(NSString *)currentDirectoryPath
-(BOOL)changeCurrentDirectoryPa
-(BOOL)copyPath:from toPath:to handler:handler
-(BOOL)createDirectoryAtPath:path attributes:attr
-(BOOL)fileExistsAtPath:path isDirectory:(BOOL *)flag
-(NSArray *)contentsOfDirectoryAtPat
-(NSDirectoryEnumerator *)enumeratorAtPath:path
-(BOOL)removeFileAtPath:path handler:handler
-(BOOL)movePath:from toPath:to handler:handler
path= [fm currentDirectoryPath] ;
NSArray *dirarray;
NSDirectoryEnumerator *direnu;
direnu = [fm enumeratorAtPath:path];
NSLog(@"contents of %@n",path);
BOOL flag;
while((path = [direnu nextObject])!=nil)
{
}
path= [fm currentDirectoryPath] ;
dirarray = [fm contentsOfDirectoryAtPat
NSLog(@"%@ ",dirarray);
常用路径工具函数
NSString * NSUserName(); 返回当前用户的登录名
NSString * NSFullUserName(); 返回当前用户的完整用户名
NSString * NSHomeDirectory(); 返回当前用户主目录的路径
NSString * NSHomeDirectoryForUser(); 返回用户user的主目录
NSString * NSTemporaryDirectory(); 返回可用于创建临时文件的路径目录
常用路径工具方法
-(NSString *) pathWithComponents:components
-(NSArray *)pathComponents
-(NSString *)lastPathComponent
-(NSString *)pathExtension
-(NSString *)stringByAppendingPathCom
-(NSString *)stringByAppendingPathExt
-(NSString *)stringByDeletingPathComp
-(NSString *)stringByDeletingPathExte
-(NSString *)stringByExpandingTildeIn
-(NSString *)stringByResolvingSymlink
-(NSString *)stringByStandardizingPat
-
使用路径NSPathUtilities.h
tempdir = NSTemporaryDirectory(); 临时文件的目录名
path = [fm currentDirectoryPath];
[path lastPathComponent]; 从路径中提取最后一个文件名
fullpath = [path stringByAppendingPathCom
extenson = [fullpath pathExtension]; 路径名的文件扩展名
homedir = NSHomeDirectory();用户的主目录
component = [homedir pathComponents];
NSProcessInfo类:允许你设置或检索正在运行的应用程序的各种类型信息
(NSProcessInfo *)processInfo
-(NSArray*)arguments
-(NSDictionary *)environment
-(int)processIdentity
-(NSString *)processName
-(NSString *)globallyUniqueString
-(NSString *)hostname
-(unsigned int)operatingSystem
-(NSString *)operatingSystemName
-(NSString *)operatingSystemVersionSt
-(void)setProcessName:(NSString *)name
过滤数组中的文件类型
///
基本文件操作NSFileHandle
常用NSFileHandle方法
(NSFileHandle *)fileHandleForReadingAtPa
(NSFileHandle *)fileHandleForWritingAtPa
(NSFileHandle *)fileHandleForUpdatingAtP
-(NSData *)availableData
-(NSData *)readDataToEndOfFile
-(NSData *)readDataOfLength:(unsigned int)bytes 从文件读取指定数目bytes的内容
-(void)writeData:data
-(unsigned long long) offsetInFile
-(void)seekToFileOffset:offset
-(unsigned long long) seekToEndOfFile
-(void)truncateFileAtOffset:offset
-(void)closeFile
获取文件大小
Using the C FILE type:
int getFileSizeFromPath(char * path)
{
}
or in XCode use the NSFileManager:
NSFileManager * filemanager = [[NSFileManager alloc]init];
if([filemanager fileExistsAtPath:[self getCompletePath] isDirectory:&isDirectory]){
// file size
}
最后
以上就是失眠八宝粥为你收集整理的文件操作的全部内容,希望文章能够帮你解决文件操作所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复