iOS案例:读取指定txt文件,并把文件中的内容输出出来
用到的是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文件,并把文件中的内容输出出来的更多相关文章
- 遍历并读取指定目录下的所有文件内容,写入Map集合然后输出在控制台和本地文件
public class FileWrite { public static void main(String[] args) throws Exception { //封装数据源目录 File sr ...
- 使用python内置模块os和openpyxl搜索指定文件夹下Excel中的内容
在指定路径下,搜索Excel文件中包含的指定内容,首先需要遍历指定路径,得到该路径下所有Excel文件的绝对/相对路径:然后读取Excel中内容,将文件中的每个单元格的值与要搜索的内容进行判断(正则比 ...
- Python3实现从文件中读取指定行的方法
from:http://www.jb51.net/article/66580.htm 这篇文章主要介绍了Python3实现从文件中读取指定行的方法,涉及Python中linecache模块操作文件的使 ...
- iOS案例:读取指定目录下的文件列表
// // main.m // 读取指定目录下的文件列表 // // Created by Apple on 15/11/24. // Copyright © 2015年 Apple. All rig ...
- 深入理解pandas读取excel,txt,csv文件等命令
pandas读取文件官方提供的文档 在使用pandas读取文件之前,必备的内容,必然属于官方文档,官方文档查阅地址 http://pandas.pydata.org/pandas-docs/versi ...
- java读取文件夹下所有文件并替换文件每一行中指定的字符串
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.I ...
- 【C++】ubuntu中读取指定目录中的所有文件
摘要:ubuntu系统下,C++程序读取指定文件夹中多个文件,保存文件名列表.文件名没有规律且不考虑读取子文件夹中的文件. 系统配置:ubuntu16.04, cmake编译 首先安利一个函数,输入s ...
- WinForm读取指定的config文件的内容
config文件的使用 一.缘起 最近做项目开始使用C#,因为以前一直使用的是C++,因此面向对象思想方面的知识还是比较全面的,反而是因没有经过完整.系统的.Net方面知识的系统学习,经常被一些在C# ...
- Python--通过索引excel表将文件进行文件夹分类的脚本+读取指定目录下所有文件名的脚本
1.通过索引excel表将文件进行文件夹分类的脚本,此脚本由于将ip和id对应并生成对应id的文件夹将文件进行分类,也可以任意规定表格内容,通过vul_sc_ip.txt和xlsx文件进行索引. # ...
随机推荐
- 160913、ionic + 高德地图定位
实例一: var AMapArea=document.getElementById('amap'); AMapArea.parentNode.style.height="100%" ...
- linux 集群配置ssh无密码访问
一.修改host文件 1) 用客户端工具(ssh client或者putty)连接到linux服务器.在root用户下输入命令 vi /etc/hosts,用vi编辑hosts文件,如下: #127. ...
- C# Community Projects
Community Supported C# Drivers See the officially supported MongoDB C# driver mongodb-csharp driver ...
- Linux环境变量的修改(永久,暂时)以及修改ls显示的时间格式
本文转自:http://blog.sina.com.cn/s/blog_8e21864f01014u9h.html Linux修改环境变量,很简单但很重要 一.Linux的变量种类 按变量的生存周期来 ...
- shell小细节
1.使用变量的时候无需首先声明其类型 2 cut 剪切数据 3.sed 定址 4.read 读取文件(管道) 5 expr 整值计算 6 lp 打印
- 深入理解GCD(一)
虽然 GCD 已经出现过一段时间了,但不是每个人都明了其主要内容.这是可以理解的:并发一直很棘手,而 GCD 是基于 C 的 API ,它们就像一组尖锐的棱角戳进 Objective-C 的平滑世界. ...
- Windows下打包Python的exe可执行文件
参考:http://www.cnblogs.com/Lands-ljk/p/5447723.html
- Python基础第一篇
一.第一句python代码 1.python执行过程:1.加载内存-词法分析-语法分析-编译-执行 2.创建hello.py文件,输入内容 #!/usr/bin/env python print &q ...
- 使用butterknife zelezny插件时必须保证已导入butterKnife的库工程。
使用butterknife zelezny插件时必须保证已导入butterKnife的库工程. 另外需要把光标定位到setContentView(R.layout.activity_main);中的a ...
- Unity-Animator深入系列---控制IK
回到 Animator深入系列总目录 要让代码控制IK,需要先在Animator中打开IK pass 然后,和IK相关的代码需要放到相应的函数中去: void OnAnimatorIK() { Deb ...