AppDelegate.m
#import "AppDelegate.h"
@implementation AppDelegate
- (void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColorwhiteColor];
//单例获取工程的包目录
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *mainPath = [mainBundle bundlePath];
NSLog(@"mainPath = %@",mainPath);
//找到file的路径
NSString *path = [[NSBundle mainBundle]
pathForResource:@"File" ofType:nil];
NSError *error = nil;
NSString *contentsStr = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding error:&error];
if (error)
{
NSLog(@"file read fail:%@",[error localizedFailureReason]);
}
NSLog(@"contentsStr = %@",contentsStr);
//文件的写入
NSString *path2 = @"/Users/1005/Desktop/123/abc.txt";
BOOL success = [contentsStr writeToFile:path2
atomically:YES
encoding:NSUTF8StringEncoding
error:nil];
if (success)
{
NSLog(@"文件写入成功");
}
//NSData
NSData *data = [NSData dataWithContentsOfFile:path];
NSString *str = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(@"str = %@",str);
NSString *path3 = @"/Users/1005/Desktop/123/bcd.txt";
[data writeToFile:path3 atomically:YES];
//文件操作类-单利获取
NSFileManager *fm = [NSFileManagerdefaultManager];
NSString *path4 = @"/Users/1005/Desktop/123/efg.txt";
/*
if (![fm fileExistsAtPath:path4])
{
BOOL s =[fm createFileAtPath:path4
contents:data
attributes:nil];
if (s)
{
NSLog(@"文件创建成功!");
}
}
*/
//删除文件
if ([fm fileExistsAtPath:path4])
{
BOOL s2 = [fm removeItemAtPath:path4 error:nil];
if (s2)
{
NSLog(@"删除文件成功");
}
}
//创建目录
NSString *path5 = @"/Users/1005/Desktop/123/456";
BOOL s3 = [fm createDirectoryAtPath:path5
withIntermediateDirectories:YES
attributes:nil
error:nil];
if (s3)
{
NSLog(@"创建目录成功");
}
//移动目录
NSString *path6 = @"/Users/1005/Desktop/123/bcd.txt";
NSString *path7 = @"/Users/1005/Desktop/123/456/bcd.txt";
BOOL s4 = [fm moveItemAtPath:path6
toPath:path7
error:nil];
if (s4)
{
NSLog(@"移动成功");
}
[self.windowmakeKeyAndVisible];
returnYES;
}
AppDelegate.H
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end