-
Notifications
You must be signed in to change notification settings - Fork 0
/
StreamWriter.swift
56 lines (49 loc) · 1.42 KB
/
StreamWriter.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import Foundation
public class StreamWriter{
let encoding:UInt!
var fileHandle:NSFileHandle!
let delimData:NSData!
init?(path:String, delimiter:String="\n", encoding:UInt=NSUTF8StringEncoding){
self.encoding = encoding
//file handle
let fileManager = NSFileManager.defaultManager()
if !fileManager.fileExistsAtPath(path){
fileManager.createFileAtPath(path,contents:nil,attributes:nil)
}
if let fileHandle = NSFileHandle(forWritingAtPath:path){
self.fileHandle = fileHandle
}else{
return nil
}
//delimiter
if let delimData = delimiter.dataUsingEncoding(encoding){
self.delimData = delimData
}else{
return nil
}
}
deinit{
self.close()
}
public func println(data:String)->Bool{
if let nsData = data.dataUsingEncoding(encoding){
fileHandle.writeData(nsData)
fileHandle.writeData(delimData)
return true
}
return false
}
public func print(data:String)->Bool{
if let nsData = data.dataUsingEncoding(encoding){
fileHandle.writeData(nsData)
return true
}
return false
}
public func close(){
if fileHandle != nil{
fileHandle.closeFile()
fileHandle = nil
}
}
}