ios 文件/目录操作

创建与删除:

//创建文件管理器

NSFileManager *fileManager = [NSFileManager defaultManager];

//获取路径

//参数NSDocumentDirectory要获取那种路径

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];//去处需要的路径

  

//更改到待操作的目录下

[fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];

//创建文件fileName文件名称,contents文件的内容,如果开始没有内容可以设置为nil,attributes文件的属性,初始为nil

[fileManager createFileAtPath:@"fileName" contents:nil attributes:nil];

//删除待删除的文件

[fileManager removeItemAtPath:@"createdNewFile" error:nil];

写入数据:

//获取文件路径

NSString *path = [documentsDirectory stringByAppendingPathComponent:@"fileName"];

  

//待写入的数据

NSString *temp = @"Hello friend";

int data0 = 100000;

float data1 = 23.45f;

  

//创建数据缓冲

NSMutableData *writer = [[NSMutableData alloc] init];

  

//将字符串添加到缓冲中

[writer appendData:[temp dataUsingEncoding:NSUTF8StringEncoding]];

//将其他数据添加到缓冲中

[writer appendBytes:&data0 length:sizeof(data0)];

[writer appendBytes:&data1 length:sizeof(data1)];

  

//将缓冲的数据写入到文件中

[writer writeToFile:path atomically:YES];

[writer release];

读取数据:

int gData0;

float gData1;

NSString *gData2;

  

NSData *reader = [NSData dataWithContentsOfFile:path];

gData2 = [[NSString alloc] initWithData:[reader subdataWithRange:NSMakeRange(0, [temp length])]

encoding:NSUTF8StringEncoding];

[reader getBytes:&gData0 range:NSMakeRange([temp length], sizeof(gData0))];

[reader getBytes:&gData2 range:NSMakeRange([temp length] + sizeof(gData0), sizeof(gData1))];

  

NSLog(@"gData0:%@ gData1:%i gData2:%f", gData0, gData1, gData2);

读取工程中的文件:

读取数据时,要看待读取的文件原有的文件格式,是字节码还是文本,我经常需要重文件中读取字节码,所以我写的是读取字节文件的方式。

//用于存放数据的变量,因为是字节,所以是UInt8

UInt8 b = 0;

//获取文件路径

NSString *path = [[NSBundle mainBundle] pathForResource:@"fileName" ofType:@""];

//获取数据

NSData *reader = [NSData dataWithContentsOfFile:path];

//获取字节的个数

int length = [reader length];

NSLog(@"------->bytesLength:%d", length);

for(int i = 0; i < length; i++)

