· Introduction
v File Management
v Type of file storage
v File Types
v Enter a file
v File Operations
·
Conclusion
Introduction
We have seen in previous tutorials what you need to know to start
coding with Libgdx to make Android Games or Apps and which we use the Desktop
for making the process of test and debug, and save time for not using the
emulator.
Now that you know
how to create an Android / Desktop project , it is time to learn how to
use file. Try to understand the maximum
to simplify the acquisition of the practical part
Handle Files
As I said and as
you know, and I will say it again (because it is too awesome! ) Libgdx works
across platform s! And what interests us here is to program on Android while doing
tests and debugging on the Desktop.
And as we have
also already mentioned Libgdx has five modules
and one of these modules allow us to handle files.
The
"File" module provides functions that allow the handling files with
those tasks:
·
Reading a file
·
Writing to a file
·
Copy a file
·
Move a file
·
List files and directories
How to save a file
We'll start with
an illustration of the way in which files can be stored in Desktop and Android
:
On
Desktop : Things are
simple, files can be referenced with a relative path or absolute path.
On
Android : The situation is
a bit more complex, files can be stored in the application as resource files
and which must be in the file Android project assets.
On Android, There
is also the internal storage, each installed application has its own repertoire
of internal storage and is only accessible by the application itself.
And there is also
the external storage, such as SD card, which may be available ( memory card
inserted) or not ( memory card removed).
Types of File
ClassPath :
The classpath
files are stored in the source file of your project where the JAR files and
compiled classes are located. These files are always read-only files .
This file type is to be avoided
whenever is possible .
Internal :
On
Desktop : Internal files
are related to the Workspace Desktop Project
On Android
: Internal files
are related to the assets of the Android project file .
These files are read-only.
I recommended in a previous
tutorial to create a file assets in
the Desktop project and link it as an internal file source like Android assets.
You also can linked assets file
from Android as source file to Desktop so you can have one assets file
both for Android and Desktop
External :
On Android
: External files
are files saved in the root directory of the SD memory card
On Desktop
: External files
are files saved in the home directory of the user
These files are read
& write files.
Absolute :
The type of file
that we can only access by specifying the full path .
For portability reasons, this
option should be used only when necessary.
Local :
On Android
: Local files are
files saved in private folder situated in root of game / app directory, this
option has been added recently and among its advantages is that these files are
private to the application, if the application is deleted they will be deleted
too.
On
desktop : Local files are
files saved in the workspace directory of the project or in the root of game /
app directory. On Desktop , internal and local mean the same thing .
External files
are read & write files .
This will summarize
the different types of files.
Type of file
|
Permission
|
Classpath
|
Read Only
|
Interne
|
Read Only
|
Externe
|
Read & Write
|
Absolu
|
Read & Write
|
Local
|
Read & Write
|
Enter a file
It is through the
class FileHande that we can load and handle files, The FileHandle
class from the package com.badlogic.gdx.files is independent of file
types.
FileHandle must be instantiated in case you want to
load text files or binary files such as an image via the Texture class
or sound via the Sound class .
The following
code shows how to get the handle of a file :
internal file
FileHandle fichier
= Gdx.files.internal("data/file.txt");
On
Desktop : The data /
directory is in the root of the project where classes are located.
But as we have
created file assets for Desktop project, it is better to put it in
there.
On Android
: The Date /
directory must be in assets
Classpath file
FileHandle fichier
= Gdx.files.classpath("file.txt");
The " file.txt
" file is located in the project directory that contains the classes
and jar files included.
External file
FileHandle fichier = Gdx.files.external("file.txt");
To recover the
" file.txt " file:
On
Desktop : It must be in
the directory / home / <user> / Linux or in the directory / Users /
<user>
To retrieve the home directory of
your user
System.getProperty(
"user.home"
));
On Android
: It must be in
the root directory of the SD card.
Absolute file
FileHandle
handle = Gdx.files.absolute("/rep1/rep2/rep3/file.txt");
To retrieve the file.txt
file, it must be exactly in the directory specified above / rep1/rep2/rep3 /
File Operations
As I mentioned earlier
FileHandle provides several methods for handling files
Example : check if a file is stored on extern or not
/ if the SD card is inserted or not it :
boolean exist ;
exist=Gdx.files.external("Iexist.txt").exists();
Example : check if a file that exist in external
storage, is a directory or not :
Boolean isDirectory ;
isDirectory =
Gdx.files.external("test/").isDirectory();
Example : List the contents of a directory saved
local storage :
int i =1;
FileHandle[]
fichiers = Gdx.files.local("LocalDirectory/").list();
for(FileHandle
file: files) {
System.out.println("Directory n°"+i+" "+file.name());
i++;
}
list() returns an array of FileHandle that
contains the files and directories that are just in this directory .
Example: ask the parent of a file or a directory:
FileHandle file = Gdx.files.local("D:/Amine/Hamid/").parent();
System.out.print("The directory : "+file.name());
Here the result
displayed is obviously " Amine ".
Example : Creating a FileHandle for a file
located in one of the directories of the internal storage :
FileHandle file =Gdx.files.internal("/data/picture/").child("picture.png");
System.out.print("Directory : "+file.name());
child (File_Name) takes a parameter that is a name of the file you want to get.
There are plenty
of other methods for more details be sure to reference the documentation javadoc http://libgdx.l33tlabs.org/docs/api/
link)
Read and Write into a file:
To read from file : it's very simple.
Example : Retrieve the contents of a file in a String
.
FileHandle file = Gdx.files.internal("file.txt");
String texte = file.readString();
Example : Retrieve the contents of a binary file
FileHandle file = Gdx.files.internal("file.bin");
byte[] bytes = fichier.readBytes();
There are plenty
of other methods that process reading a file, for more information see javadocs.
Write a file : It's as simple as reading, but we have to
consider the type of file because as we have already said only files in
external storage and in local storage and absolute tolerate writing.
Example : write into a file
FileHandle file = Gdx.files.local("file.txt");
file.writeString("blab la blab", false);
Example : write binary data
FileHandle file = Gdx.files.internal("file.bin");
byte[] bytes = file.readBytes();
There are plenty
of other methods that process writing data to a file, for more details see javadocs.
Copy, delete , rename, move files
These operations
are possible for the file types that tolerate writing :
Example : Delete a file or directory
Gdx.files.local("LocalFile.txt").delete();
Example : Renaming a file
Gdx.files.external("ExternalFile1.txt").rename("ExternalFile2.txt");
Example : move a file
Gdx.files.external("mycopy.txt").moveTo(Gdx.files.local("mylocalcopy.txt"));
Example : Copy a file
FileHandle fichierSource =
Gdx.files.internal("ExternalFile.txt");
fichierSource.copyTo(Gdx.files.external("TheCopyExternalFile.txt");
conclusion
In this tutorial,
we saw how to use files across both platforms which will be very useful in the
next tutorial which is an exercise for this part.
Problems ?
If you have any comments or questions please post them in the comments section . Thank you for reading.
See also !
Thank you for this helpful Blog. I will recommend you! :D
ReplyDelete