Objective-c: Use of Nsfilemanager

Source: Internet
Author: User



In the Foundation framework in Objective-c, file operations are implemented by the Nsfilemanager class.



Here's an example of how to create a file, write to a file, and read the contents of a file:


require 'ripl'
require 'ripl / shell_commands'
require 'ripl / color_result'
require 'ripl / color_streams'
require 'ripl / commands'
require 'ripl / rocket'

#Set output color
Ripl.config [: color_streams_stdout] =: light_blue
Ripl.config [: color_streams_stderr] =: light_red

#Definition prompt symbol
Ripl.config [: prompt] = lambda {"[# {Ripl.shell.line.to_s}] cmd>"}

# Define plugins, load files, etc ...

class Dodolook
 def help
 puts "xiaomi noops blog"
 end
 def name
 "wilbur"
 end
end
dodo = Dodolook.new

verison = "1.0.3"
Ripl.start: binding => dodo.instance_eval {binding}
-(void) testFileCreate
{
    NSFileManager * fileManager = [NSFileManager defaultManager];
    NSString * filePath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    filePath = [filePath stringByAppendingPathComponent: @ "new.txt"];
    NSLog (@ "filePath =% @", filePath);
    // determine if the file exists
    if (! [fileManager fileExistsAtPath: filePath]) {
        // If the file does not exist, create a new file
        [fileManager createFileAtPath: filePath contents: nil attributes: nil];
    }
    // Write content to the file, implemented by file handle, NSFileHandle
    NSFileHandle * fileHandle = [NSFileHandle fileHandleForWritingAtPath: filePath];
    NSString * content = @ "hey, brother.This is a test.";
    NSData * contentData = [content dataUsingEncoding: NSUTF8StringEncoding];
    [fileHandle writeData: contentData];
    // close the file
    [fileHandle closeFile];
    
    // read the contents of the file
    fileHandle = [NSFileHandle fileHandleForReadingAtPath: filePath];
    NSData * readData = [fileHandle readDataToEndOfFile];
    // data to NSString
    NSString * readStr = [[NSString alloc] initWithData: readData encoding: NSUTF8StringEncoding];
    NSLog (@ "readStr =% @", readStr);
    [fileHandle closeFile];
    // Read the file directly as NSString
    NSString * contentStr = [NSString stringWithContentsOfFile: filePath encoding: NSUTF8StringEncoding error: nil];
    NSLog (@ "contentStr =% @", contentStr);
}...


Some general operations of files, such as copying files, deleting files, moving files, and so on:


-(void) testFileOperation
{
    // get the temporary directory
    NSString * tempPath = NSTemporaryDirectory ();
    NSLog (@ "tempPath =% @", tempPath);
    // last level directory
    NSLog (@ "last =% @", [tempPath lastPathComponent]);
    // Add a level directory at the end, the original directory is unchanged, and a new directory string is returned
    NSLog (@ "add last =% @", [tempPath stringByAppendingPathComponent: @ "add"]);
    // Delete the last directory, the original directory is unchanged, and return a new directory string
    NSLog (@ "del last =% @", [tempPath stringByDeletingLastPathComponent]);
    NSString * filePath = [tempPath stringByAppendingPathComponent: @ "test.txt"];
    NSLog (@ "filePath =% @", filePath);
    // extension, output as txt
    NSLog (@ "extension =% @", [filePath pathExtension]);
    NSFileManager * manager = [NSFileManager defaultManager];
    if (! [manager fileExistsAtPath: filePath]) {
        [manager createFileAtPath: filePath contents: nil attributes: nil];
    }
    
    NSString * newPath = [tempPath stringByAppendingPathComponent: @ "newtest.txt"];
    // copy the file
    [manager copyItemAtPath: filePath toPath: newPath error: nil];
    if ([manager fileExistsAtPath: newPath]) {
        NSLog (@ "copy success");
    }
    // Delete Files
    [manager removeItemAtPath: newPath error: nil];
    if (! [manager fileExistsAtPath: newPath]) {
        NSLog (@ "remove success");
    }
    
    // whether the file is readable
    if ([manager isReadableFileAtPath: filePath]) {
        NSLog (@ "readable");
    }
    // whether the file is writable
    if ([manager isWritableFileAtPath: filePath]) {
        NSLog (@ "writeable");
    }
} 





Objective-c: Use of Nsfilemanager


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.