Objective-c files used (2)

Source: Internet
Author: User

Path: NSPathUtilities. h

 

NSPathUtilities. h contains NSString functions and classification extensions, which allow you to operate on the path name.

 

The following is an example:

# Import <Foundation/NSArray. h> <br/> # import <Foundation/NSString. h> <br/> # import <Foundation/NSFileManager. h> <br/> # import <Foundation/NSAID utoreleasepool. h> <br/> # import <Foundation/NSPathUtilities. h> </p> <p> int main (int argc, const char * argv []) <br/> {<br/> NSAID utoreleasepool * pool = [[NSAID utoreleasepool alloc] init]; <br/> NSFileManager * fm; <br/> NSString * fName = @ "path. m "; <br/> NSString * path, * t Empdir, * extension, * homedir, * fullpath; <br/> NSString * upath = @"~ Stevekochan/progs /.. /ch16 /. /path. m "; <br/> NSArray * components; </p> <p> fm = [NSFileManager defamanager manager]; </p> <p> // Get the temporary working directory </p> <p> tempdir = NSTemporaryDirectory (); <br/> NSLog (@ "Temporary Directory is % @", tempdir ); </p> <p> // Extract the base directory from current directory <br/> path = [fm currentDirectoryPath]; <br/> NSLog (@ "Base dir is % @", [path lastPathComponent]); </p> <p> // Create a full path to the file fName in current directory <br/> fullpath = [path stringByAppendingPathComponent: fName]; <br/> NSLog (@ "fullpath to % @ is % @", fName, fullpath ); </p> <p> // Get the file name extension <br/> extension = [fullpath pathExtension]; <br/> NSLog (@ "extension for % @ is % @", fullpath, extension ); </p> <p> // Get user's home directory <br/> homedir = NSHomeDirectory (); <br/> NSLog (@ "Your home directory is % @", homedir ); </p> <p> // Divide a path into its components <br/> components = [homedir pathComponents]; </p> <p> for (int I = 0; I <[components count]; I ++) <br/>{< br/> NSLog (@ "% @", [components objectAtIndex: I]); <br/>}</p> <p> // "Standardize" a path <br/> NSLog (@ "% >%@", upath, [upath stringByStandardizingPath]); </p> <p> [pool drain]; <br/> return 0; <br/>}</p> <p>

 

 

 

 

Copy files and use the NSProcessInfo class

 

Example:

# Import <Foundation/nsarray. h> <br/> # import <Foundation/nsstring. h> <br/> # import <Foundation/nsfilemanager. h> <br/> # import <Foundation/NSAID utoreleasepool. h> <br/> # import <Foundation/nspathutilities. h> <br/> # import <Foundation/nsprocessinfo. h> </P> <p> // implement a basic copy utility <br/> int main (INT argc, const char * argv []) <br/>{< br/> NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init]; <br /> Nsfilemanager * FM; <br/> nsstring * Source, * DEST; <br/> bool isdir; <br/> nsprocessinfo * proc = [nsprocessinfo processinfo]; <br/> nsarray * ARGs = [proc arguments]; </P> <p> fm = [nsfilemanager defaultmanager]; </P> <p> // check for two arguments on the command line <br/> If ([ARGs count]! = 3) <br/>{< br/> nslog (@ "Usage: % @ src dest", [proc processname]); <br/> return 1; <br/>}</P> <p> source = [ARGs objectatindex: 1]; <br/> DEST = [ARGs objectatindex: 2]; </P> <p> // make sure the source file can be read <br/> If ([FM isreadablefileatpath: Source] = No) <br/>{< br/> nslog (@ "can't read % @", source); <br/> return 2; <br/>}</P> <p> // see if the destination file is a directory <br/> // If I T is, add the source to the end of the destination </P> <p> [FM fileexistsatpath: DEST isdirectory: & isdir]; </P> <p> If (isdir = Yes) <br/> {<br/> DEST = [DEST stringbyappendingpathcomponent: [Source lastpathcomponent]; <br/>}</P> <p> // remove the destination file if it already exists <br/> [FM removefileatpath: DEST handler: Nil]; </P> <p> // okay, time to perform the copy <br/> If ([FM copypath: Source Topath: DEST handler: Nil] = No) <br/>{< br/> nslog (@ "Copy failed"); <br/> return 3; <br/>}</P> <p> nslog (@ "Copy of % @ to % @ succeeded! ", Source, DEST); </P> <p> [pool drain]; <br/> return 0; <br/>}</P> <p>

 

 

 

 

Basic file operation: NSFileHandle

 

Use the methods provided by the NSFileHandle class to allow me to use files more effectively. In general, we have to go through the following three steps when processing files:

1. open the file and obtain an NSFileHandle object to reference the file in subsequent I/O operations.

2. Perform the I/O operation on the opened files.

3. close the file.

 

The following instance opens the original testfile file, reads its content, and copies it to the file named testout.

The Code is as follows:

# Import <Foundation/NSObject. h> <br/> # import <Foundation/NSString. h> <br/> # import <Foundation/NSFileManager. h> <br/> # import <Foundation/NSFileHandle. h> <br/> # import <Foundation/NSAID utoreleasepool. h> <br/> # import <Foundation/NSData. h> </p> <p> // Implement a basic copy utility <br/> int main (int argc, const char * argv []) <br/>{< br/> NSAID utoreleasepool * pool = [[NSAID utoreleasepool alloc] init]; <br/> NSFileHandle * inFile, * outFile; <br/> NSData * buffer; </p> <p> // Open the file testfile.txt for reading <br/> inFile = [NSFileHandle fileHandleForReadingAtPath: @ "testfile.txt"]; <br/> if (inFile = nil) <br/>{< br/> NSLog (@ "Open of testfile for reading failed"); <br/> return 1; <br/>}</p> <p> // Create output file first if necessary <br/> [[NSFileManager defaultManager] createFileAtPath: @ "testout.txt" contents: nil attributes: nil]; </p> <p> // Now open outfile for writing <br/> outFile = [NSFileHandle fileHandleForWritingAtPath: @ "testout.txt"]; </p> <p> if (outFile = nil) <br/> {<br/> NSLog (@ "Open of testout for writing failed "); <br/> return 2; <br/>}</p> <p> // Truncate the output file since it may contain data <br/> [outFile truncateFileAtOffset: 0]; </p> <p> // Read the data from inFile and write it to outFile <br/> buffer = [inFile readDataToEndOfFile]; </p> <p> [outFile writeData: buffer]; </p> <p> // Close the two files <br/> [inFile closeFile]; <br/> [outFile closeFile]; </p> <p> // Verify the file's contents <br/> NSLog (@ "% @", [NSString stringWithContentsOfFile: @ "testout.txt" <br/> encoding: NSUTF8StringEncoding error: nil]); </p> <p> [pool drain]; <br/> return 0; <br/>}< br/>

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.