文件操作(复制、删除、修改)

来源:互联网 发布:淘宝春季女装新款 编辑:程序博客网 时间:2024/06/05 09:40

//写数据到文件
void writeData()
{
    NSString *path = @"/Users/administrator/Desktop/ycw.txt";
    
    NSString *temp = @"my name is yangchengwei";
    int i = 100;
    float f = 78.98;
    
    NSMutableData *write = [[NSMutableData alloc]init];
    
    [write appendData:[temp dataUsingEncoding:NSUTF8StringEncoding]];
    
    [write appendBytes:&i length:sizeof(i)];
    [write appendBytes:&f length:sizeof(f)];
    
    [write writeToFile:path atomically:YES];
    
    
}


//从文件读取数据到NSData

void readData()
{
   NSString *path = @"/Users/administrator/Desktop/ycw.txt";
    
    NSString *temp;
    
    int i;
    float f;
    
    NSData *read = [NSData dataWithContentsOfFile:path];
    
    temp = [[NSString alloc]initWithData:[read subdataWithRange:NSMakeRange(0, 23)] encoding:NSUTF8StringEncoding];
    
    [read getBytes:&i range:NSMakeRange([temp length], sizeof(i))];
    
    [read getBytes:&f range:NSMakeRange([temp length]+sizeof(i), sizeof(f))];
    
    NSLog(@"string is %@ int is %d float is %f",temp,i,f);
    
    

}


void readFile(){

    NSFileManager *manager = [NSFileManager defaultManager];
    NSString *path = @"/Users/administrator/Desktop/1.txt";
    if ([manager fileExistsAtPath:path]) {
        NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
        NSLog(@"%@",content);
    }
}
void fileBasic(){
    NSFileManager *manager;
    //获取manager对象,单例模式,Returns the default singleton instance.
    manager = [NSFileManager defaultManager];
    NSString *path = @"/Users/administrator/Desktop/1.txt";
    NSString *path2 = @"/Users/administrator/Desktop/2.txt";
    if ([manager fileExistsAtPath:path]) {
        NSLog(@"已存在");
        NSError *error;
        [manager copyItemAtPath:path toPath:path2 error:&error];
    }
    else{
        NSString *content = @"苹果iOS是由苹果公司开发的手持设备操作系统。苹果公司最早于2007年1月9日的Macworld大会上公布这个系统,最初是设计给iPhone使用的,后来陆续套用到iPod touch、iPad以及Apple TV等苹果产品上。iOS与苹果的Mac OS X操作系统一样,它也是以Darwin为基础的,因此同样属于类Unix的商业操作系统。原本这个系统名为iPhone OS,直到2010年6月7日WWDC大会上宣布改名为iOS。截止至2011年11月,根据Canalys的数据显示,iOS已经占据了全球智能手机系统市场份额的30%,在美国的市场占有率为43%.";
        NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];
        BOOL result =  [manager createFileAtPath:path contents:data attributes:nil];
        if (result) {
            NSError *error;
            [manager copyItemAtPath:path toPath:path2 error:&error];
            
        }
    }
    
}
void fileOperator()
{
    NSFileManager *manager = [NSFileManager defaultManager];
    NSString *f1 = @"/Users/administrator/Desktop/1.txt";
    NSString *f2 = @"/Users/administrator/Desktop/2.txt";
    
    NSString *content = @"苹果iOS是由苹果公司开发的手持设备操作系统。苹果公司最早于2007年1月9日的Macworld大会上公布这个系统,最初是设计给iPhone使用的,后来陆续套用到iPod touch、iPad以及Apple TV等苹果产品上。iOS与苹果的Mac OS X操作系统一样,它也是以Darwin为基础的,因此同样属于类Unix的商业操作系统。原本这个系统名为iPhone OS,直到2010年6月7日WWDC大会上宣布改名为iOS。截止至2011年11月,根据Canalys的数据显示,iOS已经占据了全球智能手机系统市场份额的30%,在美国的市场占有率为43%.";
    
    //字符串转换成NSData
    NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];
    
    Boolean status = NO;
    
    //判断f1是否存在
    if ([manager fileExistsAtPath:f1]) {
        [manager removeItemAtPath:f1 error:nil];
    }
    
    //创建文件,并添加内容
    status = [manager createFileAtPath:f1 contents:data attributes:nil];
    if (status) {
        NSLog(@"%@",[NSString stringWithContentsOfFile:f1 encoding:NSUTF8StringEncoding error:nil]);
    }
    if (![manager fileExistsAtPath:f2]) {
        [manager copyItemAtPath:f1 toPath:f2 error:nil];
    }
}

void directoryOperator()
{
    NSString *dirName = @"testDir";
    NSString *path;
    NSFileManager *fm;
    
    fm = [NSFileManager defaultManager];
    
    //获得当前目录
    path = [fm currentDirectoryPath];
    NSLog(@"currentDirectoryPath:%@",path);
    
    //创建新的目录
    if ([fm createDirectoryAtPath:dirName withIntermediateDirectories:YES attributes:nil error:nil]) {
        NSLog(@"create directory succeed!");
    }
    
    //重命名新的目录
    if ([fm moveItemAtPath:dirName toPath:@"newDir" error:nil]) {
        NSLog(@"move directory succeed!");
    }
    
    if ([fm changeCurrentDirectoryPath:@"newDir"]) {
        NSLog(@"change currentDirectory succeed!");
    }
    
    //重新获得当前目录
    path = [fm currentDirectoryPath];
    NSLog(@"currentDirectoryPath:%@",path);
}

void enumDictory1()
{
    //深度遍历
    NSFileManager *manager = [NSFileManager defaultManager];
    
    NSDirectoryEnumerator *dir = [manager enumeratorAtPath:@"/users/fmning/desktop/"];
    
    NSString *path = [dir nextObject];
    while (path != nil) {
        NSLog(@"%@",path);
        path = [dir nextObject];
    }
}

void enumDictory2()
{
    //浅度遍历
    NSFileManager *manager = [NSFileManager defaultManager];
    
    NSArray *array = [manager contentsOfDirectoryAtPath:@"/users/fmning/desktop" error:nil];
    
    for (NSString *str in array) {
        NSLog(@"%@",str);
    }
}

void fileHandle()
{
    NSFileHandle *fh;
    NSString *path = @"/users/fmning/desktop/1.txt";
    fh = [NSFileHandle fileHandleForUpdatingAtPath:path];
    if (fh != nil) {
        [fh seekToEndOfFile];
        
        NSString *appendStr = @"我是追加的一段文字@";
        [fh writeData:[appendStr dataUsingEncoding:NSUTF8StringEncoding]];
        
        [fh closeFile];
    }
    
}


//把infile文件中的内容拷贝到outfile中
void fileHandle2()
{
    NSFileHandle *infile,*outfile;
    NSData *buffer;
    
    infile = [NSFileHandle fileHandleForReadingAtPath:@"/users/fmning/desktop/1.txt"];
    outfile = [NSFileHandle fileHandleForWritingAtPath:@"/users/fmning/desktop/2.txt"];
    
    if (!infile || !outfile) {
        return;
    }
    
    //可删除outfile以前的内容
    [outfile truncateFileAtOffset:0];
    
    //读取infile文件内容到缓冲区
    buffer = [infile readDataToEndOfFile];
    
    //将缓冲区中的数据写入到outfile中
    [outfile writeData:buffer];
    
    //关闭两个文件
    [infile closeFile];
    [outfile closeFile];
}

void nsURLTest()
{
    NSURL *myurl = [NSURL URLWithString:@"http://www.baidu.com"];
    NSString *page = [NSString stringWithContentsOfURL:myurl encoding:NSASCIIStringEncoding error:nil];
    NSLog(@"%@",page);
}

void bundleTest()
{
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"person" ofType:@"plist"];
    
}

0 0
原创粉丝点击