oc檔案基本讀寫及操作,oc檔案讀寫操作

來源:互聯網
上載者:User

oc檔案基本讀寫及操作,oc檔案讀寫操作

代碼:

#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 是否原子級  即事務性寫入//NSNumber、NSDate、NSData都可以通過writeToFile寫入檔案,該檔案為純文字類型,如果將尾碼名改為plist即為xcode屬性列表檔案int main(int argc, const char * argv[]) {        //該方法是建立一個執行個體,但是使的NSFileManager的單例模式將失去效果    NSFileManager *fm0 = [[NSFileManager alloc] init];        //defaultManager使用單例模式建立NSFileManager對象    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是預定義的檔案屬性key,可通過查看系統檔案擷取其他屬性key並通過            //以下方法擷取其屬性值            NSNumber *filesize = [d valueForKey:NSFileSize];                        NSLog(@"\nfilesize = %@",filesize);                        //讀檔案            NSData *data1 = [fm1 contentsAtPath:path];                        NSString *s = [[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding];                        //檔案系統的屬性            //總空間,已用空間,可用空間,檔案數量,            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];                        //重新命名可以目標路徑與主路徑一致但是檔案名稱不同        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;}

結果:

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.