一、简单展示NSFileManager的使用

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{ @autoreleasepool {
//创建文件管理对象
NSFileManager *fm = [NSFileManager defaultManager];
//要操作的文件名
NSString *fname = @"myfile";
//获取文件的字典
NSDictionary *attr;
//当前路径
NSString *path;
//获取当前路径
path = [fm currentDirectoryPath];
//NSLog(@"\nThe current path is : %@", path); //检测文件是否存在
if ([fm fileExistsAtPath: fname] == NO) {
//如果不存在则建立一个文件
[fm createFileAtPath: fname contents: NULL attributes:nil];
//NSLog(@"\nThe file is not exist!");
//return 0;
}
//拷贝创建一个新文件, 新文件若已存在则报错
if ([fm copyItemAtPath: fname toPath: @"newFile" error: NULL] == NO) {
NSLog(@"\n Can't copy the file");
return ;
}
//检测两个文件内容是否相同
if ([fm contentsEqualAtPath: fname andPath: @"newFile"] == NO) {
NSLog(@"\nThe contents is not same");
return ;
}
//移动或者改名文件
if ([fm moveItemAtPath: @"newFile" toPath: @"myFile2" error:NULL] == NO) {
NSLog(@"\nCan't change the name");
return ;
}
//获取文件数据字典
if ((attr = [fm attributesOfItemAtPath: fname error:NULL]) == nil) {
NSLog(@"\nGet attributets failed");
return ;
}
//文件大小
NSLog(@"%@", attr[NSFileSize]);
//文件类型
NSLog(@"%@", attr[NSFileType]);
//创建者
NSLog(@"%@", attr[NSFileOwnerAccountName]);
//
NSLog(@"%@", attr[NSFileCreationDate]);
//显示文件内容
NSLog(@"\n Show the file contents");
NSLog(@"\n%@", [NSString stringWithContentsOfFile: fname encoding:NSUTF8StringEncoding error:NULL]);
}
return ;
}

二、通过NSData完成副本制作

 int main(int argc, const char * argv[])
{ @autoreleasepool {
//通过NSDate来完成文件副本制作
NSFileManager *fm = [NSFileManager defaultManager];
NSData *dt; dt = [fm contentsAtPath: @"myfile"]; if (dt == nil) {
NSLog(@"Read file failed....");
return ;
} //将缓冲区NSData中的内容复制到文件中
if ([fm createFileAtPath:@"myFavoriteFile" contents: dt attributes:nil] == NO) {
NSLog(@"Creat backups failed");
return ;
} //读出文件内容
NSLog(@"\n%@", [NSString stringWithContentsOfFile:@"myFavoriteFile" encoding: NSUTF8StringEncoding error:NULL]);
}
return ;
}

三、简单的目录操作

 #import <Foundation/Foundation.h>

 int main(int argc, const char * argv[])
{ @autoreleasepool {
NSString *newDir = @"newDir";
NSString *currentPath;
NSFileManager *fm = [NSFileManager defaultManager]; //获取当前路径
currentPath = [fm currentDirectoryPath];
NSLog(@"\nCurrentpath is : \n%@", currentPath); //在当前目录下新建一个目录
if ([fm createDirectoryAtPath:newDir withIntermediateDirectories:TRUE attributes:nil error:NULL] == NO) {
NSLog(@"\nCouldn't creat the directory...");
return ;
} //更改路径名
if ([fm moveItemAtPath: newDir toPath: @"changeDir" error:NULL] == NO) {
NSLog(@"\nChange directory name failed");
return ;
} //更改当前路径
if ([fm changeCurrentDirectoryPath:@"changeDir"] == NO) {
NSLog(@"\nChange current directory failed");
return ;
}
NSLog(@"\nAfter change current directory.....");
currentPath = [fm currentDirectoryPath];
NSLog(@"\nCurrentpath is : \n%@", currentPath);
}
return ;
}

