IPhone DevelopmentMediumFileThe read/write tutorial is the content to be introduced in this article, mainly to learnIphone DevelopmentAboutFileFor more information, see this article. For an app running on the iPhone, it can only access some of its root directories.FileThe so-called sandbox )..
After an app is published to the iPhone, its directory structure is as follows:
1. The app root can be accessed using NSHomeDirectory;
2. The Documents directory is the place where we can write and save the file:
- de style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left:
- 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">
- NSArray *paths = NSSearchPa
- thForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
-
- de>
- de style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px;
- margin-top: 0px; margin-right: 0px; margin-bottom:
- 0px; margin-left: 0px; ">NSString *documentsDirectory = [paths objectAtIndex:0];
- de>
.
3. In the tmp directory, we can write data that is needed when the program is running. The data written in the tmp directory will not exist after the program exits. You can use
- NSString *NSTemporaryDirectory(void);
Method;
4. file operations can be performed through NSFileManage, And the instance can be obtained through [NSFileManger defaultManger.
Related Operations:
Create a directory: for example, to create a test directory under Documents,
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentsDirectory = [paths objectAtIndex:0];
- NSLog(@”%@”,documentsDirectory);
- NSFileManager *fileManage = [NSFileManager defaultManager];
- NSString *myDirectory = [documentsDirectory stringByAppendingPathComponent:@“test”];
- BOOL ok = [fileManage createDirectoryAtPath:myDirectory attributes:nil];
Obtain all file names in a directory: The above myDirectory) is available.
- NSArray *file = [fileManager subpathsOfDirectoryAtPath: myDirectory error:nil];
Or
- NSArray *files = [fileManager subpathsAtPath: myDirectory ];
Read an object:
- NSData *data = [fileManger contentsAtPath:myFilePath];//myFilePath
Is the name of the file containing the complete path
Or directly use the NSData class method:
- NSData *data = [NSData dataWithContentOfPath:myFilePath];
Save an object:
You can use NSFileManager's
- - (BOOL)createFileAtPath:(NSString *)path contents:(NSData *)data attributes:(NSDictionary *)attr;
Or NSData
- - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;
- - (BOOL)writeToFile:(NSString *)path options:(NSUInteger)writeOptionsMask error:(NSError **)errorPtr;
Summary: DetailsIPhone DevelopmentMediumFileThe content of the read/write tutorial is complete. I hope this article will help you!