锐英源软件
第一信赖

精通

英语

开源

擅长

开发

培训

胸怀四海 

第一信赖

当前位置:锐英源 / 开源技术 / IOS开源社区 / SWIFT用NSInputStream实现文件按行读取
服务方向
人工智能数据处理
人工智能培训
kaldi数据准备
小语种语音识别
语音识别标注
语音识别系统
语音识别转文字
kaldi开发技术服务
软件开发
运动控制卡上位机
机械加工软件
软件开发培训
Java 安卓移动开发
VC++
C#软件
汇编和破解
驱动开发
联系方式
固话:0371-63888850
手机:138-0381-0136
Q Q:396806883
微信:ryysoft

锐英源精品开源,禁止转载和任何形式的非法内容使用,违者必究


SWIFT用NSInputStream实现文件按行读取

背景

最近写个App,里面读取大文件,用NSData方式能把文件内容全部读取,但因为文件大,害怕占用内存也大,就想用缓冲方式,一行一行读取。结果找了很多地方,没这样的代码,从XCode里的帮助也找不到线索,很是头疼。这里翻译的文章也没有真正到位地解决问题,希望有经验的朋友进行指导,对编程细节和思路进拓展。


提问

I'm trying to match reading files in my iPhone app just like in Android with InputStream.

 

How I do it in Android is like this:

我正在尝试匹配我的iPhone应用程序中的读文件,就像Android中的InputStream一样。我在Android中实现代码如下:

String line = "";
int count = 0;
FileInputStream fis = tempContext.openFileInput("myfile.txt"); // This just get File Path
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
// LOAD ARRAY LINE BY LINE
try {
while (count<myMaxNumber) {
line = br.readLine();
myArray[count] = line;
count++;
}
fis.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}


I've used code to write to a txt file successfully, but I cannot read line by line. It will read the entire file as it's made to do, and add the '\n' breaks, but I want it to read one line, and go to the next line until there are no bytes我已经使用代码成功写入txt文件,但我无法逐行读取。它将按原样读取整个文件,并添加'\ n'分隔,但我希望它读取一行,然后转到下一行直到没有字节。

if (checkFileExists()) {

let dir = dirs![0]; //documents directory
var getDocPath = dir.stringByAppendingPathComponent("favlist.txt")

var data : NSData = NSData(contentsOfFile: getDocPath)!
var inputStream : NSInputStream = NSInputStream(data: data)

let bufferSize = 1500
var myBuffer = Array<UInt8>(count: bufferSize, repeatedValue: 0)

inputStream.open()

var bytesRead = inputStream.read(&myBuffer, maxLength: bufferSize)
var tempStuff = NSString(bytes: &myBuffer, length: bytesRead, encoding: NSUTF8StringEncoding)

if (inputStream.hasBytesAvailable) {
for (var a = 0; a < myFavListMax; a++) {
// I KNOW THIS SETS THE ENTIRE TEXT FILE WITH NEW LINE BREAKS IN EVERY INDEX
// I NEED SOMETHING WHICH READS LINE BY LINE
myFavList[a] = String(tempStuff!)
}
}

inputStream.close()
}

Is there another way to do this? What is the equivalent to the android way for SWIFT in IOS 8? Can I read by Array and add '\n' as an indication to add append when using an array to read?还有另一种方法吗?在IOS 8中,SWIFT处理方式有和android方式一样的吗?
我可以通过Array读取并在使用数组读取时添加'\ n'作为指示指导添加追加吗?

回答1

I didn't have much time to get back to this but I guess this doesn't exist in NSInputStream so I just loaded everything in a NSString, then copy to a String and load into a new Array by splitting.

Works fine for what I expected!

我没有太多时间回到这个,但我想这在NSInputStream中不存在,所以我只是将所有内容加载到NSString中,然后复制到String并通过拆分加载到新的Array中。

适合我的预期!
if (checkFileExists()) {

let dir = dirs![0]; //documents directory
var getDocPath = dir.stringByAppendingPathComponent("favlist.txt")

var data : NSData = NSData(contentsOfFile: getDocPath)!
var inputStream : NSInputStream = NSInputStream(data: data)

let bufferSize = 1500
var myBuffer = Array<UInt8>(count: bufferSize, repeatedValue: 0)

inputStream.open()

var bytesRead = inputStream.read(&myBuffer, maxLength: bufferSize)
var textFileContents = NSString(bytes: &myBuffer, length: bytesRead, encoding: NSUTF8StringEncoding)
var a : String = textFileContents!
var textFileContentsTempArray = split(a){$0 == "\n"}

for (var a = 0; a < myFavListMax; a++) {
myFavList[a] = textFileContentsTempArray[a]
}

inputStream.close()
}
友情链接
版权所有 Copyright(c)2004-2021 锐英源软件
公司注册号:410105000449586 豫ICP备08007559号 最佳分辨率 1024*768
地址:郑州大学北校区院(文化路97号院)内