OC对文本的操作
2015-12-16 17:37
309 查看
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
//创建文件
NSString *homePath = NSHomeDirectory();
//创建一个文件file.text
NSString *filePath = [homePath stringByAppendingPathComponent:@"Documents/file.text"];
//NSFileManager是单利模式,所以不能使用alloc+init创建
NSFileManager *manager = [NSFileManager defaultManager];
NSString *str = @"啦啦啦啦啦啦";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
//参数:文件路径、文件内容、文件的属性
BOOL sucess = [manager createFileAtPath:filePath contents:data attributes:nil];
if(sucess){
NSLog(@"文件创建成功");
}else{
NSLog(@"文件创建失败");
}
//创建文件夹
NSString *filePaths = [homePath stringByAppendingPathComponent:@"Documents/file"];
NSError *error;
//需要传递一个创建失败的指针对象,记录创建失败的信息
BOOL success1 = [manager createDirectoryAtPath:filePaths withIntermediateDirectories:YES attributes:nil error:&error];
if(!success1){
NSLog(@"创建失败");
}else{
NSLog(@"创建成功");
}
//--------------------读取文件
//根据路径读取文件内容
NSData *datas = [manager contentsAtPath:filePath];
NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",s);
//--------------------移动文件/剪切文件
//NSFileManager中没有提供重命名的方法,所以我们可以借助移动的api进行操作
//把filePath移动到targetPath目录中
// NSString *targetPath = [homePath stringByAppendingPathComponent:@"Documents/file/file2.text"];
// BOOL sucess2 = [manager moveItemAtPath:filePath toPath:targetPath error:nil];
// if(sucess2) {
// NSLog(@"移动成功");
// }else{
// NSLog(@"移动失败");
// }
//这里有两个参数:一个是需要移动文件的路径,和需要移动到哪的路径
//--------------------复制文件
// BOOL sucess3 = [manager copyItemAtPath:filePath toPath:targetPath error:nil];
// if(sucess3){
// NSLog(@"复制成功");
// }else{
// NSLog(@"复制失败");
// }
// //--------------------删除文件
// //删除之前需要判断这个文件是否存在
// BOOL isExist = [manager fileExistsAtPath:filePath];//判断文件是否存在
// if(isExist){
// BOOL sucess4 = [manager removeItemAtPath:filePath error:nil];
// if(sucess4){
// NSLog(@"删除成功");
// }else{
// NSLog(@"删除失败");
// }
// }
// //--------------------获取文件的属性
// NSDictionary *dic = [manager attributesOfItemAtPath:filePath error:nil];
// NSLog(@"%@",dic);//通过打印我们就可以查看文件属性的一些key属性一般是NSDictionary
}
return 0;
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
//创建文件
NSString *homePath = NSHomeDirectory();
//创建一个文件file.text
NSString *filePath = [homePath stringByAppendingPathComponent:@"Documents/file.text"];
//NSFileManager是单利模式,所以不能使用alloc+init创建
NSFileManager *manager = [NSFileManager defaultManager];
NSString *str = @"啦啦啦啦啦啦";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
//参数:文件路径、文件内容、文件的属性
BOOL sucess = [manager createFileAtPath:filePath contents:data attributes:nil];
if(sucess){
NSLog(@"文件创建成功");
}else{
NSLog(@"文件创建失败");
}
//创建文件夹
NSString *filePaths = [homePath stringByAppendingPathComponent:@"Documents/file"];
NSError *error;
//需要传递一个创建失败的指针对象,记录创建失败的信息
BOOL success1 = [manager createDirectoryAtPath:filePaths withIntermediateDirectories:YES attributes:nil error:&error];
if(!success1){
NSLog(@"创建失败");
}else{
NSLog(@"创建成功");
}
//--------------------读取文件
//根据路径读取文件内容
NSData *datas = [manager contentsAtPath:filePath];
NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",s);
//--------------------移动文件/剪切文件
//NSFileManager中没有提供重命名的方法,所以我们可以借助移动的api进行操作
//把filePath移动到targetPath目录中
// NSString *targetPath = [homePath stringByAppendingPathComponent:@"Documents/file/file2.text"];
// BOOL sucess2 = [manager moveItemAtPath:filePath toPath:targetPath error:nil];
// if(sucess2) {
// NSLog(@"移动成功");
// }else{
// NSLog(@"移动失败");
// }
//这里有两个参数:一个是需要移动文件的路径,和需要移动到哪的路径
//--------------------复制文件
// BOOL sucess3 = [manager copyItemAtPath:filePath toPath:targetPath error:nil];
// if(sucess3){
// NSLog(@"复制成功");
// }else{
// NSLog(@"复制失败");
// }
// //--------------------删除文件
// //删除之前需要判断这个文件是否存在
// BOOL isExist = [manager fileExistsAtPath:filePath];//判断文件是否存在
// if(isExist){
// BOOL sucess4 = [manager removeItemAtPath:filePath error:nil];
// if(sucess4){
// NSLog(@"删除成功");
// }else{
// NSLog(@"删除失败");
// }
// }
// //--------------------获取文件的属性
// NSDictionary *dic = [manager attributesOfItemAtPath:filePath error:nil];
// NSLog(@"%@",dic);//通过打印我们就可以查看文件属性的一些key属性一般是NSDictionary
}
return 0;
}
相关文章推荐
- StringBuffer代码优化
- ie 7/8/9 placeholder 不显示 解决方案
- 机器学习入门(一)朴素贝叶斯解析
- 应用EL表达式显示客户端能够接收的内容类型
- oracle11G使用DGbroker创建dg
- iframe自适应高度
- android 友盟 用户反馈 自定义界面发图片问题
- MySQL中列子查询与行子查询操作的学习教程
- Canvas 的绘制图表的基本用法
- 1.6---旋转二维数组,旋转图像像素,旋转矩阵,90度(CC150)
- iOS真机调试报错 A valid provisioning profile for this executable was not found.
- 开启Instruments之旅吧
- 自定义View执行invalidate()方法
- 最最简单又实用的ftp安装以及使用
- [转] 深入探讨C++中的引用
- 1.5---字符串压缩(CC150)
- ios实现屏幕旋转的方法
- Qt学习笔记(QAction,QString格式化输出)
- oracle profile介绍
- PopupWindow 点击其他地方消失