"Open source Java Game Framework libgdx topic" -07-File Processing

Source: Internet
Author: User
Tags file handling libgdx

Description: File handling on different platforms is slightly different from file management
    • Desktop (Windows,linux,mac OS x, and so on): in desktops, the file system is a chunk of memory. The file can be referenced by the current working directory or by an absolute path. Permissions can be ignored, and files and directories can often be read and written by all programs.
    • Android:
The situation in Android is a bit complicated. Files can be stored in the app's apk in the form of resources or assets. These files are read-only. LIBGDX only uses the assets mechanism, assets provides access to the bitstream of the original file and is closest to the traditional file system. Resource files are available for Android's regular apps, but there are a lot of problems when using the game. Android can control resource files when loaded, such as automatically resizing pictures. The assets is stored in the assets directory of the Android project and will be packaged into the APK, which cannot be accessed by other apps. Files can also be saved to the internal storage for read and write. Each installed application has a separate directory, and this directory is accessible only to the application. You can treat this storage as a private storage space for your app. Files can also be stored in external memory, like SD cards. External memory are not available at all times. The directory for file storage is not stable, and you need to add relevant permissions in Androidmanifest.xml before using external memory.
1 <  android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
    • iOS: All file types in iOS are temporarily unavailable.
    • JavaScript or WebGL:
An original JavaScript or WEBGL application does not have a traditional file system concept. Resources such as images are implemented via URLs. The browser now natively supports local storage, which is basically similar to a traditional file system. LIBGDX provides you with an abstraction for a read-only file system.
    • The file in Libgdx represents an instance of a FileHandle class, and a filehandle has a type that specifies where the file is stored.
Type Descrption Desktop Android HTML5 Ios
Classpath Read-only Yes Yes NO Yes
Internal Read-only Yes Yes Yes Yes
Local Read-write Yes Yes No Yes
External Read-write Yes Yes No Yes
Absolute Absolute path, use as little as possible Yes Yes No Yes
      • For absolute paths and classpath, typically used for the path of a desktop program, the order of use should look like this:
      1. Internal files: All assets that are packaged with the program are internal (Internal files)
      2. Local files: If you need to write some small files, such as the state of the game, use local files. These are generally private to the application.
      3. External files: If you want to write some large files, such as or download good files, can be stored in External files, it should be noted that the external files are not stable, users can delete or move the files you write.
    • Check storage availability and path
    • You can query the available information through the file model:
      • 1 boolean isextavailable =Gdx.files.isExternalStorageAvailable (); 2 boolean islocavailable =gdx.files.islocalstorageavailable ();
    • You can also query the root path of a file like this:
      • 1 String extroot =Gdx.files.getExternalStoragePath (); 2 String locroot =gdx.files.getlocalstoragepath ();
      • get file handle
      •  1  filehandle handle =gdx.files.internal ("Dat A/myfile.txt " 2  filehandle handle =gdx.files.classpath ("MyFile.txt"); //  will be in the project directory  3  filehandle handle = Gdx.files.external ("MyFile.txt"); //  4  filehandle handle =gdx.files.absolute ("/some_dir/subdir/myfile.txt"); //
          Get the absolute path of the file  
      1. Check that the file exists, check if it is a directory:
      2. 1 Boolean exists =gdx.files.external ("Doitexist.txt"). exists (); 2 boolean isdirectory =gdx.files.external ("test/"). Isdirectory ();
      1. Listing file directories is also very simple
      2. 1 filehandle[] files =gdx.files.local ("mylocaldir/"). List (); 2  for (FileHandle file:files) {3     // Do something interesting here 4 }
      1. We can also get a handle to the parent file or child file with the same file
      2. 1 filehandle parent =gdx.files.internal ("Data/graphics/myimage.png"). Parent (); 2 filehandle child =gdx.files.internal ("data/sounds/"). Child ("Myaudiofile.mp3");
      1. Read content from File:
 1  filehandle file =gdx.files.internal ("MyFile.txt" ); 2  String text = file.readstring ();  3   Gets the binary data  4  filehandle file =gdx.files.internal ("Myblob.bin" ); 5  byte  [] bytes = File.readbytes (); 
      1. write content to file
      2.  1  filehandle file =gdx.files.loca L ("myfile.txt"  2  file.writestring ("My God, it's full of stars",  );  3   Binary file  4  filehandle file =gdx.files.local ("Myblob.bin" ); 5  file.writebytes (newbyte[] {3,-2, ten}, ); 
      1. Delete, copy, rename, and move files/directories
      2. 1 filehandle from =gdx.files.internal ("Myresource.txt"); 2 From.copyto (Gdx.files.external ("Myexternalcopy.txt")); 3 Gdx.files.external ("Myexternalcopy.txt"). Rename ("Mycopy.txt"); 4 Gdx.files.external ("Mycopy.txt"). MoveTo (Gdx.files.local ("Mylocalcopy.txt")); 5 Gdx.files.local ("Mylocalcopy.txt"). Delete ();

        The original is written by Bo Master Le Zhi Editor, the copyright belongs to the Bo owner. The original address http://www.dtblog.cn/1134.html reprint please specify the source!

"Open source Java Game Framework libgdx topic" -07-File Processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.