IOS manages files and directories

1. Common NSFileManager file methods

-(NSData *)contentsAtPath:path //Read data from a file

-(BOOL)createFileAtPath: path contents:(NSData *)data attributes:attr //Write data to a file

-(BOOL)removeItemAtPath:path error:err //Remove a file

-(BOOL)moveItemAtPath: from toPath:to error:err //Rename or move a file (to cannot already exist)

-(BOOL)copyItemAtPath:from toPath:to error:err //Copy the file (to cannot already exist)

-(BOOL)contentsEqualAtPath:path andPath:path2 //Compare the contents of two files

-(BOOL)fileExistAtPath:path //Test whether the file exists

-(BOOL)isReadableFileAtPath:path //Test whether the file exists and whether the read operation can be performed  

-(BOOL)isWriteableFileAtPath:path //Test whether the file exists and whether the write operation can be performed  

-(NSDictionary *)attributesOfItemAtPath:path error:err //Get the attributes of the file  

-(BOOL)setAttributesOfItemAtPath:attr error:err //Change the attributes of the file

2. Use a directory

-(NSString *)currentDirectoryPath //Get the current directory

-(BOOL)changeCurrentDirectoryPath:path //Change the current directory

-(BOOL)copyItemAtPath:from toPath:to error:err //Copy directory structure (to cannot already exist)

-(BOOL)createDirectoryAtPath:path withIntermediateDirectories:(BOOL)flag attribute:attr //Create a new directory

-(BOOL)fileExistAtPath:path isDirectory:(BOOL*)flag //Test whether the file is a directory (the result stored in the flag is YES/NO)

-(NSArray *)contentsOfDirectoryAtPath:path error:err //List directory contents

-(NSDirectoryEnumerator *)enumeratorAtPath:path //Enumerate the contents of the directory

-(BOOL)removeItemAtPath:path error:err //Remove empty directory

-(BOOL)moveItemAtPath:from toPath:to error:err //Rename or move a directory (to cannot already exist)

3. Common path tool method

+(NSString *)pathWithComponens:components //Construct a valid path based on the elements in components

-(NSArray *)pathComponents //Destruct the path and get the parts that make up this path

-(NSString *)lastPathComponent //Extract the last component of the path

-(NSString *)pathExtension //Extract its extension from the last component of the path

-(NSString *)stringByAppendingPathComponent:path //Add path to the end of existing path

-(NSString *)stringByAppendingPathExtension:ext //Add the specified extension to the last component of the path

-(NSString *)stringByDeletingLastPathComponent //Delete the last component of the path

-(NSString *)stringByDeletingPathExtension  //从文件的最后一部分删除扩展名

-(NSString *)stringByExpandingTileInPath   //将路径中代字符扩展成用户主目录(~)或指定用户的主目录(~user)

-(NSString *)stringByresolvingSymlinksInPath  //尝试解析路径中的符号链接

-(NSString *)stringByStandardizingPath  //通过尝试解析~、..(父目录符号)、.(当前目录符号)和符号链接来标准化路径

4、常用的路径工具函数

NSString* NSUserName(void)  //返回当前用户的登录名

NSString* NSFullUserName(void)  //返回当前用户的完整用户名

NSString* NSHomeDirectory(void)  //返回当前用户主目录的路径

NSString* NSHomeDirectoryForUser(NSString* user)  //返回用户user的主目录

NSString* NSTemporaryDirectory(void)  //返回可用于创建临时文件的路径目录

5、常用的IOS目录

Documents(NSDocumentDirectory)  //用于写入应用相关数据文件的目录,在ios中写入这里的文件能够与iTunes共享并访问,存储在这里的文件会自动备份到云端

Library/Caches(NSCachesDirectory)  //用于写入应用支持文件的目录,保存应用程序再次启动需要的信息。iTunes不会对这个目录的内容进行备份

tmp(use NSTemporaryDirectory())  //这个目录用于存放临时文件,只程序终止时需要移除这些文件,当应用程序不再需要这些临时文件时,应该将其从这个目录中删除

Library/Preferences  //这个目录包含应用程序的偏好设置文件,使用 NSUserDefault类进行偏好设置文件的创建、读取和修改

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326647054&siteId=291194637