麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

oc文件基本讀寫及操作

2019-11-14 19:29:01
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

代碼:

#import <Foundation/Foundation.h>//NSString 寫文件void stringWriteToFile(){    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"/Documents/test.txt"];    NSString *s = @"test";    [s writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];            NSString *str = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];    NSLog(@"/nstring = %@",str);        NSString *testtxt = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];    NSLog(@"/ntest.text = %@",testtxt);}//NSArray 寫文件void arrayWriteToFile(){    NSArray *arr = @[@"a",@"b",@"b",@"c"];    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"/Documents/test2.txt"];    [arr writeToFile:path atomically:YES];            NSArray *a = [[NSArray alloc] initWithContentsOfFile:path];    NSLog(@"array = /n%@",a);        NSString *s = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];    NSLog(@"/ntest2.txt = /n%@",s);}//NSDictionary 寫文件void dictionaryWriteToFile(){    NSDictionary *dic = @{@"a":@"1",                          @"b":@"2",                          @"c":@"3",                          @"d":@"4"};    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"/Documents/test3.txt"];    [dic writeToFile:path atomically:YES];        NSDictionary *d = [[NSDictionary alloc] initWithContentsOfFile:path];    NSLog(@"/ndictionary = /n%@",d);        NSString *s = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];    NSLog(@"/ntest3.txt = /n%@",s);}//atomically 是否原子級(jí)  即事務(wù)性寫入//NSNumber、NSDate、NSData都可以通過writeToFile寫入文件,該文件為純文本類型,如果將后綴名改為plist即為xcode屬性列表文件int main(int argc, const char * argv[]) {        //該方法是創(chuàng)建一個(gè)實(shí)例,但是使的NSFileManager的單例模式將失去效果    NSFileManager *fm0 = [[NSFileManager alloc] init];        //defaultManager使用單例模式創(chuàng)建NSFileManager對(duì)象    NSFileManager *fm1 = [NSFileManager defaultManager];        NSFileManager *fm2 = [NSFileManager defaultManager];        NSLog(@"/nfm0 = %p,fm1 = %p,fm2 = %p",fm0,fm1,fm2);        NSData *data = [[NSString stringWithFormat:@"main"] dataUsingEncoding:NSUTF8StringEncoding];        NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/main.txt"];            if (![fm1 fileExistsAtPath:path]) {        if ([fm1 createFileAtPath:path contents: data attributes:nil]) {            NSLog(@"create success");            NSDictionary *d = [fm1 attributesOfItemAtPath:path error:nil];                        NSLog(@"/nattributesOfItemAtPath = /n%@",d);                        //NSFileSize是預(yù)定義的文件屬性key,可通過查看系統(tǒng)文件獲取其他屬性key并通過            //以下方法獲取其屬性值            NSNumber *filesize = [d valueForKey:NSFileSize];                        NSLog(@"/nfilesize = %@",filesize);                        //讀文件            NSData *data1 = [fm1 contentsAtPath:path];                        NSString *s = [[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding];                        //文件系統(tǒng)的屬性            //總空間,已用空間,可用空間,文件數(shù)量,            NSLog(@"/nattributesOfFileSystemForPath = /n%@",[fm1 attributesOfFileSystemForPath:path error:nil]);                        NSLog(@"/nmain.txt = %@",s);        }    }    else{                        NSString *copypath = [NSHomeDirectory() stringByAppendingPathComponent:@"/Documents/main_copy.txt"];        [fm1 copyItemAtPath:path toPath:copypath error:nil];                        //重命名可以目標(biāo)路徑與主路徑一致但是文件名不同        NSString *movepath = [NSHomeDirectory() stringByAppendingPathComponent:@"/Documents/main_move.txt"];        [fm1 moveItemAtPath:path toPath:movepath error:nil];                //刪除文件        if ([fm1 removeItemAtPath:path error:nil]) {            NSLog(@"remove %@ success",path);        }        if ([fm1 removeItemAtPath:copypath error:nil]) {            NSLog(@"remove %@ success",copypath);        }        if ([fm1 removeItemAtPath:movepath error:nil]) {            NSLog(@"remove %@ success",movepath);        }    }        stringWriteToFile();        arrayWriteToFile();        dictionaryWriteToFile();        return 0;}

結(jié)果:

2015-03-08 21:18:26.064 NSFileManagerDemo[1686:79942] fm0 = 0x1001145d0,fm1 = 0x100114620,fm2 = 0x1001146202015-03-08 21:18:26.075 NSFileManagerDemo[1686:79942] remove /Users/yoran_yang/Documents/main_copy.txt success2015-03-08 21:18:26.076 NSFileManagerDemo[1686:79942] remove /Users/yoran_yang/Documents/main_move.txt success2015-03-08 21:18:26.076 NSFileManagerDemo[1686:79942] string = test2015-03-08 21:18:26.077 NSFileManagerDemo[1686:79942] test.text = test2015-03-08 21:18:26.078 NSFileManagerDemo[1686:79942] array = (    a,    b,    b,    c)2015-03-08 21:18:26.078 NSFileManagerDemo[1686:79942] test2.txt = <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PRopertyList-1.0.dtd"><plist version="1.0"><array>    <string>a</string>    <string>b</string>    <string>b</string>    <string>c</string></array></plist>2015-03-08 21:18:26.079 NSFileManagerDemo[1686:79942] dictionary = {    a = 1;    b = 2;    c = 3;    d = 4;}2015-03-08 21:18:26.079 NSFileManagerDemo[1686:79942] test3.txt = <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict>    <key>a</key>    <string>1</string>    <key>b</key>    <string>2</string>    <key>c</key>    <string>3</string>    <key>d</key>    <string>4</string></dict></plist>Program ended with exit code: 0

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 黄色免费av| 成人精品久久久 | 色妇视频 | 久久成人视屏 | 91色成人| 一级国产电影 | 玖玖视频精品 | 国产91影院 | 久久艹一区 | 欧美一级毛片免费观看 | 欧美日韩专区国产精品 | 久久免费视频7 | 一级黄色国产视频 | 亚洲第一成人av | 神马久久精品综合 | 精品一区二区三区免费看 | 久久精品国产99国产精品亚洲 | 爽爽视频免费看 | 黄视频网址 | 国产成人精品免费视频大全最热 | 97中文 | 久久亚洲精品国产一区 | 欧美综合成人 | 激情综合婷婷久久 | 斗破苍穹在线观看免费完整观看 | 婷婷久久久久久 | 日本羞羞的午夜电视剧 | 欧美成人高清视频 | 久久久国产精品成人免费 | 日韩美女电影 | 国产精品1区| 欧美一级毛片一级毛片 | 伊人亚洲精品 | 亚欧美一区二区 | xxx18hd18hd日本 | 97中文| 视频一区二区三区在线播放 | 黄色av片三级三级三级免费看 | 免费毛片播放 | 日韩a毛片免费观看 | 天天黄色片 |