One 、 The difference between file manager and file connector
File manager (NSFileManager)
This class mainly refers to the operation of files ( establish / Delete / Name change, etc ) And access to file information .
File connector (NSFileHandle)
This class mainly reads and writes the contents of the file .
Two 、 File manager (NSFileManager)
1、 Create a folder manager
NSFileManager *fileManager = [NSFileManager defaultManager];
2、 Create files and write data
// Create a folder manager
NSFileManager *fileManager = [NSFileManager defaultManager];
// Create a path
NSString *homePath = NSHomeDirectory();
homePath = [homePath stringByAppendingPathComponent:@"string.txt"];
// Initializing a string
7 NSString *string = @" Create a file and write data ";
// create a file string.txt And to stirng.txt Add data to
// Parameters 1: File path
// Parameters 2:NSData type
// Parameters 3: Parameters
BOOL result = [fileManager createFileAtPath:homePath contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
if (result) {
NSLog(@" success %@", homePath);
} else {
NSLog(@" Failure ");
}
3、 Reading data from a file
// Create a folder manager
NSFileManager *fileManager = [NSFileManager defaultManager];
// Create a path
NSString *homePath = NSHomeDirectory();
homePath = [homePath stringByAppendingPathComponent:@"string.txt"];
// from string.txt Get data in
NSData *data = [fileManager contentsAtPath:homePath];
// take NSData Type of data to NSString Data of type , And the output
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"string %@", string);
4、 File movement
// Create a folder manager
NSFileManager *fileManager = [NSFileManager defaultManager];
// Create a path
NSString *homePath = NSHomeDirectory();
NSString *filePath = [homePath stringByAppendingPathComponent:@"string.txt"];
// establish toPath
NSString *toPath = [homePath stringByAppendingPathComponent:@"test.txt"];
// take string.txt The data in is moved to test.txt in , And will string.txt Delete
BOOL result = [fileManager moveItemAtPath:filePath toPath:toPath error:nil];
if (result) {
NSLog(@" success %@", toPath);
} else {
NSLog(@" Failure ");
}
5、 Copy of documents
// Create a folder manager
NSFileManager *fileManager = [NSFileManager defaultManager];
// Create a path
NSString *homePath = NSHomeDirectory();
NSString *filePath = [homePath stringByAppendingPathComponent:@"test.txt"];
// establish toPath
NSString *toPath = [homePath stringByAppendingPathComponent:@"copy.txt"];
// take test.txt Copy data from to copy.txt in , Retain test.txt file
BOOL result = [fileManager copyItemAtPath:filePath toPath:toPath error:nil];
if (result) {
NSLog(@" success %@", toPath);
} else {
NSLog(@" Failure ");
}
6、 Compare the contents of two files to see if they are the same
// Create a folder manager
NSFileManager *fileManager = [NSFileManager defaultManager];
// Create a path
NSString *homePath = NSHomeDirectory();
NSString *filePath = [homePath stringByAppendingPathComponent:@"test.txt"];
// establish toPath
NSString *toPath = [homePath stringByAppendingPathComponent:@"copy.txt"];
// Compare the contents of the two files
BOOL result = [fileManager contentsEqualAtPath:filePath andPath:toPath];
if (result) {
NSLog(@" The content is consistent ");
} else {
NSLog(@" Inconsistent content ");
}
7、 Judge whether the file exists
// Create a folder manager
NSFileManager *fileManager = [NSFileManager defaultManager];
// Create a path
NSString *homePath = NSHomeDirectory();
NSString *filePath = [homePath stringByAppendingPathComponent:@"test.txt"];
// Judge whether the file exists
BOOL result = [fileManager fileExistsAtPath:filePath];
if (result) {
NSLog(@" File exists ");
} else {
NSLog(@" file does not exist ");
}
8、 remove file
// Create a folder manager
NSFileManager *fileManager = [NSFileManager defaultManager];
// Create a path
NSString *homePath = NSHomeDirectory();
NSString *filePath = [homePath stringByAppendingPathComponent:@"test.txt"];
// remove file
BOOL result = [fileManager removeItemAtPath:filePath error:nil];
if (result) {
NSLog(@" Removed successfully ");
} else {
NSLog(@" Remove failed ");
}
9、 Create folder
1、 Create... According to the given folder path
// Create a folder manager
NSFileManager *fileManager = [NSFileManager defaultManager];
// Create a path
NSString *homePath = NSHomeDirectory();
NSString *filePath = [homePath stringByAppendingPathComponent:@"test1/test2"];
// Create folder
BOOL result = [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
if (result) {
NSLog(@" Create success ");
} else {
NSLog(@" Create failure ");
}
2、 According to the given file path , Create the folder it's in
// Create a folder manager
NSFileManager *fileManager = [NSFileManager defaultManager];
// Create a path
NSString *homePath = NSHomeDirectory();
NSString *filePath = [homePath stringByAppendingPathComponent:@"test3/test4/hello.txt"];
// Create folder test4
// stringByDeletingLastPathComponent Delete the last one in the path / And what follows
BOOL result = [fileManager createDirectoryAtPath:[filePath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];
if (result) {
NSLog(@" Create success ");
} else {
NSLog(@" Create failure ");
}
3、 ... and 、 File connector (NSFileHandle)
NSFileHandle Is very basic, only for the operation of file content ( write in , Read , to update ), It's a NSData Write byte by byte through the connector / Read the file .(NSData <—> NSFileHandle <—> file ).
Use scenarios :
Partial modification of the contents of the document 、 Additional content .
Use steps
1). File docking and get a NSFileHandle object .
2). Read and write operations
3). Turn off docking
Be careful :NSFileHandle Class does not provide the ability to create files . You have to use NSFileManager Method to create a file . therefore , When using the methods in the table below , It's all about ensuring that the documents already exist , Otherwise return to nil.
1、 Reading data
1、 Read all the data in the file
// string.txt Already exist , Get its path
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"string.txt"];
// Create a file connector , And open a file to read
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:filePath];
// from string.txt Read all data in
NSData *data = [handle availableData];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"string %@", string);
// Close the file connector
[handle closeFile];
2、 Read from the specified location to the end of the file
Be careful , When the data in the file is Chinese characters , because utf-8 The Chinese character is three bytes , So the offset has to be 3 Multiple , Otherwise, the data cannot be read
// string.txt Already exist , Get its path
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"string.txt"];
// Create a file connector , And open a file to read
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:filePath];
// obtain stirng.txt Number of bytes of data in
NSUInteger length = [[handle availableData] length];
// The offset is half of the file
[handle seekToFileOffset:length/2.0];
// Gets the offset of the current file
NSLog(@"%llu", [handle offsetInFile]);
// Read from half of the data , Read to the end of the file
NSData *data = [handle readDataToEndOfFile];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"string %@", string);
[handle closeFile];
3、 Reads data of a specified length from a specified location
// string.txt Already exist , Get its path
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"string.txt"];
// Create a file connector , And open a file to read
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:filePath];
// obtain stirng.txt Medium data length
NSUInteger length = [[handle availableData] length];
// The offset is half of the file
[handle seekToFileOffset:length/2.0];
// Read from half of the data , Read 3 Bytes
NSData *data = [handle readDataOfLength:];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"string %@", string);
[handle closeFile];
2、 Intercepts the file to the specified length
// string.txt Already exist , Get its path
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"string.txt"];
// Create a file connector , And open a file to write and read
NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:filePath];
// Set the length of the file to 21 byte
[handle truncateFileAtOffset:];
[handle closeFile];
3、 Splice data in the specified location of the file
// string.txt Already exist , Get its path
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"string.txt"];
// Create a file connector , And open a file to write ( Or update ) Read
NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:filePath];
// Search to the end of the file content
[handle seekToEndOfFile];
NSString *appendStr = @" world";
// Splice strings at the end of the file
[handle writeData:[appendStr dataUsingEncoding:NSUTF8StringEncoding]];
[handle closeFile];
UI Advanced File manager (NSFileManager) File docking device (NSFileHandle) More articles about
- 【 primary 】iOS Learning file manager (NSFileManager) And file docking (NSFileHandle)
1. File manager (NSFileManager) 1> Main functions and methods The main role : This class mainly refers to the operation of files ( establish / Delete / Name change, etc ) And access to file information . Functional approach : 2> Create folder Create Institute ...
- 02-IOSCore - NSFileHandle、 Merge files 、 The file pointer 、 File viewer
[day0201_NSFileHandle]: File handle 1 NSFileHandle File docking device . File handle Commonly used API: - (NSData *)readDataToEndOfFile; Read data to the end ...
- win10 uwp Open file manager and select File
this paper : Let the file manager select the file , Not getting files from file manager . If some documents have been obtained , So how to select these files from the file manager ? The method is simple to use . Take pictures from the Internet Open the folder and automatically select all files First you need to get the folder , because ...
- 2018-8-10-win10-uwp- Open file manager and select File
title author date CreateTime categories win10 uwp Open file manager and select File lindexi 2018-08-10 19:16:50 +0800 2018 ...
- file NSKeyedArchiver De archiving NSKeyedUnarchiver And file management NSFileManager ( File operations )
========================== File operations ========================== One . file NSKeyedArchiver 1. The first way : Store a kind of data . // file ...
- Windows Store App JavaScript Development : File picker
As in the previous chapter C# What is introduced in the language , File picker is an interface between application and system , The file picker can interact with the file system directly in the application , Access files or folders in different locations , Or store the file in a specified location . The file picker is divided into two parts ...
- Imagine again Windows 8 Store Apps (26) - Picker : Custom file selection window , Custom file save window
original text : Imagine again Windows 8 Store Apps (26) - Picker : Custom file selection window , Custom file save window [ Source download ] Imagine again Windows 8 Store Apps (26) ...
- fight to win or die Windows 10 (95) - Picker : Custom file save picker
[ Source download ] fight to win or die Windows 10 (95) - Picker : Custom file save picker author :webabcd Introduce the battle behind the back Windows 10 And Picker Custom file save picker Example 1. Show me how to ...
- fight to win or die Windows 10 (94) - Picker : Custom file open picker
[ Source download ] fight to win or die Windows 10 (94) - Picker : Custom file open picker author :webabcd Introduce the battle behind the back Windows 10 And Picker Custom file open picker Example 1. Show me how to ...
Random recommendation
- ( turn ) Walk into JVM, You can catch fish in shallow water
This is not a description jvm What's the article about , No introduction jvm Cross platform features , It's not about jvm Security features article , It's not the explanation jvm Command operation , Data operations article , This article focuses on the life cycle of types . The life cycle of a type involves : Class loading .j ...
- Fraction to Recurring Decimal leetcode
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- be based on Python Of Web Application development practice summary
be based on Python Of Web Application development learning summary Project address This study adopts Flask frame . Develop personal blog system according to the tutorial . The blog interface is shown in the figure . The whole learning process gains a lot , Here is a summary of the study . 1.virtualenv ...
- hbase- Data recovery process
quote <https://blog.csdn.net/nigeaoaojiao/article/details/54909921> hlog Introduce : hlog structure : As you can see from the diagram , For one hl ...
- Asp.net Webform Page lifecycle for
1. Analyze the resource path of the request , Find the corresponding resource file in the directory , If the resource file cannot be found , Then return to 404 error 2. Analysis of resource files Page command , adopt Page Instructions to find code files and classes 3. Compile the page file and class together to generate the final class ( Only in the third quarter ...
- Simple web Testing process ( Reprint )
Reprinted from http://blog.csdn.net/qq_35885203 1. The interface operation mode is on jmeter Get into jmeter Installation directory bin Under the table of contents , double-click “jmeter.bat” The file can be opened jmeter ...
- JS Learning notes ( One )DOM Events and monitoring
Three ways to bind an event to an element : 1.HTML Event handler ( It is not recommended to use ) 1 <a onclick="hide()"> 2. Conventional DOM Event handler That is, in the goal DOM event ...
- vue nextTick In depth understanding of -vue performance optimization 、DOM Update time 、 Event cycle mechanism
One . Definition [nextTick. The event loop ] nextTick The origin of : because VUE Data driven view update for , It's asynchronous , That is, the moment of data modification , The view will not be updated immediately , It's the same as after all the data changes in an event loop are completed , Then unify the views ...
- spring boot(1)-Hello World
spring boot brief introduction spring boot By spring A new framework officially launched , Yes spring It's highly encapsulated , yes spring The future direction of development .spring boot It has many functions , The main function is ...
- Kotlin------ Data types and Syntax
Today, let's briefly introduce Kotlin Basic syntax . Programming languages are mostly interlinked , I can learn basic knowledge very quickly , The theory is the same , The implementation code language is different . value type Kotlin How to deal with numerical value and java Very similar , But it's not exactly the same . such as ...