func writeStrToFile(receivedString: NSString) {
let str = receivedString as String
let wr = NSMutableData()
wr.append(str.data(using:String.Encoding(rawValue: String.Encoding.utf8.rawValue))!)
let manager = FileManager.default
let urlsForDocDirectory = manager.urls(for:.documentDirectory, in:.userDomainMask)
let docPath = urlsForDocDirectory[0]
let file = docPath.appendingPathComponent("\(Demo!).xml")
let writeHandler = try? FileHandle(forWritingTo:file)
if writeHandler != nil {
writeHandler!.seekToEndOfFile()
writeHandler?.write(wr as Data)
}
}
五、获取指定路径中的所有文件名
func getPathAllFileName(path:String) -> [String]{
var fileNameArray:[String] = []
do {
fileNameArray = try FileManager.default.contentsOfDirectory(atPath: path)
} catch let error as NSError {
print("get file path error: \(error)")
}
return fileNameArray
}
六、获取指定路径中的所有xml文件名称
func getPathAllXmlFile(path:String) -> [String]{
var fileNameArray:[String] = []
var xmlFileArray:[String] = []
do {
fileNameArray = try FileManager.default.contentsOfDirectory(atPath: path)
for fileName in fileNameArray {
if fileName.hasSuffix(".xml") {
let xmlFileName = fileName.prefix(fileName.count-4)
xmlFileArray.append(String(xmlFileName))
}
}
} catch let error as NSError {
print("get file path error: \(error)")
}
return xmlFileArray
}