本文来讲述 文件类的各种操作方法,示例如下:
view plaincopy to clipboardprint?
NSString
NSFileManager *fm=NULL;
NSDictionary
fm=[NSFileManager defaultManager];
if ([fm fileExistsAtPath:fName]==NO) {
}
}
if ([fm movePath:@"newfile" toPath:@"newfolder" handler:nil]==NO) {
}
if ((dict=[fm fileAttributesAtPath:@"newfolder" traverseLink:NO])==nil) {
}
NSLog(@"file size is %i bytes",[[dict objectForKey:NSFileSize] intValue]);
if ([fm removeFileAtPath:@"newfolder" handler:nil]==YES) {
}
NSData类的使用方法
view plaincopy to clipboardprint?
NSData
fm=[NSFileManager defaultManager];
fileData=[fm contentsAtPath:@"testfile.m"];
if (fileData==nil) {
}
if ([fm createFileAtPath:@"file2" contents:fileData attributes:nil]==YES) {
}
对路径的操作方法:
view plaincopy to clipboardprint?
NSString
NSFileManager *fm=NULL;
//NSDictionary
NSString
fm=[NSFileManager defaultManager];
path=[fm currentDirectoryPath];
NSLog(@"currentDir is: %@",path);
//create a new directory
[fm createDirectoryAtPath:fName attributes:nil];
//rename the directory
[fm movePath:fName toPath:@"newDir" handler:nil];
//change currentdirectory
[fm changeCurrentDirectoryPa
path=[fm currentDirectoryPath];
NSLog(@"currentDir is: %@",path);
枚举某个目录的所有内容:
view plaincopy to clipboardprint?
NSFileManager
NSDirectoryEnumerator *dirEnum=NULL;
NSArray
NSString
fm=[NSFileManager
path=[fm currentDirectoryPath];
dirEnum=[fm enumeratorAtPath:path];
while ((path=[dirEnum nextObject])!=nil) {
}
NSLog(@"%@",dirArray);
NSFileHandle的使用示例:
view plaincopy to clipboardprint?
NSFileHandle
NSData
inFile=[NSFileHandle fileHandleForReadingAtPa
[[NSFileManager defaultManager]createFileAtPath:@"newfile1" contents:nil attributes:nil];
outFile=[NSFileHandle fileHandleForWritingAtPa
[outFile truncateFileAtOffset:0];
buffer=[inFile readDataToEndOfFile];
[outFile writeData:buffer];
[inFile closeFile];
[outFile closeFile];
本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/newjerryj/archive/2011/03/20/6263134.aspx