Mysteria

Wednesday, February 19, 2014

Files handling Libgdx



Number
Category
Translate

 

11

How? 

French
  
This is the practical part of this tutorial " Files with Libgdx "
If you have retained the theoretical part of the tutorial " Files with Libgdx " you will have no trouble understanding what follows.

What we will do ?

We will show in this part how to do the three main operations ( load , read, write ) for each file type.
We will create for each file’s type a class that implements ApplicationListener. In each class we will perform the operation.
      ·         Upload a file : which is in a location, and if there is not we will create one
      ·         Write in the file
      ·         Read a file: then display its contents in the Console on the Desktop and in Logcat on Android.

You should try to do this exercise alone. If you fail , do not waste too much time, you can see the solution as you learn the basic file manipulation .

Solution

After creating the Desktop project " FilesDesktop " and Android project " FilesAndroid " and a class for each type of file :
      ·         LocalFile
      ·         AbsoluteFile
      ·         ClaspathFile
      ·         InterneFile
      ·         ExterneFile
We will only focus on the create () method other methods will be empty.


 1) Local File
LocalFile class that implements ApplicationListener, here is the content of the create() method
@Override
public void create() {
            
//************* Local file ***************************/

       // Load local file
       file = Gdx.files.local("File_Local.txt");
       if(!file.exists())
       {
             try {
                    file.file().createNewFile();
             } catch (IOException e) {
                    e.printStackTrace();
             }
       }
            
    // Write into local file
       file.writeString(String.valueOf("Hello to you\nI am "+file.type().toString()), false);
            
    // Read from local file
       br = file.reader(100,"Big5");
       String line;
       String  content = null;
       Gdx.app.log("Content of local file ", "");
      try {
             line = br.readLine();    

             while (line != null)
             {
                    content = content +"\n"+line;
                    line = br.readLine();    
             }
       }catch (IOException e) {
             e.printStackTrace();
       }
       Gdx.app.log(""+content, "");
}

Result
We will not be interested in the display of the screen but in the contents of the Desktop Console And Logcat for Android

On Desktop
Launch of LocalFile class by class DesktopLauncher
In Console:
Content of local file :
Hello to you
I am Local:
The file is located in the project directory desktop " FilesDesktop "

On Android
Run the Localfile class by MainActivity class
In Logcat :
You will see the content of Local file
The directory where the " File_Local.txt " file is located : Data / data / package_name / files /
  
2) External File
In ExterneFile class created that implements ApplicationListener, here is the content of the create() method
@Override
public void create() {
//***********************  Externe file ****************************/    

       // Check the existence of the external storage
       if(Gdx.files.isExternalStorageAvailable())   // if SD card is inserted
       {
             file = Gdx.files.external("File_Externe.txt");
             if(!file.exists())
             {
                    try {
                           file.file().createNewFile();
                    } catch (IOException e) {
                           e.printStackTrace();
                    }
             }

             // Write into Externe file
             file.writeString(String.valueOf("Hello to you\nI am "+file.type().toString()), false);
                   
             // Read from Classpath file
             br = file.reader(100,"Big5");
             String line;
             String  content = null;
             Gdx.app.log("Content of Externe file", "");
             try {
                    line = br.readLine();    
                    content = line;
                    while (line != null)
                    {
                           line = br.readLine();    
                           if(line !=null)
                                  content = content +"\n"+ line;
                    }
             }catch (IOException e) {
                    e.printStackTrace();
             }
             Gdx.app.log(""+content, "");
       }else
       {
             Gdx.app.error(" Externe storage not available", " insert SD card and tray again");
       }
}

Be careful ! Android must have permission to write into an external file, for that you need to go to the manifest.xml file located in the Android project " FilesAndroid " and to do these steps :

Under the permission tab à then click Add  à Uses Permission


Then, select android.permission.WRITE_EXTERNAL_STORAGE

Click Ctrl + S to save the configuration

Result

We will not be interested in the display of the screen but in the contents of the Desktop Console And Logcat for Android

On Desktop
Launch of ExterneFile class by class DesktopLauncher
In Console:
Content of Externe file:
Hello to you
I am External:
The directory where the " File_Externe.txt " file : C: \ Users \ home

On Android
Launch of ExterneFile class by MainActivity class
In Logcat :
You will see these lines
Content of Externe file:
Hello to you
I am External:
The directory where the " File_Externe.txt " file : / mnt / sdcard /

3) Internal File

