#import <Foundation/Foundation.h>
void test1(){
NSFileManager *manager=[NSFileManager defaultManager];
NSString *currentPath=[manager currentDirectoryPath];
NSLog(@"current path is :%@",currentPath);
NSString *myPath=@"/Users/kenshincui/Desktop/myDocument";
BOOL result= [manager createDirectoryAtPath:myPath withIntermediateDirectories:YES attributes:nil error:nil];
if(result==NO){
NSLog(@"Couldn't create directory!");
}
NSError *error;
NSString *newPath=@"/Users/kenshincui/Desktop/myNewDocument";
if([manager moveItemAtPath:myPath toPath:newPath error:&error]==NO){
NSLog(@"Rename directory failed!Error infomation is:%@",error);
}
if([manager changeCurrentDirectoryPath:newPath]==NO){
NSLog(@"Change current directory failed!");
}
NSLog(@"current path is :%@",[manager currentDirectoryPath]);
NSString *path;
NSDirectoryEnumerator *directoryEnumerator= [manager enumeratorAtPath:newPath];
while (path=[directoryEnumerator nextObject]) {
NSLog(@"%@",path);
}
NSArray *paths= [manager contentsOfDirectoryAtPath:newPath error:nil];
NSObject *p;
for (p in paths) {
NSLog(@"%@",p);
}
}
void test2(){
NSFileManager *manager=[NSFileManager defaultManager];
NSString *filePath=@"/Users/kenshincui/Desktop/myNewDocument/test.txt";
NSString *filePath2=@"/Users/kenshincui/Desktop/test.txt";
NSString *newPath=@"/Users/kenshincui/Desktop/myNewDocument/test2.txt";
if ([manager fileExistsAtPath:filePath isDirectory:NO]) {
NSLog(@"File exists!");
}
if([manager isReadableFileAtPath:filePath]){
NSLog(@"File is readable!");
}
if ([manager contentsEqualAtPath:filePath andPath:filePath2]) {
NSLog(@"file1 equals file2");
}
if (![manager moveItemAtPath:filePath toPath:newPath error:nil]) {
NSLog(@"Rename file1 failed!");
}
NSString *filePath3=@"/Users/kenshincui/Desktop/test3.txt";
if(![manager copyItemAtPath:newPath toPath:filePath3 error:nil]){
NSLog(@"Copy failed!");
}
NSDictionary *attributes;
if ((attributes=[manager attributesOfItemAtPath:newPath error:nil])==nil) {
NSLog(@"Read attributes failed!");
}
for (NSObject *key in attributes) {
NSLog(@"%@=%@",key,attributes[key]);
}
[manager removeItemAtPath:newPath error:nil];
}
void test3(){
NSFileManager *manager=[NSFileManager defaultManager];
NSString *filePath=@"/Users/kenshincui/Desktop/myNewDocument/test2.txt";
NSData *data=[manager contentsAtPath:filePath];
NSLog(@"%@",data);
NSString *str1=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",str1);
NSString *str2=@"Kenshin";
NSData *data2=[str2 dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@",data2);
NSString *content=[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",content);
}
void test4(){
NSFileManager *manager=[NSFileManager defaultManager];
NSString *filePath=@"/Users/kenshincui/Desktop/myNewDocument/test2.txt";
NSFileHandle *fileHandle=[NSFileHandle fileHandleForReadingAtPath:filePath];
NSData *data= [fileHandle readDataToEndOfFile];
NSString *newPath=@"/Users/kenshincui/Desktop/test4.txt";
[manager createFileAtPath:newPath contents:nil attributes:nil];
NSFileHandle *fileHandle2=[NSFileHandle fileHandleForWritingAtPath:newPath];
[fileHandle2 writeData:data];
[fileHandle2 closeFile];
[fileHandle seekToFileOffset:12];
NSData *data2= [fileHandle readDataToEndOfFile];
NSLog(@"data2=%@",[[NSString alloc]initWithData:data2 encoding:NSUTF8StringEncoding]);
[fileHandle seekToFileOffset:6];
NSData *data3=[fileHandle readDataOfLength:5];
NSLog(@"data3=%@",[[NSString alloc]initWithData:data3 encoding:NSUTF8StringEncoding]);
[fileHandle closeFile];
}
void test5(){
NSString *filePath=@"/Users/kenshincui/Desktop/myDocument";
NSString *filePath2=@"/Users/kenshincui/Desktop/test.txt";
NSString *path=NSTemporaryDirectory();
NSLog(@"temporary directory is :%@",path);
NSString *lastComponent= [filePath lastPathComponent];
NSLog(@"%@",lastComponent);
NSLog(@"%@",[filePath stringByDeletingLastPathComponent]);
NSLog(@"%@",[filePath stringByAppendingPathComponent:@"Pictrues"]);
NSLog(@"%@",[filePath2 pathExtension]);
[[filePath pathComponents] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"%i=%@",idx,obj);
}];
}
void test6(){
NSURL *url=[NSURL URLWithString:@"http://developer.apple.com"];
NSString *str1=[NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",str1);
}
void test7(){
NSFileManager *manager=[NSFileManager defaultManager];
NSString *currentPath=[manager currentDirectoryPath];
NSLog(@"current path is :%@",currentPath);
NSString *filePath=[currentPath stringByAppendingPathComponent:@"test.txt"];
[manager createFileAtPath:filePath contents:[@"Hello,world!" dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
NSBundle *bundle=[NSBundle mainBundle];
NSString *path=[bundle pathForResource:@"test" ofType:@"txt"];
NSLog(@"%@",path);
NSLog(@"%@",[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]);
NSString *path1= [bundle pathForResource:@"pic" ofType:@"jpg" inDirectory:@"Resources"];
NSLog(@"%@",path1);
}
int main(int argc,char *argv[]){
test1();
test2();
test3();
test4();
test5();
test6();
test7();
return 0;
}