The role of Fileutils
Course Video Tutorial Address: http://edu.csdn.net/course/detail/1342/20982?auto_start=1
Fileutils is the file management class within Cocos2d-x. It plays a role in the management of the resource files in our game, and can be said to be the great housekeeper of game resource management. Fileutils can be read and write files, you can set a searchable path, you can obtain the absolute and relative path of the resource file, you can determine whether files and folders exist, you can obtain the size of the resource file, and so on. Many of the required functional fileutils for file operations have a corresponding interface. Therefore, there is no fileutils existence, cocos is incomplete, is incomplete.
How to obtain an instance of Fileutils
Fileutils is obtained by means of its getinstance () method, the code is as follows:
Here's a little tips,cocos2d-x. All methods of acquiring objects through getinstance () are singleton objects. In other words, the fileutils we get from the above code is a singleton object. Let's look at the engine source code to see the implementation of the GetInstance () method.
You can see that a static Fileutils object is declared first, and the GetInstance () method is implemented by a singleton design pattern, and the init () method can be seen in the method body, which is a design pattern that cocos2d-x often uses to cut the unique Called two-segment build mode. In many classes of the Create () method is also the use of static Factory + two segment built design pattern to achieve, as we learn, we will find that there are many classes in Cocos2d-x is the getinstance () method to obtain the instance object, these objects are singleton objects, In other words, there is only one instance object in the engine, which is easy to manage and efficient to use, after all, a static instance is obtained.
Use of Fileutils
Fileutils is mainly used in a few five areas.
First: You can use Fileutils to add a search path to your project, and here's how to add a search path to your project by writing code like this:
Code comments written very clearly, we must understand Addsearchpath and setseachpath the direct difference, because the game's hot update on the use of this knowledge point.
Second: You can use Fileutils to write data to a file with the following code:
From the code we can see, first we want to get a writable path, and then use the C language file for character write operations. After executing the code, a file named "External.txt" is generated in the writable path.
Third: Read the file, we can read the file by the following code:
Run the code, you can see the successful printing of the characters inside the file, printing results:
As you can see from the printed result, the last character we wrote was printed. Then both methods can read the contents of the file, what is the difference between them, the difference is that Getdatafromfile gets the content is binary, need to pass GetBytes to obtain the data read, The Getstringfromfile is a string that is directly derived from string.
Four, determine whether the file or folder exists:
We can use the Fileutils interface to determine whether a file or folder exists, the code is as follows:
Through the code comments people should be able to understand how to use Fileutils method to determine the file, folder exists, here I do not too much for everyone to explain.
V: Create and delete folders
The Fileutils interface also enables you to create and delete folders. The code is as follows:
Course Summary
In this lesson we mainly explain the use of fileutils, through the Fileutils interface call, you can complete the search path settings, file writing, file read, determine whether the file or folder exists, create or delete the folder operation. Easy to use, and by setting the search path for a folder, you can reduce the hassle of writing a full path when you are using a file in real-world development. And through Fileutils's search path has the priority characteristic, may complete the game hot update question. Through the study of this lesson, I hope you have a preliminary understanding of the Cocos2d-x file system, for our future course learning lay a good foundation.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Cocos2d-x from Getting started to mastering lesson four--"fileutils in Cocos2d-x" D