- (
IBAction
)sandBox:(
id
)sender {
// 沙盒 : 指的是苹果系统为应用程序创建的唯一的文件夹。
// 注意事项 :1. 沙盒的名字是唯一的也是随机的。由系统提供。
// 2. 每个应用程序 , 只能访问自己沙盒内部的文件。
// 沙盒文件夹下面对应三个文件夹 :
// Documents
// Library 文件夹下面包含两个文件夹 :
// Caches
// Preferences
// tmp
NSString *sendBox = NSHomeDirectory ();
NSLog ( @"sendBox:%@" , sendBox);
#warning 1. 通过字符串拼接查找文件路径
// 获取 Documents 文件路径
NSString *documentsPath = [sendBox stringByAppendingString : @"/Documents" ];
NSLog ( @"documents:%@" , documentsPath);
// 获取 tmp 文件路径
NSString *tmpPath = [sendBox stringByAppendingPathComponent : @"tmp" ];
NSLog ( @"tmp:%@" , tmpPath);
// 获取 Caches 文件夹 , 同时在文件夹下创建一个 a.txt 文件。 ( 为实现 )
NSString *CachesPath = [sendBox stringByAppendingString : @"/Library/Caches" ];
NSLog ( @"Caches:%@" , CachesPath);
NSString *aPath = [CachesPath stringByAppendingString : @"/a.txt" ];
NSLog ( @"aPath:%@" , aPath);
}
- ( IBAction )documentsPath:( id )sender {
#warning 2. 通过 NSSearchPathForDirectoriesInDomains 函数获取文件路径
// 函数参数含义如下 :
// 1. 所查找路径 ( 如果查找的 documents 路径 , 一定要给 NSDocumentDirectory)
// 2. 在哪个目录下查找 ( 直接给 NSUserDomainMask 即可 )
// 3. 路径是否完整
// 函数返回值为数组 , 一般情况下数组中只有一个元素。
NSArray *arr = NSSearchPathForDirectoriesInDomains ( NSDocumentDirectory , NSUserDomainMask , YES );
NSLog ( @"arr:%@" , arr);
// NSString *documentPahth = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// NSLog(@"document:%@", documentPahth);
}
- ( IBAction )libraryPath:( id )sender {
NSString *str = [ NSSearchPathForDirectoriesInDomains ( NSLibraryDirectory , NSUserDomainMask , YES ) lastObject ];
NSLog ( @"library:%@" , str);
}
- ( IBAction )tmpPath:( id )sender {
NSString *tmpPath = NSTemporaryDirectory ();
NSLog ( @"tmp:%@" ,tmpPath);
}
- ( IBAction )bundle:( id )sender {
// 应用程序包 : 其实就是应用程序本身 (.app) 。应用程序包不允许修改 , 只能获取。
NSString *bundlePath = [[ NSBundle mainBundle ] resourcePath ];
NSLog ( @"bundle:%@" , bundlePath); // 需要上传的东西
}
- ( IBAction )writeToFile:( id )sender {
// 将输入框里用户输入的内容写入到 Documents 文件夹下的 a.txt 文件里
// 1. 获取 Documents 文件夹路径
NSString *documents = [ NSSearchPathForDirectoriesInDomains ( NSDocumentDirectory , NSUserDomainMask , YES ) lastObject ];
NSLog ( @"documents:%@" , documents);
// 2. 获取 a.txt 文件路径
NSString *aPath = [documents stringByAppendingPathComponent : @"a.txt" ];
// 3. 写入文件
NSError *error = nil ;
BOOL isOK = [ self . myTF . text writeToFile :aPath atomically : YES encoding : NSUTF8StringEncoding error :&error];
if (isOK) {
NSLog ( @"aPath:%@" , aPath);
} else {
NSLog ( @" 文件写入失败 " );
}
// 将如下数组写入到 tmp 文件夹下的 b.plist 文件中。 ( 练习一下字典 )
// NSArray *arr = @[@" 小傻子 ", @" 男 ", @" 爱好女 "];
// NSString *tmp = NSTemporaryDirectory();
// NSString *bPath = [tmp stringByAppendingPathComponent:@"b.plist"];
// BOOL isOK = [arr writeToFile:bPath atomically:YES];
// if (isOK) {
// NSLog(@"bPath:%@", bPath);
// } else {
// NSLog(@" 文件写入失败 ");
// }
}
- ( IBAction )readFromFile:( id )sender {
// 1. 获取对应文件的路径
// NSString *bPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"b.plist"];
// // 2. 根据数据类型采用相应的方法获取数据
// NSArray *arr = [NSArray arrayWithContentsOfFile:bPath];
// NSLog(@"arr:%@", arr);
// 读取 a.txt 文件内容 , 同时显示在 myTF 上。
NSString *document = [ NSSearchPathForDirectoriesInDomains ( NSDocumentDirectory , NSUserDomainMask , YES ) lastObject ];
NSString *aPath = [document stringByAppendingPathComponent : @"a.txt" ];
NSError *error = nil ;
NSString *str = [ NSString stringWithContentsOfFile :aPath encoding : NSUTF8StringEncoding error :&error];
self . myTF . text = str;
// 沙盒 : 指的是苹果系统为应用程序创建的唯一的文件夹。
// 注意事项 :1. 沙盒的名字是唯一的也是随机的。由系统提供。
// 2. 每个应用程序 , 只能访问自己沙盒内部的文件。
// 沙盒文件夹下面对应三个文件夹 :
// Documents
// Library 文件夹下面包含两个文件夹 :
// Caches
// Preferences
// tmp
NSString *sendBox = NSHomeDirectory ();
NSLog ( @"sendBox:%@" , sendBox);
#warning 1. 通过字符串拼接查找文件路径
// 获取 Documents 文件路径
NSString *documentsPath = [sendBox stringByAppendingString : @"/Documents" ];
NSLog ( @"documents:%@" , documentsPath);
// 获取 tmp 文件路径
NSString *tmpPath = [sendBox stringByAppendingPathComponent : @"tmp" ];
NSLog ( @"tmp:%@" , tmpPath);
// 获取 Caches 文件夹 , 同时在文件夹下创建一个 a.txt 文件。 ( 为实现 )
NSString *CachesPath = [sendBox stringByAppendingString : @"/Library/Caches" ];
NSLog ( @"Caches:%@" , CachesPath);
NSString *aPath = [CachesPath stringByAppendingString : @"/a.txt" ];
NSLog ( @"aPath:%@" , aPath);
}
- ( IBAction )documentsPath:( id )sender {
#warning 2. 通过 NSSearchPathForDirectoriesInDomains 函数获取文件路径
// 函数参数含义如下 :
// 1. 所查找路径 ( 如果查找的 documents 路径 , 一定要给 NSDocumentDirectory)
// 2. 在哪个目录下查找 ( 直接给 NSUserDomainMask 即可 )
// 3. 路径是否完整
// 函数返回值为数组 , 一般情况下数组中只有一个元素。
NSArray *arr = NSSearchPathForDirectoriesInDomains ( NSDocumentDirectory , NSUserDomainMask , YES );
NSLog ( @"arr:%@" , arr);
// NSString *documentPahth = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// NSLog(@"document:%@", documentPahth);
}
- ( IBAction )libraryPath:( id )sender {
NSString *str = [ NSSearchPathForDirectoriesInDomains ( NSLibraryDirectory , NSUserDomainMask , YES ) lastObject ];
NSLog ( @"library:%@" , str);
}
- ( IBAction )tmpPath:( id )sender {
NSString *tmpPath = NSTemporaryDirectory ();
NSLog ( @"tmp:%@" ,tmpPath);
}
- ( IBAction )bundle:( id )sender {
// 应用程序包 : 其实就是应用程序本身 (.app) 。应用程序包不允许修改 , 只能获取。
NSString *bundlePath = [[ NSBundle mainBundle ] resourcePath ];
NSLog ( @"bundle:%@" , bundlePath); // 需要上传的东西
}
- ( IBAction )writeToFile:( id )sender {
// 将输入框里用户输入的内容写入到 Documents 文件夹下的 a.txt 文件里
// 1. 获取 Documents 文件夹路径
NSString *documents = [ NSSearchPathForDirectoriesInDomains ( NSDocumentDirectory , NSUserDomainMask , YES ) lastObject ];
NSLog ( @"documents:%@" , documents);
// 2. 获取 a.txt 文件路径
NSString *aPath = [documents stringByAppendingPathComponent : @"a.txt" ];
// 3. 写入文件
NSError *error = nil ;
BOOL isOK = [ self . myTF . text writeToFile :aPath atomically : YES encoding : NSUTF8StringEncoding error :&error];
if (isOK) {
NSLog ( @"aPath:%@" , aPath);
} else {
NSLog ( @" 文件写入失败 " );
}
// 将如下数组写入到 tmp 文件夹下的 b.plist 文件中。 ( 练习一下字典 )
// NSArray *arr = @[@" 小傻子 ", @" 男 ", @" 爱好女 "];
// NSString *tmp = NSTemporaryDirectory();
// NSString *bPath = [tmp stringByAppendingPathComponent:@"b.plist"];
// BOOL isOK = [arr writeToFile:bPath atomically:YES];
// if (isOK) {
// NSLog(@"bPath:%@", bPath);
// } else {
// NSLog(@" 文件写入失败 ");
// }
}
- ( IBAction )readFromFile:( id )sender {
// 1. 获取对应文件的路径
// NSString *bPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"b.plist"];
// // 2. 根据数据类型采用相应的方法获取数据
// NSArray *arr = [NSArray arrayWithContentsOfFile:bPath];
// NSLog(@"arr:%@", arr);
// 读取 a.txt 文件内容 , 同时显示在 myTF 上。
NSString *document = [ NSSearchPathForDirectoriesInDomains ( NSDocumentDirectory , NSUserDomainMask , YES ) lastObject ];
NSString *aPath = [document stringByAppendingPathComponent : @"a.txt" ];
NSError *error = nil ;
NSString *str = [ NSString stringWithContentsOfFile :aPath encoding : NSUTF8StringEncoding error :&error];
self . myTF . text = str;
}