绵阳建设网站,家居企业网站建设平台,企业建立网站的好处,吉林网站seo1、NSData和NSMutableData和NSURL
NSData和NSMutableData表示oc的数据缓冲区
作用:
1)、对数据读取NSData
2)、输出NSData的数据 NSURL可以读取网络数据 2、MSFileManager管理文件和目录
作用#xff1a;创建文件、删除文件、复制文件、移动文件 3、NSFileHandle处理文件…1、NSData和NSMutableData和NSURL
NSData和NSMutableData表示oc的数据缓冲区
作用:
1)、对数据读取NSData
2)、输出NSData的数据 NSURL可以读取网络数据 2、MSFileManager管理文件和目录
作用创建文件、删除文件、复制文件、移动文件 3、NSFileHandle处理文件io
我们可以用NSFileHandle写入数据到文件里面去、从文件里面读取数据、、 4、测试Demo
int main(int argc, char * argv[]) {autoreleasepool {NSLog(当前用户名为:%, NSUserName());NSLog(当前用户名为:%, NSFullUserName());/**NSData *data [NSData dataWithContentsOfURL:[NSURL URLWithString:https://www.baidu.com]];NSLog(%ld, [data length]);char buff[200];[data getBytes:buff range:NSMakeRange(0, 20)];NSLog(%s, buff);//NSData to UTF-8 StringNSString *str [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];NSLog();NSLog(%, str);**/
// NSString *documentsPath [self getDocumentsPath];// rwxr-xr-x 3 ls access_bpf 96B Jul 20 21:47 Documents
// drwxr-xr-x 5 ls access_bpf 160B Jul 20 20:45 Library
// drwxr-xr-x 2 ls access_bpf 64B Jul 5 16:24 SystemData
// drwxr-xr-x 2 ls access_bpf 64B Jul 5 16:24 tmp//获取沙盒根路径NSString *homePath NSHomeDirectory();NSLog(home Paht is:%, homePath);//获取 tmp目录NSString *tmpPath NSTemporaryDirectory();NSLog(tmpPath is: %, tmpPath);//获取Documents路径NSArray *docPaths NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *path [docPaths objectAtIndex:0];NSLog(document path:%, path);NSFileManager *fileManager [NSFileManager defaultManager];//创建文件夹NSString *dir dir;NSString *dirPath [path stringByAppendingPathComponent:dir];BOOL result [fileManager createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:nil];if (result){NSLog(make dir success);}else{NSLog(make dir fail);}//在document目录下面创建chenyu.txt文件然后内容为hello, I am testing NSFileManager, my name is chenyuNSString *chenyuPath [path stringByAppendingPathComponent:chenyu.txt];NSString *content hello, I am testing NSFileManager, my name is chenyu;BOOL isSuccess [fileManager createFileAtPath:chenyuPath contents:[content dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];if (isSuccess) {NSLog(make chenyu.txt success);} else {NSLog(make chenyu.txt fail);}//读取chenyu.txt文件的内容NSString *value [NSString stringWithContentsOfFile:chenyuPath encoding:NSUTF8StringEncoding error:nil];NSLog(chenyu.txt value is:%, value);//新建chenyu1.txt 文件NSString *chenyuPath1 [path stringByAppendingPathComponent:chenyu1.txt];NSString *content1 hello, I am testing NSFileManager, my name is chenyu1;BOOL result1 [fileManager createFileAtPath:chenyuPath1 contents:[content1 dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];if (result1) {NSLog(make chenyu1.txt success);} else {NSLog(make chenyu1.txt fail);}//..../Data/Application/91D24910-B3CB-470C-9C62-D54E2815DF4E/Documents/
// -rw-r--r-- 1 ls access_bpf 52B Jul 20 23:18 chenyu.txt
// -rw-r--r-- 1 ls access_bpf 53B Jul 20 23:18 chenyu1.txt
// drwxr-xr-x 2 ls access_bpf 64B Jul 20 23:08 dir
// -rw-r--r-- 1 ls access_bpf 33B Jul 20 22:12 iOS.txt//删除chenyu1.txt文件BOOL result2 [fileManager removeItemAtPath:chenyuPath1 error:nil];if (result2) {NSLog(delete chenyu1.txt success);} else {NSLog(delete chenyu1.txt fail);}
// -rw-r--r-- 1 ls access_bpf 52B Jul 20 23:23 chenyu.txt
// drwxr-xr-x 2 ls access_bpf 64B Jul 20 23:08 dir
// -rw-r--r-- 1 ls access_bpf 33B Jul 20 22:12 iOS.txt//新建chenyu2.txt 文件NSString *chenyuPath2 [path stringByAppendingPathComponent:chenyu2.txt];
// NSString *content2 hello, I am testing NSFileManager, my name is chenyu2;BOOL res [fileManager createFileAtPath:chenyuPath2 contents:nil attributes:nil];if (res) {NSLog(make chenyu2.txt success);} else {NSLog(make chenyu2.txt fail);}//NSFileHandle写数据到文件NSFileHandle *handle [NSFileHandle fileHandleForWritingAtPath:chenyuPath2];[handle writeData:[this is useing NSHandle write data to chenyu2.txt dataUsingEncoding:NSUTF8StringEncoding]];//NSFileHandle读取文件的内容NSFileHandle *readHandle [NSFileHandle fileHandleForReadingAtPath:chenyuPath2];NSData *data [readHandle readDataToEndOfFile];NSString *ss [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];NSLog(read data is:%, ss);//运行结果目录下的文件
// rw-r--r-- 1 ls access_bpf 52B Jul 20 23:33 chenyu.txt
// -rw-r--r-- 1 ls access_bpf 49B Jul 20 23:33 chenyu2.txt
// drwxr-xr-x 2 ls access_bpf 64B Jul 20 23:08 dir
// -rw-r--r-- 1 ls access_bpf 33B Jul 20 22:12 iOS.txt// 用vim打开chenyu2.txt内容如下
// this is useing NSHandle write data to chenyu2.txt}
}
上面部分部分//是在终端输入ll命令后打印的信息 5、运行结果
2018-07-21 21:22:02.7717530800 cyTest[31783:15710817] 当前用户名为:
2018-07-21 21:22:02.7741910800 cyTest[31783:15710817] 当前用户名为:
2018-07-21 21:22:02.7749730800 cyTest[31783:15710817] home Paht is:/Users/ls/Library/Developer/CoreSimulator/Devices/3FF9B833-FAF8-4C30-A855-3D40A4EAE8A6/data/Containers/Data/Application/BE126DC7-C827-4BD0-8BFC-A517ADC66E6C
2018-07-21 21:22:02.7752140800 cyTest[31783:15710817] tmpPath is: /Users/ls/Library/Developer/CoreSimulator/Devices/3FF9B833-FAF8-4C30-A855-3D40A4EAE8A6/data/Containers/Data/Application/BE126DC7-C827-4BD0-8BFC-A517ADC66E6C/tmp/
2018-07-21 21:22:02.7757900800 cyTest[31783:15710817] document path:/Users/ls/Library/Developer/CoreSimulator/Devices/3FF9B833-FAF8-4C30-A855-3D40A4EAE8A6/data/Containers/Data/Application/BE126DC7-C827-4BD0-8BFC-A517ADC66E6C/Documents
2018-07-21 21:22:02.7766000800 cyTest[31783:15710817] make dir success
2018-07-21 21:22:02.7791850800 cyTest[31783:15710817] make chenyu.txt success
2018-07-21 21:22:02.7800330800 cyTest[31783:15710817] chenyu.txt value is:hello, I am testing NSFileManager, my name is chenyu
2018-07-21 21:22:02.7823650800 cyTest[31783:15710817] make chenyu1.txt success
2018-07-21 21:22:02.7842350800 cyTest[31783:15710817] delete chenyu1.txt success
2018-07-21 21:22:02.7857280800 cyTest[31783:15710817] make chenyu2.txt success
2018-07-21 21:22:02.7869200800 cyTest[31783:15710817] read data is:this is useing NSHandle write data to chenyu2.txt