IOS document learning notes, ios document learning notes
File Operations
The foundation framework uses a file system to operate files.
The basic operations are provided by the NSFileManager class and the main functions are as follows:
- Create a new file
- Read data from files
- Write Data
- File rename
- Delete an object
- Whether the test file exists
- Determine the file size and other attributes
- Copy a file
- Test whether the two files are the same
You can use the NSFileHandle class to open a file and perform read/write operations on it.
- Open the file and perform read/write operations
- Find the specified location in the file
- Reads the target bytes of a specified book from a file, or writes a specified number of bytes of data.
-(NSData *) contentsAtPath :( NSString *) path; |
Read data from a file |
-(BOOL) createFileAtPath :( NSString *) path contents :( NSData *) data attributes :( NSDictionary *) attr;
|
Write data to a file |
-(BOOL) removeItemAtPath :( NSString *) path error :( NSError **) error |
Delete an object |
-(BOOL) moveItemAtPath :( NSString *) srcPath toPath :( NSString *) dstPath error :( NSError **) error |
Move files |
-(BOOL) copyItemAtPath :( NSString *) srcPath toPath :( NSString *) dstPath error :( NSError **) error |
Copy a file |
-(BOOL) contentsEqualAtPath :( NSString *) path1 andPath :( NSString *) path2;
|
Compare whether the content of the two files is the same |
-(BOOL) fileExistsAtPath :( NSString *) path; |
Whether the file exists |
-(BOOL) isReadableFileAtPath :( NSString *) path;
|
Whether the file can be read |
-(BOOL) isWritableFileAtPath :( NSString *) path;
|
Whether the file can be written |
-(NSDictionary *) attributesOfItemAtPath :( NSString *) path error :( NSError **) error |
Get Object Attributes |
-(BOOL) setAttributes :( NSDictionary *) attributes ofItemAtPath :( NSString *) path error :( NSError **) error |
Set file Properties |
|
|
Each path is an NSString object .~ Used as the abbreviation of the user's main directory .. Indicates the creation of the NSFileManager class in the parent directory.
<span style="font-family:Comic Sans MS;font-size:14px;">NSFileManager *fm = [NSFileManager defaultManager];</span>
When operating files in the NSData class, you need to frequently read data into a cache. The NSData class creates a cache.
Common NSFileManager directory Methods
@ Property (readonly, copy) NSString * currentDirectoryPath; |
Get Current Directory |
-(BOOL) changeCurrentDirectoryPath :( NSString *) path; |
Change current directory |
-(BOOL) copyItemAtPath :( NSString *) srcPath toPath :( NSString *) dstPath error :( NSError **) error |
Copy a file |
-(BOOL) createDirectoryAtPath :( NSString *) path withIntermediateDirectories :( BOOL) createIntermediates attributes :( NSDictionary *) attributes error :( NSError **) error |
Create a directory and Set Properties |
-(BOOL) fileExistsAtPath :( NSString *) path isDirectory :( BOOL *) isDirectory; |
Test path: Directory |
-(NSArray *) contentsOfDirectoryAtPath :( NSString *) path error :( NSError **) error |
List Directory content |
-(BOOL) removeItemAtPath :( NSString *) path error :( NSError **) error
|
Delete directory |
-(BOOL) moveItemAtPath :( NSString *) srcPath toPath :( NSString *) dstPath error :( NSError **) error
|
Move or rename a directory |
-(NSDirectoryEnumerator *) enumeratorAtPath :( NSString *) path;
|
Enumerate the directory content. If the directory is still in depth |
Path used: NSPathUtilities. hNSPathUtilities. h contains the classification extension of the NSString function, which can operate on the path name. Use these functions as much as possible.
Common Path tools and methods
+ (NSString *) pathWithComponents :( NSArray *) components; |
Construct a path based on the elements in the component |
@ Property (readonly, copy) NSArray * pathComponents; |
Analyze the path to obtain the parts of the path |
@ Property (readonly, copy) NSString * lastPathComponent; |
Obtain the last component of a path |
@ Property (readonly, copy) NSString * pathExtension; |
Get the extension from the last part of the component |
-(NSString *) stringByAppendingPathComponent :( NSString *) str; |
Add str to the end of an existing path |
-(NSString *) stringByAppendingPathExtension :( NSString *) str; |
Add extension to end |
@ Property (readonly, copy) NSString * stringByDeletingLastPathComponent; |
Delete last component |
@ Property (readonly, copy) NSString * stringByDeletingPathExtension; |
Delete the last part of the extension |
@ Property (readonly, copy) NSString * stringByExpandingTildeInPath; |
Extend the string to the main directory (~) |
@ Property (readonly, copy) NSString * stringByResolvingSymlinksInPath; |
Parse symbolic links in the path |
@ Property (readonly, copy) NSString * stringByStandardizingPath; |
Try to solve this problem ~ ... And symbolic links to standardize the path |
Using the NSProcessInfo class contains some methods that allow you to set or retrieve various types of information about running applications (that is, processes) NSProcessInfo class methods
+ (NSProcessInfo *) processInfo; |
Returns information about the current thread. |
@ Property (readonly, copy) NSArray * arguments; |
Returns the parameters of the current process as a string object number. |
@ Property (readonly, copy) NSDictionary * environment; |
Returns a dictionary describing the current environment variable. |
@ Property (readonly) int processIdentifier; |
Returns the process identifier, which is the unique number assigned to the process by the operating system. |
@ Property (readonly, copy) NSString * globallyUniqueString; |
Each time this method is called, different single-value strings are returned. Use this string to generate a temporary file name with a single value |
@ Property (readonly, copy) NSString * hostName; |
Returns the host system name. |
-(NSUInteger) operatingSystem |
Returns the number of the operating system. |
-(NSString *) operatingSystemName |
Returns the name of the operating system. |
@ Property (readonly, copy) NSString * operatingSystemVersionString; |
Returns the current version of the operating system. |
@ Property (copy) NSString * processName; |
Set the current process name to name |
|
|
Basic File Operations NSFileHandle use the methods provided by the NSFileHandle class to allow more effective use of files. There are three steps to process files: NSFileHandle Method
+ (Instancetype) fileHandleForReadingAtPath :( NSString *) path; |
Open a file and prepare to read it |
+ (Instancetype) fileHandleForWritingAtPath :( NSString *) path; |
Open a file and prepare for writing |
+ (Instancetype) fileHandleForUpdatingAtPath :( NSString *) path; |
Open a file to prepare for update (read/write) |
@ Property (readonly, copy) NSData * availableData; |
Return available data from the device or channel |
-(NSData *) readDataToEndOfFile; |
Read the remaining data until the end of the file |
-(NSData *) readDataOfLength :( NSUInteger) length; |
Read the specified bytes content from the file |
-(Void) writeData :( NSData *) data; |
Write data to a file |
@ Property (readonly) unsigned longlong offsetInFile; |
Obtains the offset of the current file. |
-(Void) seekToFileOffset :( unsignedlong long) offset; |
Set the offset of the current file |
-(Unsignedlong long) seekToEndOfFile; |
Position the offset of the current file to the end of the file. |
-(Void) truncateFileAtOffset :( unsignedlong long) offset; |
Set the object length to offset bytes. |
-(Void) closeFile; |
Close file |
The content is from objective-second edition of program design.