Foundation 框架 NSFileManager,NSData 简单的文件操作的更多相关文章

  1. 本地Eclipse连接HDFS进行简单的文件操作

    昨天总结了一点自己在搭建Hadoop完全分布式环境过程中遇到的几个小问题以及解决方案,今天在搭建成功的环境中进行了简单的文件操作,包括:文件目录的创建.文件的创建.本地文件的上传.文件的重命名.文件的 ...

  2. python反转字符串(简单方法)及简单的文件操作示例

    Python反转字符串的最简单方法是用切片: >>> a=' >>> print a[::-1] 654321 切片介绍:切片操作符中的第一个数(冒号之前)表示切片 ...

  3. scala简单的文件操作

    1.scala写入文件操作 package com.test import java.io.File import java.io.PrintWriter /** * scala文件写入 */ obj ...

  4. python作业完成简单的文件操作

    题目 请创建以学号命名的目录,在该目录中创建名称为file1.txt的文件,并将自己的个人信息(序号.姓名以及班级)等写入该文件:然后并读取文件中的内容到屏幕上:接着重新命名该文件为file2.txt ...

  5. Objective-C NSFileManager的使用 各种文件操作

    所有方法 都很简单,大概记录一下,写文件并没有是追加的方式而是简单的覆盖 //创建文件夹 - (BOOL)creatDir:(NSString*)newDirName at:(NSString*)di ...

  6. php简单的文件操作

    (1)先要想好要操作哪个文件? (2)确定文件的路径? (3)要有什么文件管理功能? 一.先做一下简单的查看文件功能,文件中的文件和文件夹都显示,但是双击文件夹可以显示下一级子目录,双击"返 ...

  7. python实现简单的登陆认证(含简单的文件操作)

    需求: 让用户输入用户名密码 认证成功后显示欢迎信息 输错三次后退出程序 可以支持多个用户登录 (提示,通过列表存多个账户信息) 用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态(本 ...

  8. C#中简单的文件操作实例

    using System; using System.IO; namespace Demo { class Program { static string tmpPath = @"D:/Lg ...

  9. Golang简单写文件操作的四种方法

    package main import ( "bufio" //缓存IO "fmt" "io" "io/ioutil" ...

随机推荐

  1. poj2027简单题

    #include <stdio.h> #include <stdlib.h> int main() { int n,x,y; scanf("%d",& ...

  2. DKNY_百度百科

    DKNY_百度百科 DKNY

  3. [置顶] C# WINCE调节屏幕亮度

    在wince里面保存屏幕亮度的值保存在注册表HKEY_CURRENT_USER\ControlPanel\\Backlight\Brightness里面,值的范围是0-100,所以要改变屏幕的亮度,只 ...

  4. 自己定义View常处理的回调函数

    onFinishInflate() 当View中全部的子控件均被映射成xml后触发 onMeasure(int, int) 确定全部子元素的大小 onLayout(boolean, int, int, ...

  5. Cocos2d-x 3.0 lua规划 真正的现在Android在响应Home密钥和Back纽带

    local listenerKey= cc.EventListenerKeyboard:create() local function onKeyReleaseed(keycode,event) if ...

  6. nodejs在服务器上运行

     nodejs运行之后,关掉链接,网站运行就会断开,需要安装forever,后台执行. 安装方法如下(在windows和Linux下都能运行)://forever的安装: npm install fo ...

  7. C#将图片转化为黑白图片

    最近项目需要将上传的图片转化为黑白图片 在网上找了很多资料,测试通过,上代码 using System; using System.Collections.Generic; using System. ...

  8. spring mvc 返回json数据的四种方式

    一.返回ModelAndView,其中包含map集 /* * 返回ModelAndView类型的结果 * 检查用户名的合法性,如果用户已经存在,返回false,否则返回true(返回json数据,格式 ...

  9. iOS 在当前控制器中发送短信

    - (void)showMessageViewWithTel:(NSString *)tel { if( [MFMessageComposeViewController canSendText] ){ ...

  10. GWT RPC机制

    GWT RPC GWT RPCRemote Procedure Calls GWT: Google Web Toolkit的缩写,有了 GWT可以使用 Java 编程语言编写 AJAX 前端,然后 G ...