Working with Files and Directories in Bada (part 2)
In the previous part of this tutorial we took a look at the File Dialog from the Simple Finger Drawing project and how it showed the contents of a directory in a UI List. Today we will dive deeper into the Bada Osp::Io API related to directories and files.
In the first place, you will need this API for comfortable browsing and management of file-system elements as well as basic/raw file input/output. This manipulations are restricted to specific write/read permissions on a limited number of directories. You can find the complete list in the previous part of this tutorial.
Of course, you can still use higher API for specific media content file input/output. These functions however mostly require corresponding privileges in the manifest.xml. Video, audio and images belong to such content. For example, the functions DecodeN() and EncodeToFile() from Osp::Media::Image are such file input/output methods for Bitmap objects. Examples of their use can be found within the Simple Finger Drawing Project.
In the Osp::Io namespace you will find five classes that are related to Directories and Files:
- Directory – represents a directory object. It allows you to create enumerations of the directory’s contents as well as to create, to rename and to remove directories within the allowed parts of the file-system.
- DirEnumerator – a very simple enumerator that allows you to move forward through directory contents and get one element at a time.
- DirEntry – is an object returned in each loop through a DirEnumerator. Basically it represents one element within a Directory, which can be a file or another directory. There is a bunch of information that you can read here like: is it a directory or not, is it hidden, when was it created, what is its name, is it read only, size, etc.
- File – represents a file object and provides a set of functions to create/read/write data into files. There is also a bunch of static methods that allow you to move, copy and rename files as well as get distinct informations like the extension or the file attributes.
- FileAttributes – a method within the File class returns an object if this class. Using it, you can get diverse information like last modification date, creation date, file size as well as other attributes (read only, hidden, system or directory)
It is noteworthy that there is a certain parity between the FileAttributes class and the DirEntry class. Both provide a subset of methods with the same name and purpose that return information about files/directories. However, while DirEntry is used in context of a loop through a directory’s contents, FileAttributes is returned by File::GetAttributes() by giving it an absolute path that you already know.
Directories
In the second phase of its construction a Directory needs to know its absolute path, that is how it is initialized. If you want to start with the Home directory of your app, you have to give it a “/Home” String:
Directory dir;
if(IsFailed(dir.Construct(L"/Home")))
{
AppLog("Directory not found or could not be initialized!");
}
Once a directory is constructed you can get its contents as you have already seen it here. This is basically all an instantiated Directory object is good for. There are also a few static methods to create, remove and rename directories:
static result Create (const Osp::Base::String &dirPathName, bool createParentDirsToo=false); static result Remove (const Osp::Base::String &dirPathName, bool recursive=false); static result Rename (const Osp::Base::String &orgDirName, const Osp::Base::String &newDirName);
The paths should be absolute and always begin with “/Home“. Following these declarations, we could use these static functions to do the following:
// Will create both Directory1 AND Directory2
Directory::Create("/Home/Directory1/Directory2", true);
// Will rename Directory2 in Directory3
Directory::Rename("/Home/Directory1/Directory2","/Home/Directory1/Directory3");
// Will recursively delete both, Directory1 and Directory3
Directory::Remove("/Home/Directory1/", true);
Files
File objects are constructed via an absolute path that you give to Construct() as well as two additional parameters:
- one for the file opening mode and
- one that specifies whether the parent directories shall be created or not.
So if you take as an example an empty Home directory and you do the following:
File newfile;
newfile.Construct("/Home/NewDir/NewFile.txt", "w", true);
Then both NewDir and NewFile.txt are created. If the directory and file already exist then the Construct() initializes the object without creating these elements within the file system (For example, all the files that are within your project’s Home folder are copied to the /Home folder of your app on the device). A very important thing to note here is the following, if you expect the Construct() method to create a file or a directory that doesn’t exist yet, then the file opening mode must be one of the following: “w”, “w+”, “a”, “a+”. You cannot open a file for reading (“r” or “r+”) and expect it to be created (aka written) at the same time.
Once you have instantiated a File object you can perform (according to the file opening mode) diverse basic cursor-based read/write operations of String, ByteBuffer and void*buffer + length.
File also provides a few static functions to get information about files or to manipulate them.
static result Copy (const Osp::Base::String &srcFilePathName, const Osp::Base::String &destFilePathName, bool failIfExist); static result GetAttributes (const Osp::Base::String &filePathName, FileAttributes &attribute); static const Osp::Base::String GetFileExtension (const Osp::Base::String &filePath); static const Osp::Base::String GetFileName (const Osp::Base::String &filePath); static bool IsFileExist (const Osp::Base::String &filePathName); static result Move (const Osp::Base::String &oldFilePathName, const Osp::Base::String &newFilePathName); static result Remove (const Osp::Base::String &filePathName);
Most of these methods are self-explanatory. There are just a few important things to note here.
When you copy a file from one location to another, the destination path shall always begin with “/Home“. The source file path, however, can begin with any read-enabled directory that you have seen here. You can also tell the Copy method to abort and not to replace existing files in the destination directory.
In a similar manner, all subsequent methods that get some kind of files-related information, can be used within any read-enabled directory. Move() and Remove(), as their names already tell, require write permissions and are therefore limited to the scope of the “/Home” directory.
This is it folks! The Osp::Io API is not that complicated, in fact I have shown the trickiest things here already – and those are actually pretty simple
. Experiment for yourself and feel free to send your suggestions and questions my way!
Related posts:
the Flagship of independent news, reviews and resources for Developers and Users of Samsung's mobile platform Bada 



2 Responses to “Working with Files and Directories in Bada (part 2)”
By zied on Jul 21, 2010 | Reply
i have copied this code from the help oh bada:
String fileName(L”/Home/C:/bada/SDK/1.0.0b3/Model/Wave_LP1/Simulator/FS/Win32FS/Osp/Applications/32z38521sj/Data.txt”);
File file;
char buffer[10];
char buffer2[5];
int i;
int readCnt;
result r = E_SUCCESS;
r = file.Construct(fileName, L”w+”);
for(i = 0 ; i < 10 ; i++)
buffer[i] = (char) i;
r = file.Write(buffer, 10);
but when i excuted it, the out put write for me " destinated file is not constructed" ??
By anonim on Aug 18, 2010 | Reply
File newfile;
newfile.Construct(“/Home/NewDir/NewFile.txt”, “w”, true);
Where do i put these??
If i have a new application.And i want to read data from a file!