精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
锐英源精品开源,禁止转载和任何形式的非法内容使用,违者必究
最近写个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'作为指示指导添加追加吗?
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!
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()
}