File Operation nsfilemanager Common Nsfilemanager file Methods:-(BOOL) Contentsatpath:path reading data from a file-(bool) Createfileatpath:path contents: (BOOL) data attributes:attr Write data to a file-(bool) Removefileatpath:path handler:handler Delete a file-(bool) Movepath:from topath:to Handler:handler re- Name or move a file (to may already exist)-(bool) copypath:from topath:to handler:handler Copy file (to cannot exist) -(bool) Contentsequalatpath: path1 andpath:path2 Compare two files content -(BOOL) fileexistsatpath:path test file exists - (bool) Isreadablefileatpath:path test file exists and can perform read operation -(BOOL) Iswritablefileatpath:path test file exists and can perform write operation -(nsdictionary *) Fileattributesatpath:path Traverselink: (BOOL) flag Get the properties of the file - (BOOL) changefileattributes:attr atpath:path / nbsp; Change the properties of a file nsfilemanager object creation: Nsfilemanager *fm; FM = [Nsfilemanager defAultmanager]; nsdictionary *attr =[fm fileattributesatpath:fname Traverselink:no]; File properties nslog (@ "File size is:%i bytes", [[attr Objectforkey:nsfilesize] Intv Alue]); NSData *data =[fm contentsatpath:@ "filename"];//file contents Common Nsfilemanager Directory method:-(NSString * ) Currentdirectorypath Get current directory -(BOOL) Chan Gecurrentdirectorypath:path Change current directory -(BOOL) copypath:from Topath:to handler:handler Copy directory structure, to cannot already exist -(BOOL) Createdirectoryatpath:path Attributes:attr Create directory -(BOOL) Fileexistsatpath:path isdirectory: (BOOL *) flag Test whether the file is a directory (flag storage structure yes/no) -(Nsarray *) Contentsofdirectoryatpath:path & nbsp; List contents of directory -(Nsdirectoryenumerator *) Enumeratoratpath:path Enumerating contents of directory -(BOOL) Removefileatpath:patH handler:handler Delete empty directory -(BOOL) movepath:from topath:to handler:handler Rename or move a directory, To cannot be an already existing 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); &NB Sp [FM fileexistsatpath:path isdirectory:&flag]; if ( Flag = = YES) [Direnu skipdescendents]; Skip subdirectories }path= [FM Currentdirectorypath];D IRARRAY = [FM Contentsofdirectoryatpath:path]; NSLog (@ "%@", Dirarray); Common path tool function NSString * NSUSERNAME (); Returns the login name of the current user nsstring * Nsfullusername (); Returns the full user name of the current user nsstring * Nshomedirectory (); Returns the path to the current user's home directory nsstring * Nshomedirectoryforuser (); Returns the user's home directory nsstring * Nstemporarydirectory (); Returns the path directory that can be used to create temporary files &nbSP; Common Path Tool Method-(NSString *) pathwithcomponents:components &NBS P construct valid paths based on elements in Components -(Nsarray *) pathcomponents &NBS P -to get the various parts of the path (NSString * ) lastpathcomponent &NBSP ; Extract the last component of the path -(NSString *) pathextension - Path extension (NSString *) ) Stringbyappendingpathcomponent:path Add path to the end of an existing path -(NSString *) stringbyappendingpathextension:ext &NBS P Add extension name to the last component of the path -(NSString *) stringbydeletingpathcomponent &NB Sp Delete the last part of the path -(NSString *) stringbydeletingpathextension & nbsp Delete the last section of the path extension -(NSString *) Stringbyexpandingtildeinpath & nbsp to expand the generation of characters in the path to the user home directory (~) or the designated household directory (~user) - (NSString *) Stringbyresolvingsymlinksinpath & nbsp Attempt to parse a symbolic link in a path -(NSString *) Stringbystandardizingpath and nbsp standardize paths by trying to parse ~ 、..、.、 and Symbolic Links - Use path Nspathutilities.h tempdir = Nstemporarydirectory (); The directory name of the temporary file path = [FM Currentdirectorypath]; [Path lastpathcomponent]; Extract the last file name from the path fullpath = [path StringbyapPendingpathcomponent:fname]; Append the file name to the end of the road extenson = [FullPath pathextension]; pathname filename extension homedir = nshomedirectory (); user's home directory component = [Homedir pathcomponents]; Each part of the path nsprocessinfo class: Allows you to set or retrieve various types of information for a running application (Nsprocessinfo *) processinfo Return current process information-(nsarray*) Argum Ents &NBSP ; return parameters of the current process as NSString object numbers-(nsdictionary *) environment &NBS P Returns a variable/value pair dictionary. Describes the current environment variable-(int) processidentity return process identification-(NSString *) processName & nbsp &nbSp return process name-(NSString *) Globallyuniquestrin G Each call to this method will return a different single value A string that can be used to generate a single-value temporary file name -(NSString *) hostname &NB Sp Return the name of the host system -(unsigned int) operatingsystem back to represent the operating system Digital -(NSString *) operatingsystemname &NBS P return operating system name -(NSString *) operatingsystemversionstring &N Bsp Return operating system current version-(void) Setproc Essname: (Nsstring *) name &N Set the current process name to name the file type in the filter array : [filelist pathsmatchingextensions:[nsarrayarraywithobject:@ "JPG" ]];///////////////////////////////////////////////////////////////////////////////////////////////////////////Basic file Operation Nsfil Ehandle Common Nsfilehandle methods (Nsfilehandle *) Filehandleforreadingatpath:path Open a file ready to read (Nsfilehandle *) Filehandleforwritingatpath:path Open a file ready to write (Nsfilehandle * ) Filehandleforupdatingatpath:path Open a text Ready to update (read and Write)-(NSData *) availabledata & nbsp   Return available data from device or channel-(NSData *) readdatatoendoffile &NB Sp to read the rest of the data until the end of the file (up to Uint_max bytes)-(NSData *) Readdat Aoflength: (unsigned int) bytes reads a specified number of bytes content from a file-(void) Writedata:data and nbsp writes data to file-(unsigned long long) offsetinfile Gets the offset of the current file-(void) Seektofileoffset:offset Set the offset of the current file -(unsigned long) seektoendoffile Position the offset of the current file at the end of the file-(void) Truncatefileatoffset:offset Set the length of the file to offset bytes-(void) CloseFile Close file Get File Size
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 and Nsfilehandle