在Objective-C 中的 Foundation 框架中,文件操作是由NSFileManager 类来实现的。

下面通过例子来说明如何创建一个文件,并向文件中写内容,以及如何读出文件中的内容:

- (void)testFileCreate
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
filePath = [filePath stringByAppendingPathComponent:@"new.txt"];
NSLog(@"filePath = %@",filePath);
// 判断文件是否存在
if (![fileManager fileExistsAtPath:filePath]){
// 若文件不存在,则新建文件
[fileManager createFileAtPath:filePath contents:nil attributes:nil];
}
// 向文件中写内容,通过文件句柄,NSFileHandle实现
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
NSString *content = @"hey,brother.This is a test.";
NSData *contentData = [content dataUsingEncoding:NSUTF8StringEncoding];
[fileHandle writeData:contentData];
// 关闭文件
[fileHandle closeFile]; // 读取文件中的内容
fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath];
NSData *readData = [fileHandle readDataToEndOfFile];
// data 转 NSString
NSString *readStr = [[NSString alloc] initWithData:readData encoding:NSUTF8StringEncoding];
NSLog(@"readStr = %@",readStr);
[fileHandle closeFile];
// 直接以NSString 的方式读取文件
NSString *contentStr = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"contentStr = %@",contentStr);
}

文件的一些常规操作,如复制文件、删除文件、移动文件等:

- (void)testFileOperation
{
// 获得临时目录
NSString *tempPath = NSTemporaryDirectory();
NSLog(@"tempPath = %@",tempPath);
// 最后一级目录
NSLog(@"last = %@", [tempPath lastPathComponent] );
// 在最后增加一级目录,原目录不变,返回一个新的目录字符串
NSLog(@"add last = %@",[tempPath stringByAppendingPathComponent:@"add"]);
// 删除最后一级目录,原目录不变,返回一个新的目录字符串
NSLog(@"del last = %@",[tempPath stringByDeletingLastPathComponent]);
NSString *filePath = [tempPath stringByAppendingPathComponent:@"test.txt"];
NSLog(@"filePath = %@",filePath);
// 扩展名,输出为 txt
NSLog(@"extension = %@",[filePath pathExtension]);
NSFileManager *manager = [NSFileManager defaultManager];
if(![manager fileExistsAtPath:filePath]){
[manager createFileAtPath:filePath contents:nil attributes:nil];
} NSString *newPath = [tempPath stringByAppendingPathComponent:@"newtest.txt"];
// 拷贝文件
[manager copyItemAtPath:filePath toPath:newPath error:nil];
if([manager fileExistsAtPath:newPath]){
NSLog(@"copy success");
}
// 删除文件
[manager removeItemAtPath:newPath error:nil];
if(![manager fileExistsAtPath:newPath]){
NSLog(@"remove success");
} // 文件是否可读
if([manager isReadableFileAtPath:filePath]){
NSLog(@"readable");
}
// 文件是否可写
if([manager isWritableFileAtPath:filePath]){
NSLog(@"writeable");
}
}

