【非凡程序員】 OC第十七節課 文件操做一 (NSFileManager和NSFileHandle)

文件操做atom

#import <Foundation/Foundation.h>spa

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSLog(@"Hello, World!");
       
        //----------- NSFileManager---------//
       
        //實例化一個對象
        NSFileManager * f=[NSFileManager defaultManager];
       
        //根據aaa.txt的路徑讀取內容
        NSData *data1=[f contentsAtPath:@"/Users/feifanchengxuyuan/Desktop/aaa.txt"];
        NSString *str1=[[NSString alloc]initWithData:data1 encoding:NSUTF8StringEncoding];
        NSLog(@"%@",str);
        
        //從一個文件中直接獲取數據(不須要轉換)
        NSLog(@"%@", [NSString stringWithContentsOfFile:@"/Users/feifanchengxuyuan/Desktop/aaa.txt" encoding:NSUTF8StringEncoding error:nil]);
code

        //建立一個b.txt  內容爲空
        //建立一個bbb.txt 而且把aaa.txt的文件複製到bbb.txt中
        [f createFileAtPath:@"/Users/feifanchengxuyuan/Desktop/b.txt" contents:nil attributes:nil];
        [f createFileAtPath:@"/Users/feifanchengxuyuan/Desktop/bbb.txt" contents:data1 attributes:nil];
        
        //把str轉化爲文件流,寫入到bbb.txt (若是是已經存在該文件,則會覆蓋以前的內容)
        NSString *str2=@"zhang";
        NSData *data2=[str2 dataUsingEncoding:NSUTF8StringEncoding];
        [f createFileAtPath:@"/Users/feifanchengxuyuan/Desktop/bbb.txt" contents:data2 attributes:nil];
   
        //獲取路徑
        NSString *str3= [f currentDirectoryPath];
        NSLog(@"%@",str3);
對象

          // 更改當前路徑
        [f changeCurrentDirectoryPath:@"/Users/feifanchengxuyuan/Desktop/"];
        NSLog(@"%@",[f 
currentDirectoryPath]);
       
        //路徑相同就是更名,路徑不一樣就是移動
        [f moveItemAtPath:@"/Users/feifanchengxuyuan/Desktop/b.txt"  toPath:@"/Users/feifanchengxuyuan/Desktop/ccc.txt"  error:nil];
        [f copyItemAtPath:@"/Users/feifanchengxuyuan/Desktop/b.txt" toPath:@"/Users/feifanchengxuyuan/Desktop/ccc" error:nil];

        //遍歷當前路徑
        NSArray *array=  [f contentsOfDirectoryAtPath:@"/Users/feifanchengxuyuan/Desktop" error:nil];
        NSLog(@"%@,array);
        
         // 枚舉遍歷
        NSLog(@"%@:[f enumeratorAtPath:@"/Users/feifanchengxuyuan/Desktop"]);
        NSEnumerator *ee=[f enumeratorAtPath:@"/Users/feifanchengxuyuan/Desktop"];
        NSArray *array1=[ee allObjects];
        NSLog(@"%@,array1);              
字符串

        //深度遍歷當前路徑
        NSArray * array1=[f subpathsAtPath:@"/Users/feifanchengxuyuan/Desktop"];
        NSLog(@"%@",array1);
string

        
        
       // 建立文件夾  withIntermediateDirectories:YES若是沒有該文件,就建立,     NO,若是不存就會報錯
        NSError *error = nil;
        [f createDirectoryAtPath:@"/Users/feifanchengxuyuan/Desktop3/aaa1" withIntermediateDirectories:YES attributes:nil error:&error];
        if( error )
        {
            NSLog( @"%@", error );
        }
       
   
  //--------------- NSFileHandle------(重點)-----//it

        //1 文件必須建立
      //2文件必須給出足夠的權限 如 chmod 777文件名+文件類型
       
        //1.獲取用戶主目錄
        NSString *homepath = NSHomeDirectory();
        NSLog( @"homepath: %@", homepath);
        //2.根據參數鏈接目錄
        NSString *sourcepath = [homepath stringByAppendingPathComponent:@"/Desktop/ssss.txt"];
        //只能寫@"/Desktop/ssss.txt" 不能寫@"/Users/feifanchengxuyuan/Desktop/ssss.txt"由於這是鏈接目錄,自動鏈接根目錄/Users/feifanchengxuyuan
io

(----------------注意--------------)
        NSLog( @"sourcepath: %@", sourcepath );
       
       //3.建立NSFileHandle對象類型fileHandleForUpdatingAtPath 爲可更新的類型
        //fileHandleForReadingAtPath //讀取
        NSFileHandle *filhandle = [NSFileHandle fileHandleForUpdatingAtPath:sourcepath];
       
        //4.定位偏移量到文件內容尾部
        [filhandle seekToEndOfFile];
       
        //5.定義要追加的字符串
        NSString *string = @"hello xiaohe   sacnkwanc;w\n";
       
       //6.轉化成文件流
        NSData *nsdate = [string dataUsingEncoding:NSUTF8StringEncoding];
       
        //7寫數據
        [filhandle writeData:nsdate];
import

        //8.關閉文件
        [filhandle closeFile];
date

        
           //將字典直接寫入到@"/Users/feifanchengxuyuan/Desktop/Friday.plist"路徑下,不須要轉換
        NSDictionary *dict=@{
                             @"1":@"a",
                             @"2":@"b",
                             @"3":@"c"
                             };
        [dict writeToFile:@"/Users/feifanchengxuyuan/Desktop/Friday.plist" atomically:YES];

 

          //echo jjjjjj>>aaa.txt  在終端中給aaa.txt添加jjjjjj文本
          cat 文件名是讀取內容
     

         chmod 777 文件名---改最大權限    chmod 000 文件名---最小權限
   

   }    return 0;}

相關文章
相關標籤/搜索