override func viewDidLoad() {
super.viewDidLoad()
self.imagePath()
self.boundlePath()
self.fileManagerDo()
}
func imagePath() {
let DocumentsPath:String = NSHomeDirectory().stringByAppendingString("/Documents")
print("DocumentsPath:" + DocumentsPath);
let fileManager = NSFileManager.defaultManager()
try! fileManager.createDirectoryAtPath(DocumentsPath, withIntermediateDirectories: true, attributes: nil)
let path = NSBundle.mainBundle().pathForResource("fire", ofType: "jpg")
print("path:" + path!)
let data = NSData(contentsOfFile: path!)
fileManager.createFileAtPath(DocumentsPath + "/image.png", contents: data, attributes: nil)
let filePath = DocumentsPath + "/image.png"
print("filePath:" + filePath)
}
func boundlePath() {
let resourcesPath1 = NSBundle.mainBundle().pathForResource("fire", ofType: "jpg")
print("resourcesPath1:" + resourcesPath1!)
let filePath = NSHomeDirectory()
print("fielPath:" + filePath)
let fileArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let fileDocPath = fileArray.last
print("fileDocPath:" + fileDocPath!)
}
func fileManagerDo() {
let fileManage = NSFileManager.defaultManager()
let filePath = NSHomeDirectory()
try! fileManage.createDirectoryAtPath(filePath, withIntermediateDirectories: true, attributes: nil)
let testFilepath = filePath + "/test.txt"
let testStr = "Hello World"
try! testStr.writeToFile(testFilepath, atomically: true, encoding: NSUTF8StringEncoding)
let image = UIImage(named: "fire.jpg")
let data : NSData = UIImageJPEGRepresentation(image!, 1.0)!
let data1 : NSData = UIImagePNGRepresentation(image!)!
data.writeToFile(filePath + "/1.jpg", atomically: true)
data1.writeToFile(filePath + "/2.png", atomically: true)
let array = NSArray(objects: "Apple","Orange","就会俩水果的英语啦,��")
array.writeToFile(filePath + "/array.plist", atomically: true)
let dictionary:NSDictionary = ["beijing":"北京","shanghai":"上海","hangzhou":"杭州"]
dictionary.writeToFile(filePath + "/dictionary.plist", atomically: true)
var has = fileManage.fileExistsAtPath(filePath)
let textPath = filePath + "/test.txt"
let newPath = filePath + "/Library" + "/test1.txt"
try! fileManage.moveItemAtPath(textPath, toPath: newPath)
let arrayPath = filePath + "/array.plist"
let arrayNewPath = filePath + "/Library" + "/array1.plist"
try! fileManage.copyItemAtPath(arrayPath, toPath: arrayNewPath)
var fileArray = fileManage.subpathsAtPath(filePath)
try! fileManage.removeItemAtPath(arrayPath)
let documentPath = filePath + "/Documents"
try! fileManage.removeItemAtPath(documentPath)
}