Objective-C: NSFileManager 的使用的更多相关文章

  1. Automake

    Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...

  2. Objective-C( Foundation框架 一 NSFileManager)

    NSFileManager 用来管理文件系统的 它可以用于常见的文件,文件夹操作(拷贝,剪切,创建) NSFileManager使用了单例模式(Singleton) 使用defaultManager可 ...

  3. Objective C (iOS) for Qt C++ Developers(iOS开发,Qt开发人员需要了解什么?)

    Qt/C++开发人员眼中的Obj-C      对于我们第一次自己定义iOS应用来说,对于来自Qt/C++开发人员来说,我不得不学习Objective-C相关语法与知识 为了让读者可以更easy理解这 ...

  4. iOS开发核心语言Objective C —— 全部知识点总结

    本分享是面向有意向从事iOS开发的伙伴及苹果产品的发烧友,亦或是已经从事了iOS的开发人员,想进一步提升者.假设您对iOS开发有极高的兴趣,能够与我一起探讨iOS开发.一起学习,共同进步.假设您是零基 ...

  5. 归档NSKeyedArchiver解归档NSKeyedUnarchiver与文件管理类NSFileManager (文件操作)

    ========================== 文件操作 ========================== 一.归档NSKeyedArchiver 1.第一种方式:存储一种数据. // 归档 ...

  6. ios 文件操作(NSFileManager)

    IOS的沙盒机制,应用只能访问自己应用目录下的文件,iOS不像android,没有SD卡概念,不能直接访问图像.视频等内容. iOS应用产生的内容,如图像.文件.缓存内容等都必须存储在自己的沙盒内. ...

  7. Objective C中的ARC的修饰符的使用---- 学习笔记九

    #import <Foundation/Foundation.h> @interface Test : NSObject /** * 默认的就是__strong,这里只是做示范,实际使用时 ...

  8. 【原】iOS学习之文件管理器(NSFileManager)和文件对接器(NSFileHandle)

    1.文件管理器(NSFileManager) 1> 主要作用及功能方法 主要作用:此类主要是对文件进行的操作(创建/删除/改名等)以及文件信息的获取. 功能方法: 2> 创建文件夹 创建所 ...

  9. Objective的字符串拼接 似乎没有Swift方便,但也可以制做一些较为方便的写法

    NSString *str1 = @"字符串1"; NSString *str2 = @"字符串2"; //在同样条件下,Objective的字符串拼接 往往只 ...

  10. [转] 从 C 到 Objective C 入门1

    转自: http://blog.liuhongwei.cn/iphone/objective-c/ 进军iPhone开发,最大的难点之一就是怪异的Objective C语法了.不过,了解之后才发现,原 ...

随机推荐

  1. Java知识点:instanceof关键字

    介绍 格式:a instanceof B,其中a是一个variable(设a所指向的对象的类型命名为A,即A是一个type. 功能:判断A是否 is-a B. 当a=null时,始终返回false. ...

  2. phonegap archive 报错 Cordova/CDVViewController.h' file not found

    在BuildSettings->Header Search Paths  增加如下路径,问题解决 $(OBJROOT)/UninstalledProducts/include "$(O ...

  3. qt创建android项目后需要加入的参数

    默认用qtcreator5.2.0创建了一个quick项目,却报如下错误: error:cstdlib.h no such file or directory 解决方法: 打开项目文件untitled ...

  4. Android 超仿Path时间轴和扇形菜单的效果

    网上看到的  ,仅此记录一下,用到的时候便于查找 效果如下: 源码下载地址 :  http://download.csdn.net/detail/abc13939746593/7251933

  5. jvm内部现成运行

    hi,all 最近抽时间把JVM运行过程中产生的一些线程进行了整理,主要是围绕着我们系统jstack生成的文件为参照依据.  前段时间因为系统代码问题,造成性能瓶颈,于是就dump了一份stack出来 ...

  6. 【转】iOS自动布局进阶用法

    原文网址:http://www.cnblogs.com/dsxniubility/p/4266581.html 本文主要介绍几个我遇到并总结的相对高级的用法(当然啦牛人会觉得这也不算什么). 简单的s ...

  7. Android-关于android:scrollbarStyle属性

    1. activity_maim.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android ...

  8. OLAP、OLTP的介绍和比较 via csdn

    OLAP.OLTP的介绍和比较 数据处理大致可以分成两大类: OLTP(On-Line Transaction Processing)联机事务处理 也称为面向交易的处理系统,其基本特征是顾客的原始数据 ...

  9. Canvas处理头像上传

    未分类 最近社区系统需要支持移动端,其中涉及到用户头像上传,头像有大中小三种尺寸,在PC端,社区用Flash来处理头像编辑和生成,但该Flash控件的界面不友好而且移动端对Flash的支持不好,考虑到 ...

  10. csv文件与DataTable互相导入处理

    封装处理下,以后项目用到可以直接使用,比较简单. 1.首先看封装好的类 using System; using System.Data; using System.IO; using System.T ...