gpt4 book ai didi

ios - 如何读取服务器上的文本文件?

转载 作者:可可西里 更新时间:2023-11-01 02:19:20 24 4
gpt4 key购买 nike

我用这段代码读取本地文本文件,成功了!

class StreamReader  {   
let encoding : UInt
let chunkSize : Int

var fileHandle : NSFileHandle!
let buffer : NSMutableData!
let delimData : NSData!
var atEof : Bool = false

init?(path: String, delimiter: String = "\n", encoding : UInt = NSUTF8StringEncoding, chunkSize : Int = 4096) {
self.chunkSize = chunkSize
self.encoding = encoding

if let fileHandle = NSFileHandle(forReadingAtPath: path),
delimData = delimiter.dataUsingEncoding(encoding),
buffer = NSMutableData(capacity: chunkSize)
{
self.fileHandle = fileHandle
self.delimData = delimData
self.buffer = buffer
} else {
self.fileHandle = nil
self.delimData = nil
self.buffer = nil
return nil
}
}

deinit {
self.close()
}

/// Return next line, or nil on EOF.
func nextLine() -> String? {
precondition(fileHandle != nil, "Attempt to read from closed file")

if atEof {
return nil
}

// Read data chunks from file until a line delimiter is found:
var range = buffer.rangeOfData(delimData, options: nil, range: NSMakeRange(0, buffer.length))
while range.location == NSNotFound {
let tmpData = fileHandle.readDataOfLength(chunkSize)
if tmpData.length == 0 {
// EOF or read error.
atEof = true
if buffer.length > 0 {
// Buffer contains last line in file (not terminated by delimiter).
let line = NSString(data: buffer, encoding: encoding)

buffer.length = 0
return line as String?
}
// No more lines.
return nil
}
buffer.appendData(tmpData)
range = buffer.rangeOfData(delimData, options: nil, range: NSMakeRange(0, buffer.length))
}

// Convert complete line (excluding the delimiter) to a string:
let line = NSString(data: buffer.subdataWithRange(NSMakeRange(0, range.location)),
encoding: encoding)
// Remove line (and the delimiter) from the buffer:
buffer.replaceBytesInRange(NSMakeRange(0, range.location + range.length), withBytes: nil, length: 0)

return line as String?
}

/// Start reading from the beginning of file.
func rewind() -> Void {
fileHandle.seekToFileOffset(0)
buffer.length = 0
atEof = false
}

/// Close the underlying file. No reading must be done after calling this method.
func close() -> Void {
fileHandle?.closeFile()
fileHandle = nil
}
}

用法

var path_up1 = "/Users/rrr/Desktop/up/1.txt"
if let aStreamReader = StreamReader(path: path_up1) {
while let line = aStreamReader.nextLine() {
println(line)
}
}

但我必须读取服务器上的文件(例如:http://www.1111.com/1.txt)。

请帮帮我!

最佳答案

可以通过NSURLConnection直接读取文件

let url = NSURL(string:"http://www.1111.com/1.txt")!
let request = NSURLRequest(URL: url)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.currentQueue()) { (response, data, error) -> Void in
if error != nil {
println(error!)
} else {
if let textFile = NSString(data: data, encoding: NSUTF8StringEncoding) {
println(textFile)
}
}
}

该示例假定文本文件是 UTF-8 编码的。

或者如果你想顺序读取数据,实现 NSURLConnection 的委托(delegate)方法并使用 init?(request:delegate:startImmediately:)

关于ios - 如何读取服务器上的文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32154616/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com