Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // KHTNViewController.m
- // NSFileManager Sample
- //
- // Created by Dino Phan on 6/18/14.
- // Copyright (c) 2014 Dino Phan. All rights reserved.
- //
- #import "KHTNViewController.h"
- @interface KHTNViewController ()
- @property (strong, nonatomic) NSFileManager *fileManager;
- @property (strong, nonatomic) NSString *stringPath;
- @property (strong, nonatomic) NSString *contentOfFile;
- @end
- @implementation KHTNViewController
- @synthesize fileManager, stringPath, contentOfFile;
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- fileManager = [NSFileManager defaultManager];
- stringPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
- contentOfFile = @"Hello World!";
- NSString *pathOfFile = [stringPath stringByAppendingPathComponent:@"data.txt"];
- NSError *error = [[NSError alloc] init];
- // Create a file
- if ([contentOfFile writeToFile:pathOfFile atomically:YES encoding:NSUTF8StringEncoding error:&error]) {
- NSLog(@"'%@' has successfully wrote!", pathOfFile);
- } else {
- NSLog(@"Error: %@", [error debugDescription]);
- }
- // Read content in file
- NSString *getContent = [[NSString alloc] initWithContentsOfFile:pathOfFile encoding:NSUTF8StringEncoding error:nil];
- NSLog(@"%@", getContent);
- // Remove a file from Document dir
- if ([fileManager removeItemAtPath:pathOfFile error:&error]) {
- NSLog(@"'%@' removed!", pathOfFile);
- } else {
- NSLog(@"Error: %@", [error debugDescription]);
- }
- // Get boolean if file exist in documents folder
- if ([fileManager fileExistsAtPath:pathOfFile]) {
- NSLog(@"File exist!");
- } else {
- NSLog(@"File isn't exist!");
- }
- // List files in root
- NSArray *fileListAtRoot = [fileManager contentsOfDirectoryAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@""] error:nil];
- NSString *fileNameInList = [[NSString alloc] init];
- for (fileNameInList in fileListAtRoot) {
- NSLog(@"%@", fileNameInList);
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement