File Creation data write read Delete write read
// Create a file path(There are three sandbox paths available. Please select one as needed)
NSArray * paths = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES );
NSString * cachPath = [[pathsobjectAtIndex: 0] stringByAppendingPathComponent: @ "HYC.txt"];
NSFileManager * fileManager = [NSFileManagerdefaultManager];
// Create a file
BOOL result = [fileManager createFileAtPath: cachPath contents: nilattributes: nil];
// CreateFileAtPath: contents: attributes: the first parameter is used to upload the full path of the file to be created. The second parameter is used to upload the file content. The file content is generally empty. The third parameter is used to upload the file property/the default permission is nil.
// If the returned value is YES, it indicates that the file is successfully created. If the returned value is NO, it indicates that the file fails to be created.
// Note: if the file you created already exists at the specified location, the file with the same name will be overwritten.
If (result)
{
NSLog (@ "created successfully ");
}
Else
{
NSLog (@ "creation failed ");
}
// Write data
NSArray * thePaths = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES );
NSString * theCachPath = [[thePathsobjectAtIndex: 0] stringByAppendingPathComponent: @ "HYC.txt"];
NSFileHandle * handle = [NSFileHandlefileHandleForUpdatingAtPath: theCachPath];
[Handle seekToEndOfFile]; // causes the file offset to be at the end
NSString * str = @ "androidfsdgfhgcjhvbknlgdfxhgcjhvkjbknsgdfxhgcjhvjbknszdgfxhc ";
NSData * data = [strdataUsingEncoding: NSUTF8StringEncoding];
// Write NSData to a file
[HandlewriteData: data];
[HandlecloseFile];
// Read data
NSFileHandle * handleRead = [NSFileHandlefileHandleForReadingAtPath: theCachPath];
[HandleReadseekToFileOffset: 0];
NSData * readdata = [handleRead readDataToEndOfFile];
NSString * strRead = [[NSStringalloc] initWithData: readdataencoding: NSUTF8StringEncoding];
NSLog (@ "1 ~~ % @ ", StrRead );
// Delete operation
NSFileManager * fileMgr = [NSFileManagerdefaultManager];
If ([fileMgrfileExistsAtPath: theCachPath]) {
[FileMgr removeItemAtPath: theCachPath error: nil];
}