Friday, 15 December 2017

File Manager

1) Create file:-

@IBAction func btn_create_file(_ sender: UIButton) {
        let _ : NSError? = nil
        file_path = document_Dir?.appendingPathComponent("ios_demo.txt") as NSString?
        file_Manager?.createFile(atPath: file_path! as String, contents: nil, attributes: nil)
        
        self.show_Alert(title_alert: "Success", message_alert: "File create successfully")
        

    }



2) Read File:-

@IBAction func btn_read_file(_ sender: UIButton) {
        file_path = document_Dir?.appendingPathComponent("/ios_demo.txt") as NSString?
        let file_content : NSData?
        
        file_content = file_Manager?.contents(atPath: file_path! as String)! as NSData?
        let str : NSString = NSString(data: file_content! as Data, encoding: String.Encoding.utf8.rawValue)!
        
        self.show_Alert(title_alert: "Success", message_alert: "Data\(str)" as NSString)
    }


3) Write File:-

@IBAction func btn_write_file(_ sender: UIButton) {
        let content : NSString = NSString(string: "Hello... I'm Kaumil")
        let file_content:NSData = content.data(using: String.Encoding.utf8.rawValue)! as NSData
        
        file_content.write(toFile: document_Dir!.appendingPathComponent("ios_demo.txt"), atomically: true)
        self.show_Alert(title_alert: "Success", message_alert: "Content Written")
    }

4) Move File:-

@IBAction func btnMoveClicked(sender: AnyObject)
    {
        let oldFilePath:String = (documentDir!.appendingPathComponent("file1.txt") as NSString?)! as String
        let newFilePath:String = (documentDir!.appendingPathComponent("/folder1/file1.txt") as NSString?)! as String
        
        do {
            
            try fileManager?.moveItem(atPath: oldFilePath, toPath: newFilePath)
        }
        catch let error as NSError {
            print("Unable to create database \(error.debugDescription)")
            
        }
        
        self.showSuccessAlert(titleAlert: "Success", messageAlert: "File moved successfully")
    }

5) Remove File:-

@IBAction func btnRemoveFile(sender: AnyObject)
    {
        filePath  =  documentDir?.appendingPathComponent("file1.txt") as NSString?
        do {
            
            try fileManager?.removeItem(atPath: filePath! as String)
        }
        catch let error as NSError {
            print("Unable to create database \(error.debugDescription)")
            
        }
        
        self.showSuccessAlert(titleAlert: "Message", messageAlert: "File removed successfully.")
    }

6) Copy File:-

@IBAction func btnCopyFileClicked(sender: AnyObject)
    {
        let originalFile = documentDir?.appendingPathComponent("file1.txt") as NSString?
        let copyFile = documentDir?.appendingPathComponent("copy.txt") as NSString?
        do {
            
            try fileManager?.copyItem(atPath: originalFile! as String, toPath: copyFile! as String)
        }
        catch let error as NSError {
            print("Unable to create database \(error.debugDescription)")
            
        }
        
        self.showSuccessAlert(titleAlert: "Success", messageAlert:"File copied successfully")
    }

7) Create Directory:-

@IBAction func btnCreateDirectoryClicked(sender: AnyObject)
    {
        filePath = documentDir?.appendingPathComponent("/folder1") as NSString?
        do {
            
            try fileManager?.createDirectory(atPath: filePath! as String, withIntermediateDirectories: false, attributes: nil)
        }
        catch let error as NSError {
            print("Unable to create database \(error.debugDescription)")
            
        }
        
        self.showSuccessAlert(titleAlert: "Success", messageAlert: "Directory created successfully")
    }

8) File Permissions:-

@IBAction func btnFilePermissionClicked(sender: AnyObject)
    {
        filePath = documentDir?.appendingPathComponent("file1.txt") as NSString?
        var filePermissions:NSString  =  ""
        
        if((fileManager?.isWritableFile(atPath: filePath! as String)) !=  nil)
        {
            filePermissions = filePermissions.appending("file is writable. ") as NSString
        }
        if((fileManager?.isReadableFile(atPath: filePath! as String)) !=  nil)
        {
            filePermissions = filePermissions.appending("file is readable. ") as NSString
        }
        if((fileManager?.isExecutableFile(atPath: filePath! as String)) !=  nil)
        {
            filePermissions = filePermissions.appending("file is executable.") as NSString
        }
        self.showSuccessAlert(titleAlert: "Success", messageAlert: "\(filePermissions)" as NSString)
    }

9) Equality Check:-

@IBAction func btnEqualityClicked(sender: AnyObject)
    {
        let filePath1 = documentDir?.appendingPathComponent("file1.txt") as NSString?
        let filePath2 = documentDir?.appendingPathComponent("file2.txt") as NSString?
        
        if((fileManager? .contentsEqual(atPath: filePath1! as String, andPath: filePath2! as String)) !=  nil)
        {
            self.showSuccessAlert(titleAlert: "Message", messageAlert: "Files are equal.")
        }
        else
        {
            self.showSuccessAlert(titleAlert: "Message", messageAlert: "Files are not equal.")
        }
    }

10) Directory Contents:-

@IBAction func btnDirectoryContentsClicked(sender: AnyObject)
    {
        
        do {
            
            let arrDirContent = try fileManager!.contentsOfDirectory(atPath: documentDir! as String)
            self.showSuccessAlert(titleAlert: "Success", messageAlert: "Content of directory \(arrDirContent)" as NSString)
        }
        catch let error as NSError {
            print("Unable to create database \(error.debugDescription)")
            
        }
        
    }

11) Show Alert Button:-

func showSuccessAlert(titleAlert:NSString,messageAlert:NSString)
    {
        let alert:UIAlertController = UIAlertController(title:titleAlert as String, message: messageAlert as String, preferredStyle: UIAlertControllerStyle.alert)
        let okAction  =  UIAlertAction(title: "OK", style: UIAlertActionStyle.default)
        {
            UIAlertAction in
        }
        alert.addAction(okAction)
        if UIDevice.current.userInterfaceIdiom  ==  .phone
        {
            self.present(alert, animated: true, completion: nil)
        }
    }

No comments:

Post a Comment