In InterneFile class created that implements ApplicationListener here is the content of the create () method
@Override
       public void create() {
      
             //***********************  Interne file ****************************/    

             // Load the internal file
             file = Gdx.files.internal("File_Interne.txt");

             // Write into Interne file
              // << if we write an error will occur >> //

             // Read from Interne file
             br = file.reader(100,"Big5");
             String line;
             String  content = null;
             Gdx.app.log("Internal file content", "");
             try {
                    line = br.readLine();    
                    content = line;
                    while (line != null)
                    {
                           line = br.readLine();    
                           if(line !=null)
                                  content = content +"\n"+ line;
                    }
             }catch (IOException e) {
                    e.printStackTrace();
             }
             Gdx.app.log(""+content, "");
       }

It will load the " File_Interne.txt " file located in assets
So we need to create a file named File_Interne.txt and write into it :
Hello to you
I'm an internal file
Then put it in asset folder of the project

Result

We will not be interested in the display of the screen but in the content of the Desktop Console
And Logcat for Android


On Desktop
Launch of InterneFile class by DesktopLauncher class
In Console:
Internal file content:
Hello to you
I am Interne: 
The file is located in the Desktop project " FilesDesktop " directory

On Android
Launch of InterneFile class by MainActivity class
In Logcat :
You will see the content written into the interne file

4) File Classpath

In ClasspathFile class created that implements ApplicationListener, here is the content of the create () method

@Override
public void create() {
            
//***********************  Classpath file ****************************/  
            
       // Load the Classpath file
       file = Gdx.files.classpath(""+Gdx.files.getExternalStoragePath()+"/File_Classpath.txt");
       if(!file.exists())
       {
             try {
                    file.file().createNewFile();
             } catch (IOException e) {
                    e.printStackTrace();
             }
       }

      // Write into Classpath file
         // << if we write an error will occur >> //

       // Read from Classpath file
       br = file.reader(100,"Big5");
       String line;
       String  content = null;
       try {
                    line = br.readLine();    
                    Gdx.app.log("Content of the Classpath file", "");
                           if(line == null)
                    {
                           Gdx.app.log("empty", "");
                    }
                    else
                    {
                           while (line != null)
                           {
                                  Gdx.app.log(""+line, "");
                                  content = content+"\n"+line;
                                  line = br.readLine();    
                           }
                    }     
             }catch (IOException e) {
                    e.printStackTrace();
             }
}

Result
We will not be interested in the display of the screen but in the content of the Desktop Console
And Logcat for Android

On Android
Launch of the Classpath class by MainActivity class
In Logcat : we see the display of the file contents

On Desktop
Launch of the Classpath class by DesktopLauncher class
In Console: we see the display of the file contents
The file is located in the desktop project " FilesDesktop " directory

5) Absolute File

In AbsoluteFile class created that implements ApplicationListener, here is the content of the create () method
@Override
public void create() {
      
       //***********************  Absolue file ****************************/    

       // load file with Absolute way
       file = Gdx.files.absolute(""+Gdx.files.getExternalStoragePath()+"/File_Absolute.txt");
       if(!file.exists())
       {
             try {
                    file.file().createNewFile();
              } catch (IOException e) {
                    e.printStackTrace();
             }
       }

       // write in the absolute file
       file.writeString(String.valueOf("Hello\nI am "+file.type().toString()+" file"), false);

       // read from the absolute file
       br = file.reader(100,"Big5");
       String line;
       String  content = null;
       Gdx.app.log("content of the Absolute file", "");
       try {
             line = br.readLine();    
             content= line;
             if(line == null)
             {
                    Gdx.app.log("empty", "");
             }
             else
             {
                    while (line != null)
                    {
                           line = br.readLine();  
                           if(line!=null)
                                  content = content+"\n"+line;
                    }
             }
       }catch (IOException e) {
             e.printStackTrace();
       }
       Gdx.app.log(""+content, "");
}

Result
We will not be interested in the display of the screen but in the content of the Desktop Console
And Logcat for Android

On Android
Launch of AbsoluteFile class by MainActivity class
In Logcat :
You can see the contents of this file in the Logcat
The directory where the " File_Absolute.txt " file is located : / mnt / sdcard /

On Desktop
Launch of AbsoluteFile class by class DesktopLauncher
In Console:
content of the Absolute file:
Hello
I am Absolute file:
The directory where the " File_Absolute.txt " file is located : C: \ Users \ home which is the external location for the desktop.
  
It's over !
You can download both Android and desktop projects to import into your workspace .
FilesDesktop Project
FilesAndroid Project
They are independent

Congratulations we have seen all types of file which you do not have to use them all. And also you need to know that there is other way to record data, we will try to see them in the future.

 <<  Previous             

Main Menu   

Problems ?
If you have any comments or questions please post them in the comments section . Thank you for reading.


See also !

 Files with libgdx

No comments:

Post a Comment