一、简单展示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. mini KMS Activator v1.3破解激活microsoft Office 2010 RTM

    利用mini KMS Activator v1.3破解激活microsoft Office 2010 RTM方法,只是为体验office而做测试使用的哦...大家觉得好就自觉购买正版去... 使用步骤 ...

  2. 面向对象程序设计-C++_课时19const_课时20不可修改的

    error C2131: 表达式的计算结果不是常数 #include <iostream> using namespace std; void main() { ; int finalGr ...

  3. Android 匿名共享内存C++接口分析

    在上一篇Android 匿名共享内存C接口分析中介绍了Android系统的匿名共享内存C语言访问接口,本文在前文的基础上继续介绍Android系统的匿名共享内存提供的C++访问接口.在C++层通过引入 ...

  4. TabHost 简单用法

    package com.google.tabhost;    import android.app.TabActivity;  import android.os.Bundle;  import an ...

  5. 指针和Const限定符

    指针和Const限定符 1.指向const对象的指针 如果指针指向的是const对象,则不允许使用指针来改变其所指的const值.C++要求指向const对象的指针具有const特性. const d ...

  6. Javascript知识四(DOM)

     [箴 10:4] 手懒的,要受贫穷:手勤的,却要富足. He becometh poor that dealeth with a slack hand: but the hand of the di ...

  7. sql server 2000 和 sql server 2005 数据库连接字符串区别

    //sql server 2000 <add name="Connection" connectionString="Data Source=.;Initial C ...

  8. 计算Date间的分钟数

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm"); Date date_begin = simpl ...

  9. shell检测interface是否已分配ip,qt调用shell脚本

    #include <QCoreApplication>#include <QDebug>#include <QTextStream>#include <QDi ...

  10. XLSReadWrite控件简介

    2015-10-22  23:57:55 原帖地址:http://www.cnblogs.com/dabiao/archive/2011/07/08/2100609.html XLSReadWrite ...