{

//读取数据

[reader getBytes:&b range:NSMakeRange(i, sizeof(b))];

NSLog(@"-------->data%d:%d", i, b);


使用目录

目录方法:

- (NSString *)currentDirectoryPath
获取当前目录

- (BOOL)changeCurrentDirectoryPath:(NSString *)path
更改当前目录

- (BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error
复制目录或文件

- (BOOL)createDirectoryAtPath:(NSString *)path withIntermediateDirectories:(BOOL)createIntermediates attributes:(NSDictionary *)attributes error:(NSError **)error
创建一个新的目录

- (BOOL)fileExistsAtPath:(NSString *)path isDirectory:(BOOL *)isDirectory
测试文件是不是目录

- (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error
列目录(不会递归)
- (NSDirectoryEnumerator *)enumeratorAtPath:(NSString *)path
枚举目录内容(会递归)
- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error
删除一个文件,文件夹,链接

- (BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error
移动(重命名)目录到一个指定的路径

NSPathUtilities.h 包含了NSString的函数和分类扩展,用于操作路径名.
#import <Foundation/NSObject.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSString.h>
#import <Foundation/NSFileManager.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSPathUtilities.h>

int main (int argc, const char * argv[]) {

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSFileManager *fm;
//NSFileManager对象
NSString *dirName = @"testdir";
NSString *path;

fm = [[NSFileManager alloc]init];

path = [fm currentDirectoryPath];
NSLog(@"%@",path);
//获取当前目录路径.

if ([fm createDirectoryAtPath:dirName withIntermediateDirectories:YES attributes:nil error:NULL] == NO) {
NSLog(@"Couldn't create dir");
return 1;
}
//新建目录

if ([fm moveItemAtPath:dirName toPath"newdir" error:NULL] == NO) {
NSLog(@"Directory rename failed");
return 2;
}
//重命名目录

if ([fm changeCurrentDirectoryPath"newdir"] == NO) {
NSLog(@"change directory failed");
return 3;
}
//修改路径到新目录

path = [fm currentDirectoryPath];
//返回当前路径

NSLog(@"Current directory path is %@",path);
//打印

NSLog(@"successful");

[pool drain];
return 0;
}

枚举目录中的内容:

NSPathUtilities.h 包含了NSString的函数和分类扩展,用于操作路径名.
#import <Foundation/NSObject.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSString.h>
#import <Foundation/NSFileManager.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSPathUtilities.h>

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSFileManager *fm;
NSString *path;
NSDirectoryEnumerator *dirEnum;
NSArray *dirArray;

fm = [[NSFileManager alloc]init];
path = [fm currentDirectoryPath];
//当前目录
dirEnum = [fm enumeratorAtPath:path];
//开始枚举过程,将其存入dirEnum中.
NSLog(@"路径:%@",path);
//打印当前路径

while ((path = [ dirEnum nextObject]) != nil) {
NSLog(@"%@",path);
}
//向dirEnum发送nextObject消息,返回下一个文件路径,当没有可供枚举时,返回nil.
//enumeratorAtPath:方法会递归打印.

dirArray = [fm contentsOfDirectoryAtPath:[fm currentDirectoryPath] error:NULL];
NSLog(@"内容为:");
//使用contentsOfDirectoryAtPath:方法枚举当前路径中的文件并存入数组dirArray.

for (path in dirArray)
NSLog(@"%@",path);

//快速枚举数组中的内容并打印.

[pool drain];
return 0;
}

使用路径:
NSPathUtilities.h 包含了NSString的函数和分类扩展,用于操作路径名.
#import
#import
#import
#import
#import
#import

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSFileManager *fm;
NSString *fName = @"path.m";
NSString *upath = @"~s0s0/123/xxx/../321/./path.m";
NSString *path, *tempdir, *extension, *homedir, *fullpath;
NSArray *components;

fm = [[NSFileManager alloc]init];
tempdir = NSTemporaryDirectory();
//NSTemporaryDirectory()函数返回可以用来创建临时文件的目录路径名,如果要创建文件,完成任务后要删除;确保文件名是唯一的.

NSLog(@"临时文件夹路径为:%@",tempdir);

path = [fm currentDirectoryPath];
//返回当前目录路径

NSLog(@"父目录名: %@",[path lastPathComponent]);
//lastPathComponent方法用与提取路径中最后一个目录名.

fullpath = [path stringByAppendingPathComponent:fName];
//stringByAppendingPathComponent:方法将文件名插入到路径的末尾,这样就能显示一个文件的完整路径.
NSLog(@"完整路径为:%@ to %@",fName,fullpath);
//打印一个文件的完整路径.

extension = [fullpath pathExtension];
//pathExtension方法返回一个完整路径中的文件扩展名,如果没有扩展名,就返回空字符.
NSLog(@"extension for %@ is %@ ",fullpath, extension);

homedir = NSHomeDirectory();
//NSHomeDirectory()函数返回当前用户的主目录
//NSHomeDirectoryForUser(username)函数可以提供用户名做参数,并返回主目录名
NSLog(@"主目录为 : %@",homedir);

components = [homedir pathComponents];
//pathComponents方法返回一个数组,数组中包含一个路径中的每个组成部分.

for (path in components)
NSLog(@"%@",path);
//依次输出数组components中保存的元素.

NSLog(@"%@ -> %@",upath, [upath stringByStandardizingPath]);
//stringByStandardizingPath方法将原路径中的代字符转化为完整路径.
//如果是路径名字中出现代字符,可以使用stringByExpandingTildeInPath方法.
[pool drain];
return 0;

}




你可能感兴趣的:(ios 文件/目录操作)