1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
| #import <Foundation/Foundation.h>
void test1(){ NSFileManager *manager=[NSFileManager defaultManager]; NSString *currentPath=[manager currentDirectoryPath]; NSLog(@"current path is :%@",currentPath); NSString *myPath=@"/Users/jason/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/jason/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); } documents est.txt */ NSArray *paths= [manager contentsOfDirectoryAtPath:newPath error:nil]; NSObject *p; for (p in paths) { NSLog(@"%@",p); } documents est.txt */ }
void test2(){ NSFileManager *manager=[NSFileManager defaultManager]; NSString *filePath=@"/Users/jason/Desktop/myNewDocument/test.txt"; NSString *filePath2=@"/Users/jason/Desktop/test.txt"; NSString *newPath=@"/Users/jason/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/jason/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]); } NSFileOwnerAccountID=501 NSFileHFSTypeCode=0 NSFileSystemFileNumber=1781953 NSFileExtensionHidden=0 NSFileSystemNumber=16777218 NSFileSize=27 NSFileGroupOwnerAccountID=20 NSFileOwnerAccountName=jason NSFileCreationDate=2014-07-28 11:47:58 +0000 NSFilePosixPermissions=420 NSFileHFSCreatorCode=0 NSFileType=NSFileTypeRegular NSFileExtendedAttributes={ "com.apple.TextEncoding" = <7574662d 383b3133 34323137 393834>; } NSFileGroupOwnerAccountName=staff NSFileReferenceCount=1 NSFileModificationDate=2014-07-28 11:47:58 +0000 */ [manager removeItemAtPath:newPath error:nil]; }
void test3(){ NSFileManager *manager=[NSFileManager defaultManager]; NSString *filePath=@"/Users/jason/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/jason/Desktop/myNewDocument/test2.txt"; NSFileHandle *fileHandle=[NSFileHandle fileHandleForReadingAtPath:filePath]; NSData *data= [fileHandle readDataToEndOfFile]; NSString *newPath=@"/Users/jason/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/jason/Desktop/myDocument"; NSString *filePath2=@"/Users/jason/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); }]; 0=/ 1=Users 2=jason 3=Desktop 4=myDocument */ }
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; }
|