用到的是NSString中的initWithContentsOfFile: encoding方法

//
// main.m
// 读取指定文件并输出内容
//
// Created by Apple on 15/11/24.
// Copyright © 2015年 Apple. All rights reserved.
// /*
*读取指定txt文件,并把文件中的内容输出出来,
*/
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { NSError *error = nil;
NSMutableString *path = [NSMutableString stringWithCapacity:];
NSString *home = [@"~" stringByExpandingTildeInPath];
[path appendString:home];
[path appendString:@"/work/temp.txt"];
//NSString *string = [[NSString alloc] initWithContentsOfFile:@"/Users/apple/work/temp.txt" encoding:NSUTF8StringEncoding error:&error];
NSString *string = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error]; //如果有报错,则把报错信息输出来
if (error != nil) {
NSLog(@"%@",[error localizedDescription]);
} NSLog(@"%@",string); return ;
}

补充多一个例子:

//
// main.m
// 字符串练习2:读写文件
//
// Created by Apple on 15/12/7.
// Copyright © 2015年 Apple. All rights reserved.
// #import <Foundation/Foundation.h>
void readFile(NSString *path);
void writeToFile(NSString *path, NSString *str); int main(int argc, const char * argv[]) { //读取文件中的内容
NSString *path1 = @"/Users/apple/Desktop/KeenApps/Object-C/Object-c-Test/字符串练习2:读写文件/1.txt";
//NSString *path = @"/Users/apple/Desktop/2.txt"; NSLog(@"读取文件:");
readFile(path1); //写入文件内容
NSString *path2 = @"/Users/apple/Desktop/KeenApps/Object-C/Object-c-Test/字符串练习2:读写文件/2.txt";
NSLog(@"写入文件");
NSString *str = @"这是一个测试";
writeToFile(path2,str); NSLog(@"读取文件:");
readFile(path2);
return ;
} //读取文件
void readFile(NSString *path){
NSError *error = nil;
NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error]; if (error != nil) {
NSLog([error localizedDescription]);//将错误信息输出来
}
else{
NSLog(@"%@",str);
} } //写入文件
void writeToFile(NSString *path, NSString *str){
NSError *error = nil;
//atomically : YES时,没有写完,则会全部撤销;NO时候,没有写完,不会撤销
//注意:这种写入方式,如果文件补存在,则创建;如果文件存在,则覆盖原文件的内容
BOOL flag = [str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];//一般error都设置为nil,保证写入成功
if (flag) {
NSLog(@"写入成功");
}
else{
NSLog(@"写入失败");
}
}

补充:

使用URL方式访问:http://www.cnblogs.com/KeenLeung/p/5028012.html

iOS案例:读取指定txt文件,并把文件中的内容输出出来的更多相关文章

  1. 遍历并读取指定目录下的所有文件内容,写入Map集合然后输出在控制台和本地文件

    public class FileWrite { public static void main(String[] args) throws Exception { //封装数据源目录 File sr ...

  2. 使用python内置模块os和openpyxl搜索指定文件夹下Excel中的内容

    在指定路径下,搜索Excel文件中包含的指定内容,首先需要遍历指定路径,得到该路径下所有Excel文件的绝对/相对路径:然后读取Excel中内容,将文件中的每个单元格的值与要搜索的内容进行判断(正则比 ...

  3. Python3实现从文件中读取指定行的方法

    from:http://www.jb51.net/article/66580.htm 这篇文章主要介绍了Python3实现从文件中读取指定行的方法,涉及Python中linecache模块操作文件的使 ...

  4. iOS案例:读取指定目录下的文件列表

    // // main.m // 读取指定目录下的文件列表 // // Created by Apple on 15/11/24. // Copyright © 2015年 Apple. All rig ...

  5. 深入理解pandas读取excel,txt,csv文件等命令

    pandas读取文件官方提供的文档 在使用pandas读取文件之前,必备的内容,必然属于官方文档,官方文档查阅地址 http://pandas.pydata.org/pandas-docs/versi ...

  6. java读取文件夹下所有文件并替换文件每一行中指定的字符串

    import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.I ...

  7. 【C++】ubuntu中读取指定目录中的所有文件

    摘要:ubuntu系统下,C++程序读取指定文件夹中多个文件,保存文件名列表.文件名没有规律且不考虑读取子文件夹中的文件. 系统配置:ubuntu16.04, cmake编译 首先安利一个函数,输入s ...

  8. WinForm读取指定的config文件的内容

    config文件的使用 一.缘起 最近做项目开始使用C#,因为以前一直使用的是C++,因此面向对象思想方面的知识还是比较全面的,反而是因没有经过完整.系统的.Net方面知识的系统学习,经常被一些在C# ...

  9. Python--通过索引excel表将文件进行文件夹分类的脚本+读取指定目录下所有文件名的脚本

    1.通过索引excel表将文件进行文件夹分类的脚本,此脚本由于将ip和id对应并生成对应id的文件夹将文件进行分类,也可以任意规定表格内容,通过vul_sc_ip.txt和xlsx文件进行索引. # ...

随机推荐

  1. 161103、Spring Boot 入门

    Spring Boot 入门 spring Boot是Spring社区较新的一个项目.该项目的目的是帮助开发者更容易的创建基于Spring的应用程序和服务,让更多人的人更快的对Spring进行入门体验 ...

  2. 控制反转(IOC)和依赖注入(DI)的区别

    IOC   inversion of control  控制反转 DI   Dependency Injection  依赖注入 要理解这两个概念,首先要搞清楚以下几个问题: 参与者都有谁? 依赖:谁 ...

  3. interrupt ,interrupted 和 isInterrupted

    1.interrupt  interrupt方法用于中断线程.调用该方法的线程的状态为将被置为"中断"状态. 注意:线程中断仅仅是置线程的中断状态位,不会停止线程.需要用户自己去监 ...

  4. masonry插件和infinitescroll插件实现无刷新无分页完美瀑布流

    地址有:http://www.17sucai.com/pins/2657.html 如果你善于发现美,如果你善于观察新鲜的事物,如果你是一名有爱的前端攻城师或设计尸,那么你一定不会对下面图片中的结构感 ...

  5. hdu So Easy!

    So Easy! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  6. 2016年11月27日 星期日 --出埃及记 Exodus 20:18

    2016年11月27日 星期日 --出埃及记 Exodus 20:18 When the people saw the thunder and lightning and heard the trum ...

  7. Android Handler Message总结

    http://blog.csdn.net/caesardadi/article/details/8473777 当应用程序启动时,会开启一个主线程(也就是UI线程),由她来管理UI,监听用户点击,来响 ...

  8. 基于Node的PetShop,RESTful API以及认证

    前篇 - 基本认证,用户名密码 后篇 - OAuth2 认证 由于宠物店的业务发展需要,我们需要一种更加便捷的方式来管理日益增多的宠物和客户.最好的方法就是开发一个APP,我可以用这个APP来添加.更 ...

  9. centos 001

    CentOS6.5中修改yum源 在自己安装的CentOS6.5中使用yum安装软件,总是提示404错误信息,百度后发现原来要设置yum源. 在安装完CentOS后一般需要修改yum源,才能够在安装更 ...

  10. IMetadataAware接口的特性定制Model元数据

    第一步创建元数据类 using System; using System.Collections.Generic; using System.Linq; using System.Reflection ...