1. File Creation
-(Ibaction) createfile { // For error messages Nserror * error; // Create a file manager Nsfilemanager * filemgr = [nsfilemanager defaultmanager]; // Point to the file directory Nsstring * documentsdirectory = [nshomedirectory () stringbyappendingpathcomponent: @ "documents"]; // Create a directory
[[Nsfilemanager defaultmanager] createdirectoryatpath: [nsstring stringwithformat: @ "% @/myfolder", nshomedirectory ()] attributes: Nil]; // File we want to create in the specified ents directory the file we want to create will appear in the file directory
// Result is:/Documents documents/file1.txt. The result is/documents/file1.txt. Nsstring * filepath = [documentsdirectory Stringbyappendingpathcomponent: @ "file2.txt"]; // String to be written Nsstring * STR = @ "iphonedeveloper tips \ nhttp: // iphonedeveloptips, com "; // Write a file [STR writetofile: filepath atomically: Yes encoding: nsutf8stringencoding error: & error]; // Display the contents of the file directory Nslog (@ "documentsdirectory: contentsofdirectoryatpath: documentsdirectory error: & error]);
}
|
|
2. Rename the file
Rename a file
To rename an object, we need to move the object to a new path. The following code creates the desired path of the target file, and then requests to move the file and display the file directory after moving it. // Rename the object by moving the object Nsstring * filepath2 = [documentsdirectory
Stringbyappendingpathcomponent: @ "file2.txt"]; // Determine whether to move If ([filemgr moveitematpath: filepath topath: filepath2 error: & error]! = Yes)
Nslog (@ "unable to move file: % @", [error localizeddescription]); // Display the contents of the file directory Nslog (@ "documentsdirectory: % @",
[Filemgr contentsofdirectoryatpath: documentsdirectoryerror: & error]); |
|
3. delete an object
To complete this technique, let's take a look at how to delete an object: // Determine whether to delete the file in filepath2
If ([filemgr removeitematpath: filepath2 error: & error]! = Yes)
Nslog (@ "unable to delete file: % @", [error localizeddescription]); // Display the contents of the file directory Nslog (@ "documentsdirectory: % @",
[Filemgr contentsofdirectoryatpath: documentsdirectoryerror: & error]);
Once the file is deleted, as you expected, the file directory will be automatically cleared:
These examples can only show you some details about file processing. To get a more comprehensive and detailed explanation, you need to understand the nsfilemanager file. |
|
4. Delete all files in the directory
// Obtain the file path -(Nsstring *) attchmentfolder { Nsstring * Document = [nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes) objectatindex: 0]; Nsstring * Path = [document stringbyappendingpathcomponent: @ "attchments"];
Nsfilemanager * manager = [nsfilemanager defamanager manager];
If (! [Manager contentsofdirectoryatpath: path error: Nil]) {
[Manager createdirectoryatpath: path withintermediatedirectories: No attributes: Nil error: Nil]; }
Return path;
} -- Clear attachments Bool result = [[nsfilemanager defaultmanager] removeitematpath: [[mopappdelegate instance] attchmentfolder] error: Nil];
|
How to obtain the attributes of a file on the iPhone
-(Nsdata *) applicationdatafromfile :( nsstring *) filename
{
Nsarray * paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes );
Nsstring * documentsdirectory = [paths objectatindex: 0];
Nsstring * appfile = [documentsdirectory stringbyappendingpathcomponent: Filename];
Nsdata * Data = [[nsdata alloc] initwithcontentsoffile: appfile] autorelease];
Return data;
}
-(Void) getfileattributes
{
Nsfilemanager * filemanager = [nsfilemanager defaultmanager];
Nsstring * Path = @ "/1ct. rtf ";
Nsdictionary * fileattributes = [filemanager fileattributesatpath: path traverselink: Yes];
Nslog (@"@@");
If (fileattributes! = Nil ){
Nsnumber * filesize;
Nsstring * fileowner, * creationdate;
Nsdate * filemoddate;
// Nsstring * nsfilecreationdate
// File Size
If (filesize = [fileattributes objectforkey: nsfilesize]) {
Nslog (@ "file size: % Qi \ n", [filesize unsignedlonglongvalue]);
}
// File creation date
If (creationdate = [fileattributes objectforkey: nsfilecreationdate]) {
Nslog (@ "file creationdate: % @ \ n", creationdate );
// Textfield. Text = nsfilecreationdate;
}
// File owner
If (fileowner = [fileattributes objectforkey: nsfileowneraccountname]) {
Nslog (@ "Owner: % @ \ n", fileowner );
}
// File modification date
If (filemoddate = [fileattributes objectforkey: nsfilemodificationdate]) {
Nslog (@ "modification date: % @ \ n", filemoddate );
}
}
Else {
Nslog (@ "path (% @) is invalid.", PATH );
}
}
///////////////////
File type. What about file thumbnails ???
======================================
// Obtain the Home Directory of the current application
Nsstring directorypath = nshomedirectory ();
// Obtain all files in the current directory
Nsarray directorycontents = [[nsfilemanager defaultmanager] directorycontentsatpath: directorypath];
// Obtain a file or folder
Nsstring * selectedfile = (nsstring *) [directorycontents objectatindex: indexpath. Row];
// Construct a complete path
[Directorypath stringbyappendingpathcomponent: selectedfile];
Bool isdir;
// Determine whether it is a directory
If ([[nsfilemanager defaultmanager] fileexistsatpath: selectedpath isdirectory: & isdir] & isdir)
{// Directory
}
Else
{// File
}
// Format the date
Nsdateformatter * dateformatter = [[nsdateformatter alloc] init];
[Dateformatter setdatestyle: nsdateformattermediumstyle];
[Dateformatter settimestyle: nsdateformatternostyle];
// Format the number
Nsnumberformatter * numberformatter = [[nsnumberformatter alloc] init];
[Numberformatter setpositiveformat: @ "#,## 0. ## bytes"];
// Get file attributes
Nsdictionary * fileattributes = [[nsfilemanager defaultmanager] fileattributesatpath: directorypath traverselink: Yes];
// Obtain the file creation date
Nsdate * modificationdate = (nsdate *) [fileattributes objectforkey: nsfilemodificationdate];
// Obtain the object size in bytes
Nsnumber * filesize = (nsnumber *) [fileattributes objectforkey: nsfilesize];
// Format the file size
Nsstring A = [numberformatter stringfromnumber: filesize];
// Format the file creation date
Nsstring B = [dateformatter stringfromdate: modificationdate];
[Numberformatter release];
[Dateformatter release];
// Read File Content operation-(void) loadfilecontentsintotextview
{
// Open a file through a stream
Nsinputstream * inputstream = [[nsinputstream alloc] initwithfileatpath: filepath];
[Inputstream open];
Nsinteger maxlength = 128;
Uint8_t readbuffer [maxlength];
// Whether the end ID has been reached
Bool endofstreamreached = no;
// Note: this tight loop will block until stream ends
While (! Endofstreamreached)
{
Nsinteger bytesread = [inputstream read: readbuffer maxlength: maxlength];
If (bytesread = 0)
{// Read the file to the end
Endofstreamreached = yes;
}
Else if (bytesread =-1)
{// File reading error
Endofstreamreached = yes;
}
Else
{
Nsstring * readbufferstring = [[nsstring alloc] initwithbytesnocopy: readbuffer length: bytesread encoding: nsutf8stringencoding freewhendone: No];
// Load non-segmented characters to the view
[Self appendtexttoview: readbufferstring];
[Readbufferstring release];
}
}
[Inputstream close];
[Inputstream release];
}
Asynchronous file reading in the network, network reliability may cause blocking of the above method
Nsstream can work asynchronously. You can register a function that calls back when there are bytes in the stream. If there is no readable, do not block it.