1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 // Do any additional setup after loading the view. 4 self.view.backgroundColor = [UIColor whiteColor]; 5 6 7 //沙盒:系統爲每一個應用程序分配的一個文件夾,文件夾名字隨機,別人不能訪問 8 9 NSString *sandBoxPath = NSHomeDirectory() ; 10 NSLog(@"%@",sandBoxPath); 11 12 //沙盒文件下有3個文件夾,分別是 Documents Library tmp 13 14 /* 15 1.Documents 該文件下的內容會上傳到iCloud ,iCloud給每一個用戶分配空間5G,通常用來存儲一些用戶信息,遊戲進度信息 16 17 2.獲取Documents 文件夾路勁 18 19 */ 20 21 /** 22 * <#Description#> 23 * 24 * @param NSDocumentDirectory 搜索哪一個文件夾的路徑 25 * @param NSUserDomainMask 在那個文件系統中搜索 26 * @param YES 要獲取絕對路徑(YES)相對路徑(NO) 27 * 28 * @return 29 */ 30 31 NSArray *documentArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 32 33 // NSLog(@"%@",documentArray); 34 35 //數組就有一個元素 36 NSString *myDocPath = [documentArray firstObject]; 37 38 // NSLog(@"%@",myDocPath); 39 40 41 42 //Library 文件夾下有兩個子文件 Caches(緩存) Preferences 43 44 //Library 文件夾路徑 45 NSArray *libraryArray = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); 46 47 NSString *libraryPath = libraryArray[0]; 48 49 // NSLog(@"%@",libraryPath); 50 51 52 53 //獲取Caches文件夾的路徑 通常放一些緩存文件 , 網絡緩存等等 54 55 NSArray *cachesArray = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) ; 56 57 NSString *cacherPath = [cachesArray lastObject]; 58 59 // NSLog(@"%@",cacherPath); 60 61 62 //獲取Preferences文件夾路徑 63 64 // NSArray *preferencesArray = NSSearchPathForDirectoriesInDomains(NSPreferencePanesDirectory, NSUserDomainMask, YES) ; 65 // 66 // NSString *preferencesPath1 = [preferencesArray lastObject]; 67 68 NSString *preferencesPath = [sandBoxPath stringByAppendingString:@"/Library/Preferences"] ; 69 NSString *preferencesPath1 = [libraryPath stringByAppendingPathComponent:@"Preferences"] ; 70 71 // NSLog(@"%@",preferencesPath); 72 73 74 75 //獲取tem文件夾的路徑(臨時文件,好比下載完以後的壓縮包,解壓完以後就刪除) 76 77 //獲取tmp目錄 78 79 //獲取應用程序的tmp目錄要比獲取Documents目錄容易的多。使用函數NSTemporaryDirectory ()能夠得到tmp目錄路徑。 80 81 NSString* tempPath = NSTemporaryDirectory(); 82 83 //獲取文件的完整路徑。 84 85 NSString* tempFile = [tempPath stringByAppendingPathComponent:@"properties.plist"]; 86 87 NSLog(@"%@",tempPath); 88 // 89 90 //.app IOS8 以前存在沙盒文件裏面 91 //當前的應用程序的可執行文件,也就是NSBundle ,在程序編譯的過程當中,會把本身的全部的文件都打包進NSBundle中,這些文件都是隻讀的 92 93 //獲取.app文件的路徑 94 NSString *appPath = [[NSBundle mainBundle]resourcePath]; 95 // NSLog(@"*********%@",appPath); 96 97 //獲取.app某個文件的路徑 98 NSString *infoPlistPath = [[NSBundle mainBundle]pathForResource:@"Info" ofType:@"plist"]; 99 NSDictionary *dic = [[NSDictionary alloc]initWithContentsOfFile:infoPlistPath]; 100 101 NSLog(@"%@",dic); 102 103 104 105 //讀寫簡單文件 NSString NSDictionary NSArray NSData 能夠直接寫入本地 106 107 //將字符串寫入本地 108 /* 109 1.首先須要知道文件(字符串)寫進哪裏,也就是路徑 110 2.須要知道寫入的哪一個文件 111 3.字符串調用寫入的方法 112 */ 113 114 //文件寫入的路徑(後面拼接的是文件的名字),但此時該路徑下尚未文件 115 //後面寫的會覆蓋以前的 116 117 NSString *filePath = [myDocPath stringByAppendingPathComponent:@"文檔.txt"]; 118 119 // NSString *fiel = @"什麼鬼"; 120 121 NSString *fiel1 = @"啊"; 122 123 /* 124 fiel 寫入的文件 125 參數1:路徑 126 參數2:是否須要保護正在寫入的文件 127 參數3:編碼格式 128 參數4:錯誤信息 129 */ 130 131 NSError *error = nil ; 132 [fiel1 writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error]; 133 134 // NSLog(@"%@",filePath); 135 136 137 //用過路徑讀取文件 138 139 NSString *readFiel = [[NSString alloc]initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; 140 141 NSLog(@"%@",readFiel); 142 143 144 145 //讀寫數組 146 147 NSString *arrFielPath = [cacherPath stringByAppendingPathComponent:@"arr.plist"] ; 148 149 NSArray *arrFiel = @[@"123",@"111",@"5458444",@"9999",@"4444"]; 150 151 [arrFiel writeToFile:arrFielPath atomically:YES]; 152 153 // NSLog(@"%@",arrFielPath); 154 155 //本地讀取數組 156 NSArray *readarr = [[NSArray alloc]initWithContentsOfFile:arrFielPath]; 157 NSLog(@"%@",readarr); 158 159 160 //讀寫字典 161 NSString *dicFielPath = [preferencesPath1 stringByAppendingPathComponent:@"preferen.plist"]; 162 163 NSDictionary *dic1 = @{@"A2":@"21",@"B3":@"22",@"C4":@"23"}; 164 165 [dic1 writeToFile:dicFielPath atomically:YES]; 166 167 // NSLog(@"%@",dicFielPath); 168 169 NSDictionary *dic2 = [[NSDictionary alloc]initWithContentsOfFile:dicFielPath]; 170 NSLog(@"%@",dic2); 171 172 }