NSFileManager and NSFileHandle of IOS, nsfilehandle
The number of temporary cache files for mobile apps in the current stage is gradually increasing. In app development, there are more and more operations on mobile device files. In IOS, file operations mainly involve two types: NSFileManager and NSFileHandle, the following describes how to use these two classes:
1. File Creation
// Initialize an NSFileManager class defaultManager method to singleton mode. initialize NSFileManager * fileManager = [NSFileManager defaultManager] in singleton mode. // The splicing path NSString * path = NSHomeDirectory (); path = [path stringByAppendingPathComponent: @ "deskTop/date.txt"]; // create a file BOOL flag = [fileManager createFileAtPath: path contents: nil attributes: nil]; if (flag) {NSLog (@ "File Created successfully");} else {NSLog (@ "file creation failed ");}
2. Create a directory
NSFileManager * fileManager = [NSFileManager defaultManager]; NSString * path = NSHomeDirectory (); path = [path stringByAppendingPathComponent: @ "deskTop/pro/cpp"]; BOOL flag = [fileManager createDirectoryAtPath: path withIntermediateDirectories: YES attributes: nil error: nil]; if (flag) {NSLog (@ "created successfully");} else {NSLog (@ "failed to create ");}
3. delete files and directories
NSFileManager * fileManager = [NSFileManager defaultManager]; NSString * rootPath = NSHomeDirectory (); NSString * dirPath = [rootPath stringByAppendingPathComponent: @ "deskTop/newFolder"]; NSArray * array = [fileManager contentsOfDirectoryAtPath: dirPath error: nil]; for (NSString * str in array) {NSString * newPath = [dirPath stringByAppendingPathComponent: str]; BOOL flag = [fileManager removeItemAtPath: newPath error: nil]; if (flag) {NSLog (@ "deleted successfully ");} else {NSLog (@ "failed to delete ");}}
There are many methods for file operations. We can refer to the official API that we may use.
// Copy one file to another file [fileManager copyItemAtPath: path1 toPath: path2 error: nil]; // move one file to another file [fileManager moveItemAtPath: path1 toPath: path2 error: nil]; // obtain the content in the file NSData * readData = [fileManager contentsAtPath: path]
Case: here we can use an example to calculate the number of all rows in a file. The idea is: first, we need to read all the information in the file, to count the number of rows, we only need to count the number of line breaks in the file. The example is as follows:
NSFileManager * fileManager = [NSFileManager defaultManager]; NSString * rootPath = NSHomeDirectory (); NSString * dirPath = [rootPath stringByAppendingPathComponent: @ "deskTop/newFolder/main. m "]; NSData * data = [fileManager contentsAtPath: dirPath]; NSString * str = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; int count = (int) [str componentsSeparatedByString: @ "\ n"]. count; NSLog (@ "the current total number of rows is % d", count );
4. File writing
We use NSFileHandler most during file read/write operations. We use this class to perform file operations.
// Initialize a file handle NSFileHandle * fileHandle = [NSFileHandle fileHandleForWritingAtPath: filePath]; NSString * str = @ "pppppppppppp "; // move the file cursor to the last position of the file [fileHandle seekToEndOfFile]; NSData * data = [str dataUsingEncoding: NSUTF8StringEncoding]; // write data [fileHandle writeData: data]; // disable [fileHandle closeFile] after use;
5. File Reading
NSFileHandle * fileHandle = [NSFileHandle fileHandleForReadingAtPath: filePath]; // read NSData * data = [fileHandle readDataToEndOfFile] at the end of the file; // locate the cursor [fileHandle seekToFileOffset: 2]; // query the number of available data of the file [fileHandle availableData]; // read the NSData * data = [fileHandle readDataOfLength: 3] of the file of the specified length; NSString * str = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; NSLog (@ "% @", str); // close the handle [fileHandle closeFile];
Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
Copyright Disclaimer: The copyright of this article is shared by Yantai jereh Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the consent of the author and provide the original article connection clearly on the article page, otherwise, you are entitled to pursue legal liability.
Technical Consultation: