NSFileManager使用总结

//
//  AdressCard.h
//  Fmzongjie
//
//  Created by neworigin on 14-9-16.
//  Copyright (c) 2014年 Damon. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface AdressCard : NSObject<NSCoding>
{
    NSString *name;
    NSString *qqnumber;
}
@property NSString *name;
@property NSString *qqnumber;

@end


//
//  AdressCard.m
//  Fmzongjie
//
//  Created by neworigin on 14-9-16.
//  Copyright (c) 2014年 Damon. All rights reserved.
//

#import "AdressCard.h"

@implementation AdressCard
@synthesize name;
@synthesize qqnumber;
- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:name forKey:@"name"];
    [aCoder encodeObject:qqnumber forKey:@"qqnumber"];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self=[super init]) {
        self.name=[aDecoder decodeObjectForKey:@"name"];
        self.qqnumber=[aDecoder decodeObjectForKey:@"qqnumber"];
        return self;
    }
    return nil;
}
@end





//
//  main.m
//  Fmzongjie
//
//  Created by neworigin on 14-9-16.
//  Copyright (c) 2014年 Damon. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "AdressCard.h"

NSString *[email protected]"/Users/neworigin/Desktop/file1.txt";
NSString *[email protected]"/Users/neworigin/Desktop/file2.txt";
NSString *[email protected]"/Users/neworigin/Desktop/file3.txt";
NSString *[email protected]"/Users/neworigin/Desktop/file4.txt";
int main(int argc, const char * argv[])
{
    @autoreleasepool {
        AdressCard *a=[[AdressCard alloc]init];
        [email protected]"Damon";
        [email protected]"544712635";
        /*写入文件*/
        [NSKeyedArchiver archiveRootObject:a toFile:filename];
        /*读出文件*/
        AdressCard *b=[NSKeyedUnarchiver unarchiveObjectWithFile:filename];
        NSLog(@"1.读出文件filename: %@,%@",b.name,b.qqnumber);
        /**FM定义**/
        NSFileManager *fm=[NSFileManager defaultManager];
        /*创建字典显示文件信息*/
        NSDictionary *filedic=[fm attributesOfItemAtPath:filename error:nil];
        NSLog(@"2.创建字典显示文件信息: %@",filedic);
        /*利用字典单独显示相应值,例如filesize*/
        int filesize=[[filedic objectForKey:@"NSFileSize"]intValue];
        NSLog(@"3.filesize= %d",filesize);
        /*通过提取文件中的二进制数据获取正常信息*/
        NSData *fileinf=[fm contentsAtPath:filename];
        NSLog(@"4.二进制信息: %@",fileinf);//二进制信息
        AdressCard *c=[NSKeyedUnarchiver unarchiveObjectWithData:fileinf];
        NSLog(@"5.正常信息: %@,%@",c.name,c.qqnumber);
        /*创建文件*/
        BOOL succeed=NO;
        succeed=[fm createFileAtPath:file2name contents:fileinf attributes:nil];
        if (succeed) {
            NSLog(@"创建file2name成功");
        }
        else
            NSLog(@"创建file2name失败");
        /*比较两个文件是否相同*/
        BOOL equal=NO;
        equal=[fm contentsEqualAtPath:filename andPath:file2name];
        if (equal) {
            NSLog(@"文件相同");
        }
        else
            NSLog(@"文件不同");
        /*递归显示,包括子目录*/
        NSDirectoryEnumerator *enumer=[fm enumeratorAtPath:@"/Users/neworigin/Desktop/"];
        NSLog(@"6.enumer: %@",[enumer allObjects]);
        //获取当前目录路径
NSLog(@"%@",[fm currentDirectoryPath]);
//列举出指定目录下的内容(OS X 10.0以后使用,10.5 第一次被弃用 ——> - (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error)
   //NSArray * dirArray105 = [fm directoryContentsAtPath:tmpPath];
        NSLog(@"%@",[fm contentsOfDirectoryAtPath:@"/Users/neworigin/Desktop/" error:nil]);
//得到当前工程的临时目录
NSLog(@"%@", NSTemporaryDirectory());
//得到当前登录用户主目录(homeDirector)
NSString *home=NSHomeDirectory();
        NSLog(@"%@",home);
//得到当前指定用户主目录(HomeDirectoryForUser)
NSLog(@"%@",NSHomeDirectoryForUser(@"neworigin"));
//得到路径中最后一个组合目录
NSLog(@"%@",[home lastPathComponent]);
        NSLog(@"%@",[filename lastPathComponent]);
//在已存在的目录后面追加自定义目录(可以写'/',也可以不写'/')
NSString *st=[file3name stringByAppendingString:@"/oc/oc"];
        NSLog(@"%@",st);
//创建指定路径下面的目录(其中参数“withIntermediateDirectories:(BOOL)”,如果传递的是‘NO’->当前目录之前的路径如果无效,则返回NO,即指定路径下的目录创建失败;如果传递的是‘YES’->当前目录之前的路径如果无论有效或无效,则返回YES,即指定路径下的目录创建成功;)
        BOOL creat=NO;
creat=[fm createDirectoryAtPath:st withIntermediateDirectories:YES attributes:nil error:nil];
        if (creat) {
            NSLog(@"creat succeed");
        }
        else
            NSLog(@"creat failed");
//得到当前目录
NSLog(@"%@",[fm currentDirectoryPath]);

//得到路径中文件的后缀
        NSLog(@"%@",[filename pathExtension]);
        
    }
    return 0;
}


Last modification:January 1st, 1970 at 08:00 am
如果看了这个文章可以让你少加会班,可以请我喝杯可乐
已打赏名单
微信公众号

Leave a Comment